简体   繁体   English

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?

My program is not working on eclipse.. And i dont know whats wrong with it.. Plz help.. And plz tell the reason as well.我的程序不能在 eclipse 上运行.. 我不知道它有什么问题.. 请帮忙.. 并请告诉原因。

 public class ProblemThree {

        public static void main(String args[])
        {
            long a=0L, z=0L;
            long n=600851475143L;

            for(long i=2;i<=n ;++i)
            {
                if(600851475143L % i==0)
                {
                    a=i;

                    if(a%2==0)
                        {;  }
                    else if(a%3==0)
                        { ;}
                    else if(a%5==0)
                        { ;}
                    else if(a%7==0)
                        { ;}
                    else if (a>z)
                    { 
                        z=a;
                    }

                }

            }

            System.out.println(z);

        }
    }

Thank you guys for your feedback but i have solved this question myself with the following code.. :)谢谢你们的反馈,但我已经用下面的代码自己解决了这个问题..:)

public class ProblemThree {公共类问题三{

public static void main(String args[])
{

    long n=600851475143L;

    for(long i=2;i<n ;++i)
    {
        while(n % i==0)
        {//for yes
            n=n/i;

        }   

    }
    System.out.println(n);
}

} }

Your program isn't "returning" anything because it is still running.您的程序没有“返回”任何东西,因为它仍在运行。 Your loop is still iterated.您的循环仍在迭代。 Your code needs to be modified to be more performant.您的代码需要修改以提高性能。 Here is my solution to the same problem.这是我对同一问题的解决方案。

long testNum = 600851475143l; 
int largestFactor = 0; 
long loopMax = 17425170l; //largest known prime  
for (int i = 3; i * i <= loopMax; i++) { 
    boolean isPrime = true;             
    for (int j = 2; j < i; j++) { 
       if (i % j == 0) { 
          isPrime = false; 
          break; 
       } 
    }                         
    if (isPrime && testNum % i == 0) { 
        System.out.println("prime factor: " + i); 
        largestFactor = i; 
        loopMax = (testNum / i) + 1; 
    } 
} 
System.out.println("result is: " + largestFactor);
package Euler;

import java.util.Scanner;

public class euler3 {
    public static void main(String args[])
    {
        long num;
        Scanner sc = new Scanner(System.in);
        num= sc.nextLong();

        for(int i=2;i<num; i++)
        {
            while(num%i == 0)
            {
                //System.out.println(i);
                num=num/i;
            }
        }
        if(num>2)
            System.out.println(num);

    }

}    

The prime factors of 13195 are 5, 7, 13 and 29. 13195 的质因数是 5、7、13 和 29。
The largest prime factor of the number 600851475143 is 6857.数 600851475143 的最大质因数是 6857。

#include <bits/stdc++.h>
using namespace std;
int SieveOfEratosthenes(int x,long long signed int n)
{
    bool prime[x];                                 //use sieve upto square root of 
                                                       // required number
    memset(prime, true, sizeof(prime));
 
    for (long long signed int p = 2; p * p <= x; p++)    
    {
        if (prime[p] == true) 
        {
            for (long long signed int i = p * p; i <= x; i += p)
                prime[i] = false;
        }
    }
    int res;
    for (long long signed int p = 2; p <= x; p++)
        if (prime[p])
            {
                if(n%p==0)   //if number is divisble by prime number than update with 
                res=p;        //latest value of prime number
            }
            return res;
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout<<SieveOfEratosthenes(775146,600851475143);//Return the answer

    // long long signed int n;cin>>n;   for value of n
   //cout<<SieveOfEratosthenes(sqrt(n),n);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM