2520是能被1到10整除的最小数字,找出能被1到20整除的最小数字。

解决方案 »

  1.   


    public class Test {
    private static int TOTAL = 20;
    private static int[] PRIMEs = {2, 3, 5, 7, 11, 13, 17, 19};

    public static void main(String[] args) {
    long result = 1;
    int[] middleResult = new int[TOTAL];
    boolean flag = false; for (int i = 0, j; i < PRIMEs.length; i++) {
    for (j = 0; j < TOTAL; j++) {
    if (middleResult[j] == 0) middleResult[j] = j + 1;
    if (middleResult[j] %PRIMEs[i] == 0 ) {
    middleResult[j] /= PRIMEs[i];
    flag = true;
    }
    }
    if (flag) {
    result *= PRIMEs[i];
    i--;
    flag = false;
    }
    }
    System.out.println(result);
    }
    }Result = 232792560
      

  2.   


    public static void main(String[] args) { int[] nums = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
    System.out.println(minComMuNum(nums));

    }

    public static int minComMuNum(int[] nums){
    int result = 1;
    for (int i = 0; i < nums.length; i++) {
    result = getMCM(result, nums[i]);
    }
    return result;
    }

        public static int getMCD(int i, int j)
        {
            return j == 0 ? i : getMCD(j, i % j);
        }    public static int getMCM(int i, int j)
        {
            return i / getMCD(i, j) * j;
        }
      

  3.   

        int[] primes = new int[] {
            2, 3, 5, 7, 11, 13, 17, 19
        };
        int[] exponents = new int[primes.length];
        long result = 1;
        int limit = 20;
        for (int n = 2; n <= limit; n++) {
          for (int i = 0; i < primes.length; i++) {
            int x = n;
            int e = 0;
            int p = primes[i];
            if (p >= x) {
              break;
            }
            do {
              e++;
              x /= p;
              if (e > exponents[i]) {
                result *= p;
                exponents[i] = e;
              }
            } while (x % p == 0);
          }
        }
        System.out.println(Arrays.toString(exponents));
        System.out.println(result);
      

  4.   


    public class Test {
    public static void main(String args[]) throws IOException {
    int i=2*2*2*2*3*3*5*7*11*13*17*19;
    System.out.println(i);
    }
    }232792560