简体   繁体   中英

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.

 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.
The largest prime factor of the number 600851475143 is 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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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