A faster way to compute the largest prime factor Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Project Euler #3 - largest prime factorFaster way to determine largest prime factorx64 Assembly - checking for largest prime factorProject Euler #3 (Largest prime factor) in SwiftProject Euler 3: Getting the largest prime factor of a numberVery slow Project Euler Q3 (largest prime factor of a large number)Project Euler 3: Largest prime factorLargest prime factor of a given numberProject Euler 3 - Largest prime factorEuler problem 3: largest prime factor of the numberWork out largest prime factor of a number

PIC mathematical operations weird problem

Second order approximation of the loss function (Deep learning book, 7.33)

Mistake in years of experience in resume?

Where did Arya get these scars?

I preordered a game on my Xbox while on the home screen of my friend's account. Which of us owns the game?

My bank got bought out, am I now going to have to start filing tax returns in a different state?

A faster way to compute the largest prime factor

"Whatever a Russian does, they end up making the Kalashnikov gun"? Are there any similar proverbs in English?

What's parked in Mil Moscow helicopter plant?

How would I use different systems of magic when they are capable of the same effects?

How to avoid introduction cliches

Why didn't the Space Shuttle bounce back into space as many times as possible so as to lose a lot of kinetic energy up there?

Why does the Cisco show run command not show the full version, while the show version command does?

A Paper Record is What I Hamper

What to do with someone that cheated their way through university and a PhD program?

A strange hotel

How to translate "red flag" into Spanish?

What *exactly* is electrical current, voltage, and resistance?

Can you stand up from being prone using Skirmisher outside of your turn?

Passing args from the bash script to the function in the script

Putting Ant-Man on house arrest

Are there moral objections to a life motivated purely by money? How to sway a person from this lifestyle?

What do you call the part of a novel that is not dialog?

How to get even lighting when using flash for group photos near wall?



A faster way to compute the largest prime factor



Announcing the arrival of Valued Associate #679: Cesar Manara
Unicorn Meta Zoo #1: Why another podcast?Project Euler #3 - largest prime factorFaster way to determine largest prime factorx64 Assembly - checking for largest prime factorProject Euler #3 (Largest prime factor) in SwiftProject Euler 3: Getting the largest prime factor of a numberVery slow Project Euler Q3 (largest prime factor of a large number)Project Euler 3: Largest prime factorLargest prime factor of a given numberProject Euler 3 - Largest prime factorEuler problem 3: largest prime factor of the numberWork out largest prime factor of a number



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1












$begingroup$


