Palindromic primes: Difference between revisions
Content added Content deleted
(Added Easylang) |
(Added PL/I-80 example) |
||
Line 916: | Line 916: | ||
<!--</syntaxhighlight>--> |
<!--</syntaxhighlight>--> |
||
Same output. Didn't actually test if this way was any faster, but expect it would be. |
Same output. Didn't actually test if this way was any faster, but expect it would be. |
||
=={{header|PL/I-80}}== |
|||
<syntaxhighlight lang = "pl/i"> |
|||
palindromic_primes: procedure options (main); |
|||
%replace |
|||
search_limit by 1000, |
|||
true by '1'b, |
|||
false by '0'b; |
|||
dcl |
|||
(k, count) fixed bin; |
|||
put skip edit('Showing palindromic primes up to ',search_limit) (a,f(4)); |
|||
put skip; |
|||
count = 0; |
|||
do k = 2 to search_limit; |
|||
if isprime(k) & ispalindrome(k) then |
|||
do; |
|||
put edit(k) (f(6)); |
|||
count = count + 1; |
|||
if mod(count,8) = 0 then put skip; |
|||
end; |
|||
end; |
|||
put skip edit(count, ' were found') (f(4),a); |
|||
isprime: proc(n) returns (bit(1)); |
|||
dcl |
|||
(n, i, limit) fixed bin; |
|||
/* deal with special cases first */ |
|||
if n < 2 then return (false); |
|||
if mod(n, 2) = 0 then return (n = 2); |
|||
/* else search up to sqrt of n for divisors */ |
|||
limit = floor(sqrt(n)); |
|||
/* aside from 2, only odd numbers can be prime */ |
|||
i = 3; |
|||
do while ((i <= limit) & (mod(n, i) ^= 0)); |
|||
i = i + 2; |
|||
end; |
|||
return (i > limit); |
|||
end isprime; |
|||
ispalindrome: proc(n) returns (bit(1)); |
|||
dcl |
|||
(n, nn, q, nd, i, j) fixed bin, |
|||
digits(1:10) fixed bin; |
|||
/* n is passed by reference, and is modified by */ |
|||
/* the algorithm, so we work with a local copy */ |
|||
nn = n; |
|||
/* strip off digits and store in array */ |
|||
nd = 0; /* digits found so far */ |
|||
do while (nn > 0); |
|||
nd = nd + 1; |
|||
q = floor(nn / 10); |
|||
digits(nd) = nn - q * 10; |
|||
nn = q; |
|||
end; |
|||
/* move from outside in looking for equality */ |
|||
i = 1; |
|||
j = nd; |
|||
do while ((i < j) & (digits(i) = digits(j))); |
|||
i = i + 1; |
|||
j = j - 1; |
|||
end; |
|||
return (digits(i) = digits(j)); |
|||
end ispalindrome; |
|||
end palindromic_primes; |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Showing palindromic primes up to 1000 |
|||
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|Python}}== |
=={{header|Python}}== |