# 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*/

``````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++;
}
``````

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）

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) {
n /= i;
}
}
if (n > 1) {
}
return factors;
}
``````

``````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) {