素数在Java中 - 算法素数、算法、Java

2023-09-11 02:33:05 作者:放过你,不可能。

我已经开始学习code在Java和决定我会使用项目欧拉网站给我小任务,尝试和完成新的编码的每一位我学习。所以,我碰到问题3 :

I have started learning to code in Java and decided I would use the Project Euler site to give me little tasks to try and complete with each bit of new coding I learn. So I came across Problem 3:

对13195的首要因素是5,7,13和29。 什么是数字600851475143的最大素因子?

The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ?

我想过这个问题,并研究了许多不同的理论,关于素数,以及他们如何可以通过各种不同的计算发现是从2测试号码 - >ñ看看他们是一个素数,如果是,那么我会分裂Tn的变量(在这种情况下600851475143)由新发现的素数,看看它是否是一个因素。如果是,我会把它赋值给变量马力(最高质数),并在节目我会输出马力到控制台给我结果的末尾。

I thought about the problem and researched many different theories about prime numbers and how they can be found via various different calculations (Sieve of Eratosthenes being an example) and the solution I conjured up was to test numbers from 2 --> n and see if they were a prime number, if they were then I would divide the Tn variable (in this case 600851475143) by the newly discovered prime number and see if it was a factor. If it was, I would assign it to the variable Hp (Highest Prime number) and at the end of the program I would output Hp to the console to give my result.

下面是我的code:

public class Largest_Prime_Factor_NEW_SOLUTION {

    static long Tn = 600851475143L;
    static long Hp = 0;
    static boolean isPrime = false;

    public static void main(String[] args) {

        for (long i=2; i<Tn; i++) {
            System.out.println("TESTING NUMBER " + i);
            for (long k=2; k < i; k++) {
                if (i % k == 0) {
                    System.out.println(i + " IS NOT A PRIME");
                    break;
                } else if (k + 1 == i) {
                    isPrime = true;
                }
            }

            if (isPrime) {
            System.out.println(i + " IS A PRIME");
            if (Tn % i == 0) {
                System.out.println(Tn + " IS DIVISIBLE BY " + i);
                Hp = i;
            } else {
                System.out.println(Tn + " IS NOT DIVISIBLE BY " + i);
            }
            }

            isPrime = false;
        }
        System.out.println("THE HIGHEST PRIME NUMBER OF " + Tn + " IS " + Hp);
    }
}

现在我知道,这code是非常低效和刚开始我已成功地从我开始(有循环无处不在!)凝结,但什么,我是问的是,我怎么能提高呢?它蚕食我,因为我的一切研究违背别人会怎么做,这是非常令人困惑。我曾尝试筛方法,但据我所知,布尔数组只能是一个int数组,从来没有一个多头排列?

Now I know that this code is very inefficient and for just starting I have managed to condense it from where I began (there were loops everywhere!) but what I am is asking is, how can I improve this? It's eating away at me because everything I research contradicts what others would do and it's very confusing. I have tried the sieve method but i understand that a boolean array can only be an int array and never a long array?

据我所知,开始code,当我将被限制到什么知识,我可以使用,但只是出于兴趣,我很希望看到最终的解决办法是。

I understand that when beginning to code I will be limited to what knowledge I can use, but just out of interest, I am keen to see what the final solution would be.

推荐答案

你可以做的就是找​​到 Tn的最低的约数。假如是 P ,再次找到最低的除数 TN / P> / code>等等。

What you can do is find the lowest divisor of Tn. Suppose that is p, find the lowest divisor again for Tn/p and so on.

现在,每走一步 P 为素数[以下说明。因此,收集他们,他们是 Tn的的素因子。

Now, at every step p is prime[explanation below]. So collect them and they are the prime divisors of Tn.

有关更好的时间复杂度,可以以检查约数高达 CEIL(开方(TN))只,而不是 TN-I

For better time-complexity, you can check for divisors up to upto ceil(sqrt(Tn)) only, instead of Tn-1.

而当你开始检查素数为 Tn的,你可以用 2 启动。而一旦你得到一个素因子 P 不要再从 2 开始的 TN / P 。因为, TN / P> / code>也是 Tn的的除数,自 Tn的没有除数小于 P TN / P> / code>不具备这一点。因此,开始与 P 再次[ P 可以有多个功率 Tn的。如果 P 不分 Tn的,移动到 P + 1

And when you start checking for prime divisor for Tn, you can start with 2. And once you get a prime divisor p don't start again from 2 for Tn/p. Because, Tn/p is also a divisor of Tn and since Tn does not have divisors less than p, Tn/p does not have it too. So start with p again[p can have multiple power in Tn]. If p does not divide Tn, move to p+1.

例如:

Tn的= 45 1.先从2.2不把45 2.下一个考验是3。45整除3 SO 3是它的一个素因子。 3.现在检查素因子从45/3 = 15,但重新开始与3不2。 4.好了,15是被3整除所以先从15/3 = 5 5.注意事项5,CEIL(开方(5))为3。5,但不能整除3。但是,由于4> CEIL(开方(5)) 我们可以说5是毫无疑问的黄金。

Tn = 45 1. start with 2. 2 does not divides 45. 2. next test is for 3. 45 is divisible by 3. So 3 is a prime divisor of it. 3. Now check prime divisors from 45/3 = 15, but start with 3. not from 2 again. 4. Well, 15 is divisible by 3. So start with 15/3 = 5 5. Note for 5, ceil(sqrt(5)) is 3. But 5 is not divisible by 3. But since 4 > ceil(sqrt(5)) and we can say 5 is a prime without any doubt.

所以45的主要因子为3和5。

So the prime divisor of 45 are 3 and 5.

的一个数为什么最小约数(除1)是素?

Why smallest divisor(except 1) of a number is a prime ?

假设上面的说法是错误的。然后,数N有一个最小的尚未复合因子,例如c。

Suppose above statement is false. Then a number N has a smallest yet composite divisor, say C.

所以C | N 现在C是复合所以,它具有除数比本身,但大于1。 例如c这样的除数是P. 因此P | C,但我们有C | N =>点| N,其中1&LT; P&LT; C.

So C|N Now C is composite so, it has divisor less than itself but greater than one. Say such a divisor of C is P. So P|C , but we have C|N => P|N where 1 < P < C.

这与我们的假设,即C是N的最小公约数,所以一些最小公约数始终是一个素数。

This contradicts our assumption that C is the smallest divisor of N, so smallest divisors of a number is always a prime.