De Polignac numbers: Difference between revisions
Content added Content deleted
(Added Easylang) |
(New post.) |
||
Line 851: | Line 851: | ||
273421</syntaxhighlight> |
273421</syntaxhighlight> |
||
(We use 999 here for the 1000th number because 0 is the first J index.) |
(We use 999 here for the 1000th number because 0 is the first J index.) |
||
=={{header|Java}}== |
|||
<syntaxhighlight lang="java"> |
|||
public final class DePolignacNumbers { |
|||
public static void main(String[] args) { |
|||
System.out.println("The first 50 de Polignac numbers:"); |
|||
for ( int n = 1, count = 0; count < 10_000; n += 2 ) { |
|||
if ( isDePolignacNumber(n) ) { |
|||
count += 1; |
|||
if ( count <= 50 ) { |
|||
System.out.print(String.format("%5d%s", n, ( count % 10 == 0 ? "\n" : " ") )); |
|||
} else if ( count == 1_000 ) { |
|||
System.out.println(); |
|||
System.out.println("The 1,000th de Polignac number: " + n); |
|||
} else if ( count == 10_000 ) { |
|||
System.out.println(); |
|||
System.out.println("The 10,000th de Polignac number: " + n); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
private static boolean isDePolignacNumber(int number) { |
|||
for ( int p = 1; p < number; p <<= 1 ) { |
|||
if ( isPrime(number - p) ) { |
|||
return false; |
|||
} |
|||
} |
|||
return true; |
|||
} |
|||
private static boolean isPrime(int number) { |
|||
if ( number < 2 ) { |
|||
return false; |
|||
} |
|||
if ( number % 2 == 0 ) { |
|||
return number == 2; |
|||
} |
|||
if ( number % 3 == 0 ) { |
|||
return number == 3; |
|||
} |
|||
int delta = 2; |
|||
int k = 5; |
|||
while ( k * k <= number ) { |
|||
if ( number % k == 0 ) { |
|||
return false; |
|||
} |
|||
k += delta; |
|||
delta = 6 - delta; |
|||
} |
|||
return true; |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
{{ out }} |
|||
<pre> |
|||
The first 50 de Polignac numbers: |
|||
1 127 149 251 331 337 373 509 599 701 |
|||
757 809 877 905 907 959 977 997 1019 1087 |
|||
1199 1207 1211 1243 1259 1271 1477 1529 1541 1549 |
|||
1589 1597 1619 1649 1657 1719 1759 1777 1783 1807 |
|||
1829 1859 1867 1927 1969 1973 1985 2171 2203 2213 |
|||
The 1,000th de Polignac number: 31941 |
|||
The 10,000th de Polignac number: 273421 |
|||
</pre> |
|||
=={{header|jq}}== |
=={{header|jq}}== |