Nice primes: Difference between revisions

3,288 bytes added ,  20 days ago
Added Easylang
(Added Easylang)
 
(4 intermediate revisions by 3 users not shown)
Line 729:
</pre>
 
 
=={{header|EasyLang}}==
{{trans|11l}}
<syntaxhighlight>
fastfunc isprim num .
if num < 2
return 0
.
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
func digroot n .
return 1 + (n - 1) mod 9
.
for n = 501 to 999
if isprim digroot n = 1 and isprim n = 1
write n & " "
.
.
</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>
 
=={{header|F_Sharp|F#}}==
Line 1,453 ⟶ 1,483:
</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>
 
=={{header|Python}}==
<syntaxhighlight lang="python">
def is_prime(n):
"""Check if a number is prime."""
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
 
def sum_of_digits(n):
"""Calculate the repeated sum of digits until the sum's length is 1."""
while n > 9:
n = sum(int(digit) for digit in str(n))
return n
 
def find_nice_primes(lower_limit=501, upper_limit=1000):
"""Find all Nice primes within the specified range."""
nice_primes = []
for n in range(lower_limit, upper_limit):
if is_prime(n):
sumn = sum_of_digits(n)
if is_prime(sumn):
nice_primes.append(n)
return nice_primes
 
# Example usage
nice_primes = find_nice_primes()
print(nice_primes)
</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>
=={{header|PL/0}}==
<syntaxhighlight lang="pascal">
Line 1,763 ⟶ 1,883:
33: 997 > Σ = 7
done...
</pre>
 
=={{header|RPL}}==
≪ { } 500
'''DO'''
NEXTPRIME
'''IF''' DUP 1 - 9 MOD 1 + ISPRIME? '''THEN'''
SWAP OVER + SWAP '''END'''
'''UNTIL''' DUP 1000 ≥ '''END'''
DROP
≫ '<span style="color:blue">TASK</span>' STO
{{out}}
<pre>
1: { 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,895 ⟶ 2,029:
{{libheader|Wren-iterate}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./math" for Int
import "./iterate" for Stepped
import "./fmt" for Fmt
 
var sumDigits = Fn.new { |n|
2,041

edits