Nice primes: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: added a foot header to the output.)
Line 291: Line 291:
33 nice primes found.
33 nice primes found.
</pre>
</pre>

=={{header|D}}==
{{trans|C++}}
<lang d>import std.stdio;

bool isPrime(uint n) {
if (n < 2) {
return false;
}
if (n % 2 == 0) {
return n == 2;
}
if (n % 3 == 0) {
return n == 3;
}
for (uint p = 5; p * p <= n; p += 4) {
if (n % p == 0) {
return false;
}
p += 2;
if (n % p == 0) {
return false;
}
}
return true;
}

uint digitalRoot(uint n) {
return n == 0 ? 0 : 1 + (n - 1) % 9;
}

void main() {
immutable from = 500;
immutable to = 1000;
writeln("Nice primes between ", from, " and ", to, ':');
uint count;
foreach (n; from .. to) {
if (isPrime(digitalRoot(n)) && isPrime(n)) {
count++;
write(n);
if (count % 10 == 0) {
writeln;
} else {
write(' ');
}
}
}
writeln;
writeln(count, " nice primes found.");
}</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|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
Line 302: Line 359:
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
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>
</pre>

=={{header|Factor}}==
=={{header|Factor}}==
Using the following formula to find the digital root of a base 10 number:
Using the following formula to find the digital root of a base 10 number: