java vs ruby中的质数

uxhixvfz  于 2023-08-01  发布在  Java
关注(0)|答案(1)|浏览(73)

我试着将Ruby代码转换为Java代码:
text
代码在下面。正如您所看到的,因为java中没有质数(Ruby中的require prime行使所有数字都是质数),所以我的程序只是返回所有数字。我不知道如何在Java中实现。感谢帮助。

import java.util.HashSet;
import java.util.Set;

public class sus {
    public static void main(String[] args) {
        long maxNum = 9876543210L;
        int minNum = 1023456789;
        int maxPrimeToCube = 2143;
        int count = 0;

        for (int primeToCube = 2; primeToCube <= maxPrimeToCube; primeToCube++) {
            int cubedVal = primeToCube * primeToCube * primeToCube;

            for (int primeToSquare = 2; primeToSquare <= maxPrimeToCube; primeToSquare++) {
                int squaredVal = primeToSquare * primeToSquare;
                int combinedVal = squaredVal + cubedVal;

                if (combinedVal < minNum) {
                    continue;
                }

                if (combinedVal > maxNum) {
                    break;
                }

                String s = String.valueOf(combinedVal);
                Set<Character> uniqueDigits = new HashSet<>();
                for (char digit : s.toCharArray()) {
                    uniqueDigits.add(digit);
                }

                if (uniqueDigits.size() == 10) {
                    count++;
                    System.out.println("val: " + s + " = " + primeToSquare + "^2 + " + primeToCube + "^3, count: " + count);
                }
            }
        }
        System.out.println(count);
        
    }
}

字符串
_________________________________________________________-

kjthegm6

kjthegm61#

我相信你的代码中的错误是你没有使用质数。

  • Ruby**Prime* 类将是一个素数列表。

您只是使用了一个值范围,从 22143
您可以使用 Java 类来模拟 Prime 类。
这将用素数填充 List,给定对象示例化期间提供的 maximum 值。

class Prime {
    List<Integer> list;

    Prime(int maximum) {
        list = new ArrayList<>();
        boolean prime;
        for (int valueA = 2; valueA <= maximum; valueA++) {
            prime = true;
            for (int valueB = 2; valueB * valueB <= valueA ; valueB++) {
                if (valueA % valueB == 0) {
                    prime = false;
                    break;
                }
            }
            if (prime) list.add(valueA);
        }
    }
}

字符串
下面是一个示例用法。

for (int prime_to_cube : new Prime(2143).list)


此外,您可以使用以下代码模拟 StringSet 的转换:

// Check if all digits are unique by converting to a set
s = String.valueOf(combined_val);
if (new HashSet<>(Arrays.asList(s.split(""))).size() == 10)


这里是一个完整的重构,来自 Ruby 代码。
对于 prime_to_square 循环,我不得不使用 maximum 为 * 100,000 * 的 Prime 列表来获得所需的结果。

int f() {
    // Determine the max & min numbers that can be made with 10 distinct digits and no leading zeroes
    long max_num = 9_876_543_210L;
    long min_num = 1_023_456_789;

    // Find the largest prime having a cube <= max_num
    long max_prime_to_cube = 2143;

    int count = 0;

    // Cube every prime up to 2143
    long cubed_val, squared_val, combined_val;
    String s;
    for (long prime_to_cube : new Prime(2143).list) {
        if (prime_to_cube > max_prime_to_cube) return count;
        cubed_val = prime_to_cube * prime_to_cube * prime_to_cube;

        // Try adding the square of every prime until we exceed the maximum valid number
        for(long prime_to_square : new Prime(100_000).list) {
            squared_val = prime_to_square * prime_to_square;
            combined_val = squared_val + cubed_val;
            if (combined_val < min_num) continue;
            if (combined_val > max_num) break;

            // Check if all digits are unique by converting to a set
            s = String.valueOf(combined_val);
            if (new HashSet<>(Arrays.asList(s.split(""))).size() == 10) {
                count += 1;
                System.out.printf(
                    "val: %s = %d^2 + %d^3, count:%d%n",
                    s, prime_to_square, prime_to_cube, count);
            }
        }
    }
    return count;
}


产出

val: 1328675409 = 36451^2 + 2^3, count:1
val: 1478325609 = 38449^2 + 2^3, count:2
val: 3085469217 = 55547^2 + 2^3, count:3
val: 3507126849 = 59221^2 + 2^3, count:4
...
val: 9682357410 = 5689^2 + 2129^3, count:1267
val: 9837162450 = 13681^2 + 2129^3, count:1268
val: 9814362750 = 523^2 + 2141^3, count:1269
val: 9815674302 = 1259^2 + 2141^3, count:1270
1270

相关问题