Rare numbers: Difference between revisions

304 bytes removed ,  16 days ago
m (→‎{{header|Wren}}: Minor tidy)
 
(2 intermediate revisions by the same user not shown)
Line 3,896:
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">val .perfectsquare = ffn(.n) isInteger{ (.n ^/ 2) div 1 }
{{works with|langur|0.8.11}}
<syntaxhighlight lang="langur">val .perfectsquare = f isInteger .n ^/ 2
 
val .israre = ffn(.n) {
val .r = reverse(.n)
if .n == .r: return false
Line 3,907 ⟶ 3,906:
}
 
val .findfirst = ffn(.max) {
for[=[]] .i = 0; len(_for) < .max; .i += 1 {
if .israre(.i) {
Line 3,917 ⟶ 3,916:
# if you have the time...
writeln "the first 5 rare numbers: ", .findfirst(5)</syntaxhighlight>
 
With 0.8.11, the built-in reverse() function will flip the digits of a number. Without this, you could write your own function to do so as follows (if not passed any negative numbers).
 
<syntaxhighlight lang="langur">val .reverse = f toNumber join reverse split .n</syntaxhighlight>
 
=={{header|Lua}}==
885

edits