Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is causing a segmentation fault?
    text
    copied!<p>I have been attempting to write a program that will determine if a number is prime or not. I have based it off of the Sieve of Eratosthenes. Anyway, my program works for small numbers (15485863 works), but if I use large numbers (ex. 17485863) I receive a segmentation fault. I am using unsigned long longs and do not think I have surpassed their maximum value. I just don't see what I have done wrong. Thank you in advance for any assistance!</p> <pre><code>#include &lt;iostream&gt; #include &lt;limits&gt; using namespace std; bool soe (unsigned long long); int main (void) { unsigned long long x = 17485863; bool q = soe(x); cout &lt;&lt; x &lt;&lt; " is "; if(q) cout &lt;&lt; "prime." &lt;&lt; endl; else cout &lt;&lt; "not prime." &lt;&lt; endl; return 0; } bool soe(unsigned long long input) { unsigned long long arrayLength = input%2 + input/2; unsigned long long index = 2; unsigned long long greatestMult = 0; bool array[arrayLength]; array[0] = true; //ignore true values in the array array[1] = true; do{ array[index] = false; }while(++index &lt; arrayLength); index = 2; do { if(input%index != 0) { greatestMult = input/index; while(index*greatestMult &gt; arrayLength) greatestMult--; do { array[index*greatestMult] = true; }while(--greatestMult &gt; 0); do { if(!array[index]) break; }while(++index &lt; arrayLength); } else { cout &lt;&lt; endl &lt;&lt; input &lt;&lt; " is divisble by " &lt;&lt; index &lt;&lt; endl; return false; } }while(index &lt; arrayLength); return true; } </code></pre>
 

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