找到你要的答案

Q:Number to be multiplied to form a perfect cube

Q:要相乘成完美立方体的数

I wish to find the lowest integer 'y' which when multiplied to number 'x', 'x*y' should form a perfect cube. Here's what I have done.

boolean flag=false;
int i=1;
long y=0;
while(flag!=true){
  long abc= x*i 
  double apple = Math.cbrt(abc);
  int al = (int)apple;
  if(apple==al){
    flag=true;
    y=i;
  }
  i++;
}

Above code solves the purpose, but is there any better way to do this?

For example x=9 than y=3. /* y is least possible integer*/

我希望找到最低的整数“Y”乘以乘以x,“x * y”应该形成一个完美的立方体。以下是我所做的。

boolean flag=false;
int i=1;
long y=0;
while(flag!=true){
  long abc= x*i 
  double apple = Math.cbrt(abc);
  int al = (int)apple;
  if(apple==al){
    flag=true;
    y=i;
  }
  i++;
}

以上代码解决了这个目的,但是有没有更好的方法呢?

例如x = 9比y = 3。是最小可能整数*

answer1: 回答1:

Are you looking for the lowest perfect cube? Maybe something like this?

int y(int x) {
    int i = 1;
    while(i < MAX) {
       int n = i * i * i;
       if(n % x == 0) {
           return n / x;
       }
       i++;
    }
    return -1;
}

(where MAX might be Math.cbrt(INT_MAX) and I assume x > 1)

你在寻找最低的完美立方体吗?也许像这样?

int y(int x) {
    int i = 1;
    while(i < MAX) {
       int n = i * i * i;
       if(n % x == 0) {
           return n / x;
       }
       i++;
    }
    return -1;
}

(在最大可能的数学。银行(int_max)我认为X & gt;1)

answer2: 回答2:

The general solution to decompose number into primes and then add primes that are needed to number have an whole number cube root.

For example if you have number 225, you get 3^2*5^2 and you need power of that numbers devidable by 3 (to be a cube root), therefore you multiply it with y=3^1*5^1

If you have 32, it is 2^5, so you need to multiply it to have 2^6 because power of 6 is devidable by 3

Having this output

for number 27 this is result map {} which means that y=1 and perfect cube is equal 27
for number 3 this is result map {3=2} which means that y=9 and perfect cube is equal 27
for number 15 this is result map {3=2, 5=2} which means that y=225 and perfect cube is equal 3375
for number 16 this is result map {2=2} which means that y=4 and perfect cube is equal 64
for number 17 this is result map {17=2} which means that y=289 and perfect cube is equal 4913
for number 32 this is result map {2=1} which means that y=2 and perfect cube is equal 64
for number 45 this is result map {3=1, 5=2} which means that y=75 and perfect cube is equal 3375

With this code

public static void main(String[] args) {
    Map<Integer,Integer> map;
    int x;

    x = 27;
    map = perfectCube(x);
    System.out.println("for number " + x + " this is result map " + map + " which means that y=" + mapToInteger(map) + " and perfect cube is equal " + (mapToInteger(map)*x));

    x = 3;
    map = perfectCube(x);
    System.out.println("for number " + x + " this is result map " + map + " which means that y=" + mapToInteger(map) + " and perfect cube is equal " + (mapToInteger(map)*x));

    x = 15;
    map = perfectCube(x);
    System.out.println("for number " + x + " this is result map " + map + " which means that y=" + mapToInteger(map) + " and perfect cube is equal " + (mapToInteger(map)*x));

    x = 16;
    map = perfectCube(x);
    System.out.println("for number " + x + " this is result map " + map + " which means that y=" + mapToInteger(map) + " and perfect cube is equal " + (mapToInteger(map)*x));

    x = 17;
    map = perfectCube(x);
    System.out.println("for number " + x + " this is result map " + map + " which means that y=" + mapToInteger(map) + " and perfect cube is equal " + (mapToInteger(map)*x));

    x = 32;
    map = perfectCube(x);
    System.out.println("for number " + x + " this is result map " + map + " which means that y=" + mapToInteger(map) + " and perfect cube is equal " + (mapToInteger(map)*x));

    x = 45;
    map = perfectCube(x);
    System.out.println("for number " + x + " this is result map " + map + " which means that y=" + mapToInteger(map) + " and perfect cube is equal " + (mapToInteger(map)*x));

}

private static int mapToInteger(Map<Integer, Integer> map){
    int x=1;
    for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
        x*= Math.pow(entry.getKey(), entry.getValue());
    }
    return x;
}

private static Map<Integer, Integer> perfectCube(int x) {
    Map<Integer, Integer> map = mapPrimes(x);
    Map<Integer, Integer> mapY = new HashMap<>();

    for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
        if ( (entry.getValue()+1)/3.0 == (entry.getValue()+1)/3){
            mapY.put(entry.getKey(), 1);
        } else if ( (entry.getValue()+2)/3.0 == (entry.getValue()+2)/3){
            mapY.put(entry.getKey(),2);
        }
    }

    return mapY;
}