I am self-learning js and came across this problem(#3) from the Euler Project




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



What is the largest prime factor of the number 600851475143 ?




Logic:



  • Have an array primes to store all the prime numbers less than number


  • Loop through the odd numbers only below number to check for primes using i



  • Check if i is divisible by any of the elements already in primes.



    • If yes, isPrime = false and break the for loop for j by j=primesLength

    • If not, isPrime = true



  • If isPrime == true then add i to the array primes and check if number%i == 0



    • If number%i == 0% update the value of factor as factor = i


  • Return factor after looping through all the numbers below number


My code:






function problem3(number)
let factor = 1;
let primes = [2]; //array to store prime numbers

for(let i=3; i<number; i=i+2) //Increment i by 2 to loop through only odd numbers
let isPrime = true;
let primesLength= primes.length;

for(let j=0; j< primesLength; j++)
if(i%primes[j]==0)
isPrime = false;
j=primesLength; //to break the for loop



if(isPrime == true)
primes.push(i);
if(number%i == 0)
factor = i;



return factor;


console.log(problem3(600851475143));





It is working perfectly for small numbers, but is quite very slow for 600851475143. What should I change in this code to make the computation faster?










share|improve this question









New contributor




Eagle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$


















    1












    $begingroup$


    I am self-learning js and came across this problem(#3) from the Euler Project




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



    What is the largest prime factor of the number 600851475143 ?




    Logic:



    • Have an array primes to store all the prime numbers less than number


    • Loop through the odd numbers only below number to check for primes using i



    • Check if i is divisible by any of the elements already in primes.



      • If yes, isPrime = false and break the for loop for j by j=primesLength

      • If not, isPrime = true



    • If isPrime == true then add i to the array primes and check if number%i == 0



      • If number%i == 0% update the value of factor as factor = i


    • Return factor after looping through all the numbers below number


    My code:






    function problem3(number)
    let factor = 1;
    let primes = [2]; //array to store prime numbers

    for(let i=3; i<number; i=i+2) //Increment i by 2 to loop through only odd numbers
    let isPrime = true;
    let primesLength= primes.length;

    for(let j=0; j< primesLength; j++)
    if(i%primes[j]==0)
    isPrime = false;
    j=primesLength; //to break the for loop



    if(isPrime == true)
    primes.push(i);
    if(number%i == 0)
    factor = i;



    return factor;


    console.log(problem3(600851475143));





    It is working perfectly for small numbers, but is quite very slow for 600851475143. What should I change in this code to make the computation faster?










    share|improve this question









    New contributor




    Eagle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$














      1












      1








      1





      $begingroup$


      I am self-learning js and came across this problem(#3) from the Euler Project




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



      What is the largest prime factor of the number 600851475143 ?




      Logic:



      • Have an array primes to store all the prime numbers less than number


      • Loop through the odd numbers only below number to check for primes using i



      • Check if i is divisible by any of the elements already in primes.



        • If yes, isPrime = false and break the for loop for j by j=primesLength

        • If not, isPrime = true



      • If isPrime == true then add i to the array primes and check if number%i == 0



        • If number%i == 0% update the value of factor as factor = i


      • Return factor after looping through all the numbers below number


      My code:






      function problem3(number)
      let factor = 1;
      let primes = [2]; //array to store prime numbers

      for(let i=3; i<number; i=i+2) //Increment i by 2 to loop through only odd numbers
      let isPrime = true;
      let primesLength= primes.length;

      for(let j=0; j< primesLength; j++)
      if(i%primes[j]==0)
      isPrime = false;
      j=primesLength; //to break the for loop



      if(isPrime == true)
      primes.push(i);
      if(number%i == 0)
      factor = i;



      return factor;


      console.log(problem3(600851475143));





      It is working perfectly for small numbers, but is quite very slow for 600851475143. What should I change in this code to make the computation faster?










      share|improve this question









      New contributor




      Eagle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      I am self-learning js and came across this problem(#3) from the Euler Project




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



      What is the largest prime factor of the number 600851475143 ?




      Logic:



      • Have an array primes to store all the prime numbers less than number


      • Loop through the odd numbers only below number to check for primes using i



      • Check if i is divisible by any of the elements already in primes.



        • If yes, isPrime = false and break the for loop for j by j=primesLength

        • If not, isPrime = true



      • If isPrime == true then add i to the array primes and check if number%i == 0



        • If number%i == 0% update the value of factor as factor = i


      • Return factor after looping through all the numbers below number


      My code:






      function problem3(number)
      let factor = 1;
      let primes = [2]; //array to store prime numbers

      for(let i=3; i<number; i=i+2) //Increment i by 2 to loop through only odd numbers
      let isPrime = true;
      let primesLength= primes.length;

      for(let j=0; j< primesLength; j++)
      if(i%primes[j]==0)
      isPrime = false;
      j=primesLength; //to break the for loop



      if(isPrime == true)
      primes.push(i);
      if(number%i == 0)
      factor = i;



      return factor;


      console.log(problem3(600851475143));





      It is working perfectly for small numbers, but is quite very slow for 600851475143. What should I change in this code to make the computation faster?






      function problem3(number)
      let factor = 1;
      let primes = [2]; //array to store prime numbers

      for(let i=3; i<number; i=i+2) //Increment i by 2 to loop through only odd numbers
      let isPrime = true;
      let primesLength= primes.length;

      for(let j=0; j< primesLength; j++)
      if(i%primes[j]==0)
      isPrime = false;
      j=primesLength; //to break the for loop



      if(isPrime == true)
      primes.push(i);
      if(number%i == 0)
      factor = i;



      return factor;


      console.log(problem3(600851475143));





      function problem3(number)
      let factor = 1;
      let primes = [2]; //array to store prime numbers

      for(let i=3; i<number; i=i+2) //Increment i by 2 to loop through only odd numbers
      let isPrime = true;
      let primesLength= primes.length;

      for(let j=0; j< primesLength; j++)
      if(i%primes[j]==0)
      isPrime = false;
      j=primesLength; //to break the for loop



      if(isPrime == true)
      primes.push(i);
      if(number%i == 0)
      factor = i;



      return factor;


      console.log(problem3(600851475143));






      javascript beginner programming-challenge time-limit-exceeded primes






      share|improve this question









      New contributor




      Eagle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Eagle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 1 hour ago









      200_success

      131k17157422




      131k17157422






      New contributor




      Eagle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 2 hours ago









      EagleEagle

      1085




      1085




      New contributor




      Eagle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Eagle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Eagle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          2 Answers
          2






          active

          oldest

          votes


















          3












          $begingroup$

          There are many questions about Project Euler 3 on this site already. The trick is to pick an algorithm that…



          • Reduces n whenever you find a factor, so that you don't need to consider factors anywhere near as large as 600851475143

          • Only finds prime factors, and never composite factors, so that you never need to explicitly test for primality.

          Your algorithm suffers on both criteria: the outer for loop goes all the way up to 600851475143 (which is insane), and you're testing each of those numbers for primality (which is incredibly computationally expensive).






          share|improve this answer









          $endgroup$




















            1












            $begingroup$

            The first problem is that you are trying to find all prime numbers under number. The number of prime numbers under x is approximately x/ln(x) which is around 22153972243.4 for our specific value of x



            This is way too big ! So even if you where capable of obtaining each of these prime numbers in constant time it would take too much time.



            This tells us this approach is most likely unfixable.






            share|improve this answer








            New contributor




            Jorge Fernández is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            $endgroup$













              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "196"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );






              Eagle is a new contributor. Be nice, and check out our Code of Conduct.









              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f219063%2fa-faster-way-to-compute-the-largest-prime-factor%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              3












              $begingroup$

              There are many questions about Project Euler 3 on this site already. The trick is to pick an algorithm that…



              • Reduces n whenever you find a factor, so that you don't need to consider factors anywhere near as large as 600851475143

              • Only finds prime factors, and never composite factors, so that you never need to explicitly test for primality.

              Your algorithm suffers on both criteria: the outer for loop goes all the way up to 600851475143 (which is insane), and you're testing each of those numbers for primality (which is incredibly computationally expensive).






              share|improve this answer









              $endgroup$

















                3












                $begingroup$

                There are many questions about Project Euler 3 on this site already. The trick is to pick an algorithm that…



                • Reduces n whenever you find a factor, so that you don't need to consider factors anywhere near as large as 600851475143

                • Only finds prime factors, and never composite factors, so that you never need to explicitly test for primality.

                Your algorithm suffers on both criteria: the outer for loop goes all the way up to 600851475143 (which is insane), and you're testing each of those numbers for primality (which is incredibly computationally expensive).






                share|improve this answer









                $endgroup$















                  3












                  3








                  3





                  $begingroup$

                  There are many questions about Project Euler 3 on this site already. The trick is to pick an algorithm that…



                  • Reduces n whenever you find a factor, so that you don't need to consider factors anywhere near as large as 600851475143

                  • Only finds prime factors, and never composite factors, so that you never need to explicitly test for primality.

                  Your algorithm suffers on both criteria: the outer for loop goes all the way up to 600851475143 (which is insane), and you're testing each of those numbers for primality (which is incredibly computationally expensive).






                  share|improve this answer









                  $endgroup$



                  There are many questions about Project Euler 3 on this site already. The trick is to pick an algorithm that…



                  • Reduces n whenever you find a factor, so that you don't need to consider factors anywhere near as large as 600851475143

                  • Only finds prime factors, and never composite factors, so that you never need to explicitly test for primality.

                  Your algorithm suffers on both criteria: the outer for loop goes all the way up to 600851475143 (which is insane), and you're testing each of those numbers for primality (which is incredibly computationally expensive).







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 1 hour ago









                  200_success200_success

                  131k17157422




                  131k17157422























                      1












                      $begingroup$

                      The first problem is that you are trying to find all prime numbers under number. The number of prime numbers under x is approximately x/ln(x) which is around 22153972243.4 for our specific value of x



                      This is way too big ! So even if you where capable of obtaining each of these prime numbers in constant time it would take too much time.



                      This tells us this approach is most likely unfixable.






                      share|improve this answer








                      New contributor




                      Jorge Fernández is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.






                      $endgroup$

















                        1












                        $begingroup$

                        The first problem is that you are trying to find all prime numbers under number. The number of prime numbers under x is approximately x/ln(x) which is around 22153972243.4 for our specific value of x



                        This is way too big ! So even if you where capable of obtaining each of these prime numbers in constant time it would take too much time.



                        This tells us this approach is most likely unfixable.






                        share|improve this answer








                        New contributor




                        Jorge Fernández is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.






                        $endgroup$















                          1












                          1








                          1





                          $begingroup$

                          The first problem is that you are trying to find all prime numbers under number. The number of prime numbers under x is approximately x/ln(x) which is around 22153972243.4 for our specific value of x



                          This is way too big ! So even if you where capable of obtaining each of these prime numbers in constant time it would take too much time.



                          This tells us this approach is most likely unfixable.






                          share|improve this answer








                          New contributor




                          Jorge Fernández is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






                          $endgroup$



                          The first problem is that you are trying to find all prime numbers under number. The number of prime numbers under x is approximately x/ln(x) which is around 22153972243.4 for our specific value of x



                          This is way too big ! So even if you where capable of obtaining each of these prime numbers in constant time it would take too much time.



                          This tells us this approach is most likely unfixable.







                          share|improve this answer








                          New contributor




                          Jorge Fernández is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          share|improve this answer



                          share|improve this answer






                          New contributor




                          Jorge Fernández is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          answered 53 mins ago









                          Jorge FernándezJorge Fernández

                          1645




                          1645




                          New contributor




                          Jorge Fernández is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.





                          New contributor





                          Jorge Fernández is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






                          Jorge Fernández is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.




















                              Eagle is a new contributor. Be nice, and check out our Code of Conduct.









                              draft saved

                              draft discarded


















                              Eagle is a new contributor. Be nice, and check out our Code of Conduct.












                              Eagle is a new contributor. Be nice, and check out our Code of Conduct.











                              Eagle is a new contributor. Be nice, and check out our Code of Conduct.














                              Thanks for contributing an answer to Code Review Stack Exchange!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              Use MathJax to format equations. MathJax reference.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f219063%2fa-faster-way-to-compute-the-largest-prime-factor%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Are there any AGPL-style licences that require source code modifications to be public? Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Force derivative works to be publicAre there any GPL like licenses for Apple App Store?Do you violate the GPL if you provide source code that cannot be compiled?GPL - is it distribution to use libraries in an appliance loaned to customers?Distributing App for free which uses GPL'ed codeModifications of server software under GPL, with web/CLI interfaceDoes using an AGPLv3-licensed library prevent me from dual-licensing my own source code?Can I publish only select code under GPLv3 from a private project?Is there published precedent regarding the scope of covered work that uses AGPL software?If MIT licensed code links to GPL licensed code what should be the license of the resulting binary program?If I use a public API endpoint that has its source code licensed under AGPL in my app, do I need to disclose my source?

                              2013 GY136 Descoberta | Órbita | Referências Menu de navegação«List Of Centaurs and Scattered-Disk Objects»«List of Known Trans-Neptunian Objects»

                              Button changing it's text & action. Good or terrible? The 2019 Stack Overflow Developer Survey Results Are Inchanging text on user mouseoverShould certain functions be “hard to find” for powerusers to discover?Custom liking function - do I need user login?Using different checkbox style for different checkbox behaviorBest Practices: Save and Exit in Software UIInteraction with remote validated formMore efficient UI to progress the user through a complicated process?Designing a popup notice for a gameShould bulk-editing functions be hidden until a table row is selected, or is there a better solution?Is it bad practice to disable (replace) the context menu?