Nice primes: Difference between revisions

1,307 bytes added ,  2 months ago
Task in PHP
(Adding python task)
(Task in PHP)
Line 1,451:
659 673 677 691 709 727 743 761 797 821 839
853 857 887 907 911 929 941 947 977 983 997
</pre>
 
=={{header|PHP}}==
{{trans|Python}}
<syntaxhighlight lang="php">
<?php
// Function to check if a number is prime
function isPrime($n) {
if ($n <= 1) {
return false;
}
for ($i = 2; $i <= sqrt($n); $i++) {
if ($n % $i == 0) {
return false;
}
}
return true;
}
 
// Function to sum the digits of a number until the sum is a single digit
function sumOfDigits($n) {
while ($n > 9) {
$sum = 0;
while ($n > 0) {
$sum += $n % 10;
$n = (int)($n / 10);
}
$n = $sum;
}
return $n;
}
 
function findNicePrimes($lower_limit=501, $upper_limit=1000) {
// Find all Nice primes within the specified range
$nice_primes = array();
for ($n = $lower_limit; $n < $upper_limit; $n++) {
if (isPrime($n)) {
$sumn = sumOfDigits($n);
if (isPrime($sumn)) {
array_push($nice_primes, $n);
}
}
}
return $nice_primes;
}
// Main loop to find and print "Nice Primes"
$nice_primes = findNicePrimes();
foreach ($nice_primes as $prime) {
echo $prime . " ";
}
?>
</syntaxhighlight>
{{out}}
<pre>
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>
 
Line 1,488 ⟶ 1,543:
[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|PL/0}}==
<syntaxhighlight lang="pascal">
3

edits