Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I make my implementation of Project Euler 25 faster, so I can actually compute the answer?
    primarykey
    data
    text
    <p>Here is my implementation of <a href="http://projecteuler.net/index.php?section=problems&amp;id=25" rel="nofollow">Problem 25 - Project Euler</a> (see comments in code for explanation of how it works):</p> <pre><code>#include &lt;iostream&gt; //Declare headers and use correct namespace #include &lt;math.h&gt; using namespace std; //Variables for the equation F_n(newTerm) = F_n-1(prevTerm) + Fn_2(currentTerm) unsigned long long newTerm = 0; unsigned long long prevTerm = 1; //F_1 initially = 1 unsigned long long currentTerm = 1; //F_2 initially = 2 unsigned long long termNo = 2; //Current number for the term void getNextTerms() { //Iterates through the Fib sequence, by changing the global variables. newTerm = prevTerm + currentTerm; //First run: newTerm = 2 unsigned long long temp = currentTerm; //temp = 1 currentTerm = newTerm; //currentTerm = 2 prevTerm = temp; //prevTerm = 1 termNo++; //termNo = 3 } unsigned long long getLength(unsigned long long number) //Returns the length of the number { unsigned long long length = 0; while (number &gt;= 1) { number = number / 10; length++; } return length; } int main (int argc, const char * argv[]) { while (true) { getNextTerms(); //Gets next term in the Fib sequence if (getLength(currentTerm) &lt; 1000) { //Checks if the next terms size is less than the desired length } else { //Otherwise if it is perfect print out the term. cout &lt;&lt; termNo; break; } } } </code></pre> <p>This works for the example, and will run quickly as long as this line:</p> <pre><code> if (getLength(currentTerm) &lt; 1000) { //Checks if the next term's size is less than the desired length </code></pre> <p>says 20 or lower instead of 1000. But if that number is greater than 20 it takes a forever, my patience gets the better of me and I stop the program, how can I make this algorithm more efficient?</p> <p>If you have any questions just ask in the comments. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload