Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8415943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T01:35:46+00:00 2026-06-10T01:35:46+00:00

Problem #3 on Project Euler is: The prime factors of 13195 are 5, 7,

  • 0

Problem #3 on Project Euler is:

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143?

My solution takes forever. I think I got the right implementation; however, when testing with the big number, I have not being able to see the results. It runs forever. I wonder if there’s something wrong with my algorithm:

public class LargestPrimeFactor3 {

    public static void main(String[] args) {
        long start, end, totalTime;
        long num = 600851475143L;
        long pFactor = 0;

        start = System.currentTimeMillis();

        for(int i = 2; i < num; i++) {
            if(isPrime(i)) {                
                if(num % i == 0) {
                    pFactor = i;                        
                }
            }
        }

        end = System.currentTimeMillis();
        totalTime = end - start;
        System.out.println(pFactor + " Time: "+totalTime);
    }

    static boolean isPrime(long n) {

        for(int i = 2; i < n; i++) {
            if(n % i == 0) {
                return false;
            }
        }        
        return true;
    }     
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T01:35:48+00:00Added an answer on June 10, 2026 at 1:35 am

    Although not in Java, I think you can probably make out the following. Basically, cutting down on the iterations by only testing odd divisors and up to the square root of a number is needed. Here is a brute force approach that gives an instant result in C#.

    static bool OddIsPrime (long oddvalue)  // test an odd >= 3 
    {
        // Only test odd divisors.
        for (long i = 3; i <= Math.Sqrt(oddvalue); i += 2)
        {
            if (value % i == 0)
                return false;
        }
        return true;
    }
    
    static void Main(string[] args)
    {
        long max = 600851475143;   // an odd value
        long maxFactor = 0;
    
        // Only test odd divisors of MAX. Limit search to Square Root of MAX.
        for (long i = 3; i <= Math.Sqrt(max); i += 2)
        {
            if (max % i == 0)
            {
                if (OddIsPrime(i))  // i is odd
                {
                    maxFactor = i;
                }
            }
        }
        Console.WriteLine(maxFactor.ToString());
        Console.ReadLine();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to solve the Project Euler problem 3 in Python: The prime factors
Possible Duplicate: Project Euler, Problem 10 java solution not working So, I'm attempting to
As stated in problem 37 at Project Euler : The number 3797 has an
I'm working on Project Euler problem number 205 which states: Peter has nine four-sided
Here is what I have so far: //project Eular Problem 4: Prime Factors #include<iostream>
I'm working on Project Euler Problem 14. Here's my solution. import Data.List collatzLength ::
I am attempting Problem 20 on Project Euler and this is the solution that
NOTE: This is a solution for Project Euler Problem 14 . If you still
I just solved the first problem from Project Euler in JavaFX for the fun
I solved Problem 10 of Project Euler with the following code, which works through

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.