Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Calc {
- public static void main(String[] args) {
- try {
- int a = Integer.parseInt(args[0]);
- int b = Integer.parseInt(args[1]);
- System.out.println(calc(a, b));
- } catch (Exception e) {
- e.printStackTrace(System.err);
- System.exit(1);
- }
- }
- private static int gcd(int a, int b) {
- if (a == 0 || b == 0) {
- throw new IllegalArgumentException();
- }
- a = Math.abs(a);
- b = Math.abs(b);
- while (b > 0) {
- int c = a;
- a = b;
- b = c % b;
- }
- return a;
- }
- private static int lcm(int a, int b) {
- a = Math.abs(a);
- b = Math.abs(b);
- return a / gcd(a, b) * b;
- }
- private static int lcm_quo(int a, int b) {
- a = Math.abs(a);
- b = Math.abs(b);
- return a / gcd(a, b);
- }
- private static int calc(int a, int b) {
- int sum = 0;
- for (int i = 1; i <= a; ++i) {
- sum += lcm_quo(i, b);
- }
- return sum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement