Next special primes: Difference between revisions

Added Java
(Added C)
(Added Java)
Line 113:
fmt.Printf("%6d %6d %3d\n", lastSpecial, i, lastGap)
lastSpecial = i
}
}
}</lang>
 
{{out}}
<pre>
Special primes under 1,050:
Prime1 Prime2 Gap
2 3 1
3 5 2
5 11 6
11 19 8
19 29 10
29 41 12
41 59 18
59 79 20
79 101 22
101 127 26
127 157 30
157 191 34
191 227 36
227 269 42
269 313 44
313 359 46
359 409 50
409 461 52
461 521 60
521 587 66
587 659 72
659 733 74
733 809 76
809 887 78
887 967 80
967 1049 82
</pre>
 
=={{header|Java}}==
{{trans|C}}
<lang java>class SpecialPrimes {
private static boolean isPrime(int n) {
if (n < 2) return false;
if (n%2 == 0) return n == 2;
if (n%3 == 0) return n == 3;
int d = 5;
while (d*d <= n) {
if (n%d == 0) return false;
d += 2;
if (n%d == 0) return false;
d += 4;
}
return true;
}
 
public static void main(String[] args) {
System.out.println("Special primes under 1,050:");
System.out.println("Prime1 Prime2 Gap");
int lastSpecial = 3;
int lastGap = 1;
System.out.printf("%6d %6d %3d\n", 2, 3, lastGap);
for (int i = 5; i < 1050; i += 2) {
if (isPrime(i) && (i-lastSpecial) > lastGap) {
lastGap = i - lastSpecial;
System.out.printf("%6d %6d %3d\n", lastSpecial, i, lastGap);
lastSpecial = i;
}
}
}
9,476

edits