Calmo numbers: Difference between revisions
Content added Content deleted
(Add Mathematica/Wolfram Language implementation) |
(Add PARI/GP implementation) |
||
Line 747: | Line 747: | ||
<pre> 165 273 385 399 561 595 665 715 957 |
<pre> 165 273 385 399 561 595 665 715 957 |
||
</pre> |
</pre> |
||
=={{header|PARI/GP}}== |
|||
<syntaxhighlight lang="PARI/GP"> |
|||
\\ Function to get the divisors of a number excluding 1 and the number itself |
|||
eligibleDivisors(n) = { |
|||
my(divs = divisors(n)); \\ Compute all divisors of n |
|||
if (#divs <= 2, return([])); \\ If there are no divisors other than 1 and n, return an empty list |
|||
vecextract(divs, "2..-2"); \\ Extract divisors without the first and last elements (1 and n) |
|||
}; |
|||
\\ Function to check if the sum of every three consecutive eligible divisors is prime |
|||
isCalmoNumber(n) = { |
|||
my(divisors, sums, i, k); |
|||
divisors = eligibleDivisors(n); \\ Get eligible divisors |
|||
if ((#divisors % 3 != 0) || (#divisors == 0), return(0)); \\ If not divisible by 3 or no divisors, return false |
|||
sums = vector(#divisors \ 3, i, sum(k = 1, 3, divisors[3*i + k - 3])); \\ Compute sums of every three divisors |
|||
for (i = 1, #sums, |
|||
if (!isprime(sums[i]), return(0)); \\ If any sum is not prime, return false |
|||
); |
|||
return(1); \\ If all sums are prime, return true |
|||
}; |
|||
{ |
|||
\\ Find all Calmo numbers under 1000 |
|||
calmoNumbers = []; |
|||
for (n = 1, 1000, |
|||
if (isCalmoNumber(n), calmoNumbers = concat(calmoNumbers, n)); \\ Check if n is a Calmo number and add to the list |
|||
); |
|||
print(calmoNumbers); \\ Print all Calmo numbers found under 1000 |
|||
} |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[165, 273, 385, 399, 561, 595, 665, 715, 957] |
|||
</pre> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |