Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Euler-Problem 14 Program Freezing
    primarykey
    data
    text
    <p>I'm working on Euler Problem 14: <a href="http://projecteuler.net/index.php?section=problems&amp;id=14" rel="nofollow noreferrer">http://projecteuler.net/index.php?section=problems&amp;id=14</a></p> <p>I figured the best way would be to create a vector of numbers that kept track of how big the series was for that number... for example from 5 there are 6 steps to 1, so if ever reach the number 5 in a series, I know I have 6 steps to go and I have no need to calculate those steps. With this idea I coded up the following:</p> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; #include &lt;iomanip&gt; using namespace std; int main() { vector&lt;int&gt; sizes(1); sizes.push_back(1); sizes.push_back(2); int series, largest = 0, j; for (int i = 3; i &lt;= 1000000; i++) { series = 0; j = i; while (j &gt; (sizes.size()-1)) { if (j%2) { j=(3*j+1)/2; series+=2; } else { j=j/2; series++; } } series+=sizes[j]; sizes.push_back(series); if (series&gt;largest) largest=series; cout &lt;&lt; setw(7) &lt;&lt; right &lt;&lt; i &lt;&lt; "::" &lt;&lt; setw(5) &lt;&lt; right &lt;&lt; series &lt;&lt; endl; } cout &lt;&lt; largest &lt;&lt; endl; return 0; } </code></pre> <p>It seems to work relatively well for smaller numbers but this specific program stalls at the number 113382. Can anyone explain to me how I would go about figuring out why it freezes at this number? </p> <p>Is there some way I could modify my algorithim to be better? I realize that I am creating duplicates with the current way I'm doing it: for example, the series of 3 is 3,10,5,16,8,4,2,1. So I already figured out the sizes for 10,5,16,8,4,2,1 but I will duplicate those solutions later.</p> <p>Thanks for your help!</p>
    singulars
    1. This table or related slice is empty.
    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