Palindromic primes: Difference between revisions
Content added Content deleted
m (→{{header|Wren}}: Minor tidy) |
(Added S-BASIC example) |
||
Line 1,210: | Line 1,210: | ||
<pre>[2, 3, 5, 7, 11, 101, 131, 151, 181, 191, 313, 353, 373, 383, 727, 757, 787, 797, 919, 929] |
<pre>[2, 3, 5, 7, 11, 101, 131, 151, 181, 191, 313, 353, 373, 383, 727, 757, 787, 797, 919, 929] |
||
</pre> |
</pre> |
||
=={{header|S-BASIC}}== |
|||
<syntaxhighlight lang="BASIC"> |
|||
$constant FALSE = 0 |
|||
$constant TRUE = 0FFFFH |
|||
rem - return true if n is palindromic, otherwise false |
|||
function ispalindrome(n = integer) = integer |
|||
var i, j, result = integer |
|||
var s = string |
|||
s = str$(n) |
|||
i = 2 rem - skip over leading sign or space |
|||
j = len(s) |
|||
while i < j and (mid(s,i,1)) = (mid(s,j,1)) do |
|||
begin |
|||
i = i + 1 |
|||
j = j - 1 |
|||
end |
|||
if (mid(s,i,1)) = (mid(s,j,1)) then |
|||
result = TRUE |
|||
else |
|||
result = FALSE |
|||
end = result |
|||
rem - return n mod m |
|||
function mod(n, m = integer) = integer |
|||
end = n - m * (n / m) |
|||
rem - return true if n is prime, otherwise false |
|||
function isprime(n = integer) = integer |
|||
var i, limit, result = integer |
|||
if n = 2 then |
|||
result = TRUE |
|||
else if (n < 2) or (mod(n,2) = 0) then |
|||
result = FALSE |
|||
else |
|||
begin |
|||
limit = int(sqr(n)) |
|||
i = 3 |
|||
while (i <= limit) and (mod(n, i) <> 0) do |
|||
i = i + 2 |
|||
result = not (i <= limit) |
|||
end |
|||
end = result |
|||
rem - main code begins here |
|||
var i, count = integer |
|||
print "Looking up to 1000 for palindromic primes" |
|||
count = 0 |
|||
for i = 2 to 1000 |
|||
if isprime(i) then |
|||
if ispalindrome(i) then |
|||
begin |
|||
print using "##### ";i; |
|||
count = count + 1 |
|||
if mod(count, 6) = 0 then print |
|||
end |
|||
next i |
|||
print |
|||
print count; " were found" |
|||
end |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Looking up to 1000 for palindromic primes |
|||
2 3 5 7 11 101 |
|||
131 151 181 191 313 353 |
|||
373 383 727 757 787 797 |
|||
919 929 |
|||
20 were found |
|||
</pre> |
|||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
<syntaxhighlight lang="ruby">func palindromic_primes(upto, base = 10) { |
<syntaxhighlight lang="ruby">func palindromic_primes(upto, base = 10) { |