Nice primes: Difference between revisions

1,615 bytes added ,  2 years ago
Line 758:
509 547 563 569 587 599 601 617 619 641 653 659 673 677 691 709 727 743 761 797 821 839 853 857 887 907 911 929 941 947 977 983 997
</pre>
 
=={{header|Java}}==
{{trans|Kotlin}}
<lang java>public class NicePrimes {
private static boolean isPrime(long n) {
if (n < 2) {
return false;
}
if (n % 2 == 0L) {
return n == 2L;
}
if (n % 3 == 0L) {
return n == 3L;
}
 
var p = 5L;
while (p * p <= n) {
if (n % p == 0L) {
return false;
}
p += 2;
if (n % p == 0L) {
return false;
}
p += 4;
}
return true;
}
 
private static long digitalRoot(long n) {
if (n == 0) {
return 0;
}
return 1 + (n - 1) % 9;
}
 
public static void main(String[] args) {
final long from = 500;
final long to = 1000;
int count = 0;
 
System.out.printf("Nice primes between %d and %d%n", from, to);
long n = from;
while (n < to) {
if (isPrime(digitalRoot(n)) && isPrime(n)) {
count++;
System.out.print(n);
if (count % 10 == 0) {
System.out.println();
} else {
System.out.print(' ');
}
}
 
n++;
}
System.out.println();
System.out.printf("%d nice primes found.%n", count);
}
}</lang>
{{out}}
<pre>Nice primes between 500 and 1000
509 547 563 569 587 599 601 617 619 641
653 659 673 677 691 709 727 743 761 797
821 839 853 857 887 907 911 929 941 947
977 983 997
33 nice primes found.</pre>
 
=={{header|Julia}}==
1,452

edits