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 5985105
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:28:49+00:00 2026-05-22T22:28:49+00:00

How can I make this code run faster? public class ProjectEuler3 { public static

  • 0

How can I make this code run faster?

public class ProjectEuler3 {
    public static void main(String[] args) {
        System.out.println(findfactors(600851475143l));
    }

    public static long findfactors(long n) {
        long[] factors = new long[1000000];
        int nooffactor = 0;

        int c = 0;

        for (long i = 2; i < n; i++) {
            if (findPrime(i)) {
                factors[c++] = i;
                nooffactor++;
            }
        }

        return factors[nooffactor - 1];
    }

    public static boolean findPrime(long n) {
        for (long 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-05-22T22:28:49+00:00Added an answer on May 22, 2026 at 10:28 pm

    You could try something like this.

    private static final BitSet IS_PRIME = new BitSet(); static {
        IS_PRIME.set(2);
    }
    
    private static int IS_PRIME_LIMIT = 2;
    
    public static boolean isPrime(int n) {
        int p = IS_PRIME_LIMIT;
        while (p < n) {
            p++;
            IS_PRIME.set(p);
            if (p % 2 == 0) {
                IS_PRIME.clear(p);
            } else {
                for (int i = 3; i * i <= p; i += 2)
                    if (IS_PRIME.get(i) && p % i == 0) {
                        IS_PRIME.clear(p);
                        break;
                    }
            }
        }
        IS_PRIME_LIMIT = p;
        return IS_PRIME.get(n);
    }
    
    public static List<Integer> findfactors(long n) {
        List<Integer> ret = new ArrayList<Integer>();
        int sqrtN = (int) Math.sqrt(n);
    
        for (int i = 2; n > 1 && i <= sqrtN; i++) {
            if (isPrime(i)) {
                while (n > 1 && n % i == 0) {
                    n /= i;
                    ret.add(i);
                }
            }
        }
        if (n > 1)
            ret.add((int) n);
        return ret;
    }
    
    public static void main(String... args) {
        // warm up
        for(int i=0;i<10000;i++)
            findfactors(600851475143L);
    
        //  time one lookup.
        long start = System.nanoTime();
        List<Integer> factors = findfactors(600851475143L);
        long time = System.nanoTime() - start;
        System.out.println(factors);
        System.out.printf("Took %.3f ms to find factors%n", time/1e6);
    }
    

    prints

    [71, 839, 1471, 6857]
    Took 0.051 ms to find factors
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

You can change the connection string at run-time like this. You make the connection
How can I make this code run in parallel? List<Crop> crops = new List<Crop>();
How can I make this work? switch(property.PropertyType){ case typeof(Boolean): //doStuff break; case typeof(String): //doOtherStuff
How can I make this java generic cast ? public interface IField { }
I want to make this code run after 20 seconds with setTimeout: <script type=text/javascript>
Ok, let's see if I can make this make sense. I have a program
Now that I can make useful user controls in WPF (thanks to this stackoverflow
I can't for the life of me find a way to make this work.
How is it that tools like this one can make an ajax style call
I'm getting a InvalidOperationException when trying to run this code: database.Produkts.SingleOrDefault<Produkt>(x => x.Kod ==

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.