我希望找到最低的整数' 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。 / * y是最小可能的整数* /
答案 0 :(得分:2)
您在寻找最低的完美立方体吗?也许是这样的?
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;
}
(其中MAX
可能是Math.cbrt(INT_MAX)
,我认为x > 1
)
答案 1 :(得分:1)
将数字分解为素数然后添加数字所需的素数的一般解决方案具有整数立方根。
例如,如果您的号码为225
,则会获得3^2
* 5^2
,并且您需要3
可排除的数字(作为多维数据集根目录),因此,您将其乘以y=3^1*5^1
如果您有32
,则为2^5
,因此您需要将其乘以2^6
,因为6
的权力可以3
< / p>
有了这个输出
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;
}