Note that there are some explanatory texts on larger screens.

plurals
  1. POPrime generating number finder not producing correct output
    primarykey
    data
    text
    <p>I'm working on this problem:</p> <blockquote> <p>Consider the divisors of 30: 1,2,3,5,6,10,15,30.<br> It can be seen that for every divisor d of 30, d+30/d is prime.</p> <p>Find the sum of all positive integers n not exceeding 100 000 000 such that for every divisor d of n, d+n/d is prime.</p> </blockquote> <p>and I thought for sure I had it, but alas, it's apparently giving me the wrong answer (<code>12094504411074</code>).</p> <p>I am fairly sure my sieve of Eratosthenes is working (but maybe not), so I think the problem is somewhere in my algorithm. It seems to get the right answer for <code>n = 30</code> (<code>1+2+6+10+22+30 = 71</code> - is this correct?), but as numbers get larger, it apparently stops working.</p> <p>Here is my Java code:</p> <pre><code>import java.util.HashSet; public class Generators { static HashSet&lt;Integer&gt; hSet = new HashSet&lt;Integer&gt;(); public static void main(String[] args) { // TODO Auto-generated method stub int n = 100000000; sieveErat(n + 1); //Fill a hashSet with prime numbers System.out.println("Sieve complete"); int check = 0; long sum = 3; for(int i = 2; i &lt;= n; i++){ int numDivisors = 0; int numPrimeChecks = 0; boolean done = false; if(!hSet.contains(i+1)){ //i+1 must be a prime number for i to be prime generating continue; } else{ for(int j = 2; j &lt; i/2; j++){ if(i%j == 0){ numDivisors++; check = j + i/j; if(hSet.contains(check)){ done = true; numPrimeChecks++; } }else{ break; } } if(numPrimeChecks == numDivisors &amp;&amp; done){ sum += i; } } } System.out.println(sum); } public static void sieveErat(int N){ boolean[] isPrime = new boolean[N + 1]; for (int i = 2; i &lt;= N; i++) { isPrime[i] = true; //count++; } // mark non-primes &lt;= N using Sieve of Eratosthenes for (int i = 2; i*i &lt;= N; i++) { // if i is prime, then mark multiples of i as nonprime // suffices to consider mutiples i, i+1, ..., N/i if (isPrime[i]) { for (int j = i; i*j &lt;= N; j++) { isPrime[i*j] = false; // count--; } } } for(int i = 2; i &lt; isPrime.length; i++){ if(isPrime[i]){ hSet.add(i); } } // System.out.println(count); } } </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