public static Map<Integer, Integer> mapPrimes(int numbers) {
    Map<Integer, Integer> map = new HashMap<>();
    List<Integer> primes = primeFactors(numbers);
    for (Integer prime : primes) {
        if (map.containsKey(prime)) {
            int value = map.get(prime);
            map.put(prime, value + 1);
        } else {
            map.put(prime, 1);
        }
    }
    return map;
}

public static List<Integer> primeFactors(int numbers) {
    int n = numbers;
    List<Integer> factors = new ArrayList<>();
    for (int i = 2; i <= n / i; i++) {
        while (n % i == 0) {
            factors.add(i);
            n /= i;
        }
    }
    if (n > 1) {
        factors.add(n);
    }
    return factors;
}

一般的解决方案分解为素数的素数数然后添加所需要的数量有一个完整的数的立方根。

例如,如果你有225,你得到3 ^ 2×5 ^ 2你需要的数devidable 3功率(是一个立方根),因此你将它与y = 3 ^ 1×5 ^ 1

如果你有32,这是2 ^ 5,所以你需要乘有2 ^ 6因为功率6是devidable 3

在这个输出

for number 27 this is result map {} which means that y=1 and perfect cube is equal 27
for number 3 this is result map {3=2} which means that y=9 and perfect cube is equal 27
for number 15 this is result map {3=2, 5=2} which means that y=225 and perfect cube is equal 3375
for number 16 this is result map {2=2} which means that y=4 and perfect cube is equal 64
for number 17 this is result map {17=2} which means that y=289 and perfect cube is equal 4913
for number 32 this is result map {2=1} which means that y=2 and perfect cube is equal 64
for number 45 this is result map {3=1, 5=2} which means that y=75 and perfect cube is equal 3375

这段代码

public static void main(String[] args) {
    Map<Integer,Integer> map;
    int x;

    x = 27;
    map = perfectCube(x);
    System.out.println("for number " + x + " this is result map " + map + " which means that y=" + mapToInteger(map) + " and perfect cube is equal " + (mapToInteger(map)*x));

    x = 3;
    map = perfectCube(x);
    System.out.println("for number " + x + " this is result map " + map + " which means that y=" + mapToInteger(map) + " and perfect cube is equal " + (mapToInteger(map)*x));

    x = 15;
    map = perfectCube(x);
    System.out.println("for number " + x + " this is result map " + map + " which means that y=" + mapToInteger(map) + " and perfect cube is equal " + (mapToInteger(map)*x));

    x = 16;
    map = perfectCube(x);
    System.out.println("for number " + x + " this is result map " + map + " which means that y=" + mapToInteger(map) + " and perfect cube is equal " + (mapToInteger(map)*x));

    x = 17;
    map = perfectCube(x);
    System.out.println("for number " + x + " this is result map " + map + " which means that y=" + mapToInteger(map) + " and perfect cube is equal " + (mapToInteger(map)*x));

    x = 32;
    map = perfectCube(x);
    System.out.println("for number " + x + " this is result map " + map + " which means that y=" + mapToInteger(map) + " and perfect cube is equal " + (mapToInteger(map)*x));

    x = 45;
    map = perfectCube(x);
    System.out.println("for number " + x + " this is result map " + map + " which means that y=" + mapToInteger(map) + " and perfect cube is equal " + (mapToInteger(map)*x));

}

private static int mapToInteger(Map<Integer, Integer> map){
    int x=1;
    for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
        x*= Math.pow(entry.getKey(), entry.getValue());
    }
    return x;
}

private static Map<Integer, Integer> perfectCube(int x) {
    Map<Integer, Integer> map = mapPrimes(x);
    Map<Integer, Integer> mapY = new HashMap<>();

    for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
        if ( (entry.getValue()+1)/3.0 == (entry.getValue()+1)/3){
            mapY.put(entry.getKey(), 1);
        } else if ( (entry.getValue()+2)/3.0 == (entry.getValue()+2)/3){
            mapY.put(entry.getKey(),2);
        }
    }

    return mapY;
}


public static Map<Integer, Integer> mapPrimes(int numbers) {
    Map<Integer, Integer> map = new HashMap<>();
    List<Integer> primes = primeFactors(numbers);
    for (Integer prime : primes) {
        if (map.containsKey(prime)) {
            int value = map.get(prime);
            map.put(prime, value + 1);
        } else {
            map.put(prime, 1);
        }
    }
    return map;
}

public static List<Integer> primeFactors(int numbers) {
    int n = numbers;
    List<Integer> factors = new ArrayList<>();
    for (int i = 2; i <= n / i; i++) {
        while (n % i == 0) {
            factors.add(i);
            n /= i;
        }
    }
    if (n > 1) {
        factors.add(n);
    }
    return factors;
}
java