Note that there are some explanatory texts on larger screens.

plurals
  1. POProject Euler challenge 3: Finding the largest prime factor of a large number
    primarykey
    data
    text
    <p>Can't find the prime factor of 600851475143 for projecteuler. My code successfully computes the largest prime factor of the test number 13195 and every test number I throw at it, but somehow it degrades with the large prime number. Do you know why?</p> <pre><code>#include &lt;iostream&gt; #include &lt;queue&gt; using namespace std; int split(int split); int largestprimefactor(priority_queue&lt;int&gt; myints); int main() { int response = 2; do{ priority_queue&lt;int&gt; myints; int number; cout &lt;&lt; "Please enter a number: "; cin &gt;&gt; number; myints.push(number); int lcf = largestprimefactor(myints); cout &lt;&lt; endl &lt;&lt; "Largest prime factor is: " &lt;&lt; lcf; cout &lt;&lt; endl &lt;&lt; "Again?(1 for yes 2 for no): "; cin &gt;&gt; response; }while(response == 1); } uint64_t split(uint64_t split) { if(split%2 != 0) { if((split/2))%2 == 0) for(uint64_t i = (split/2)-1; i&gt;1; i=i-2) if(split%i == 0) return i; else for(uint64_t i = (split/2); i&gt;1; i=i-2) if(split%i == 0) return i; return 1; } else return 2; } int largestprimefactor(priority_queue&lt;int&gt; myints) { // largestfactor holds the next number to be tested for primeness in the queue do{ int largestfactor = myints.top(); myints.pop(); //splat will hold the first factor split finds of the top item in the queue int splat = split(largestfactor); //if it holds a 1 then that means that there are no factors if(splat != 1 &amp;&amp; largestfactor) { myints.push(splat); myints.push(largestfactor / splat); } else return largestfactor; }while(myints.top() &gt; 1); } </code></pre>
    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