Rare numbers: Difference between revisions
Content added Content deleted
Langurmonkey (talk | contribs) |
Langurmonkey (talk | contribs) |
||
Line 3,894: | Line 3,894: | ||
=== not optimized === |
=== not optimized === |
||
It could look something like the following (ignoring whatever optimizations the other examples are using), if it was fast enough. I did not have the time/processor to test finding the first 5. The |
It could look something like the following (ignoring whatever optimizations the other examples are using), if it was fast enough. I did not have the time/processor to test finding the first 5. The israre() function appears to return the right answer, tested with individual numbers. |
||
<syntaxhighlight lang="langur"> |
<syntaxhighlight lang="langur"> |
||
val perfectsquare = fn n: (n ^/ 2) div 1 |
|||
val |
val israre = fn(n) { |
||
val |
val r = reverse(n) |
||
if |
if n == r: return false |
||
val |
val sum = n + r |
||
val |
val diff = n - r |
||
diff > 0 and perfectsquare(sum) and perfectsquare(diff) |
|||
} |
} |
||
val |
val findfirst = fn(mx) { |
||
for[=[]] |
for[=[]] i = 0; len(_for) < mx; i += 1 { |
||
if |
if israre(i) { |
||
_for ~= [ |
_for ~= [i] |
||
} |
|||
} |
|||
} |
|||
val findandprint = impure fn(mx) { |
|||
for[cnt=0] i = 0; cnt < mx ; i += 1 { |
|||
if israre(i) { |
|||
writeln "\n rare ", i, " " |
|||
cnt += 1 |
|||
} |
} |
||
} |
} |
||
Line 3,915: | Line 3,925: | ||
# if you have the time... |
# if you have the time... |
||
writeln "the first 5 rare numbers: ", |
writeln "the first 5 rare numbers: ", findfirst(5) |
||
</syntaxhighlight> |
|||
=={{header|Lua}}== |
=={{header|Lua}}== |