Self numbers: Difference between revisions

Content added Content deleted
m (→‎{{header|Perl}}: removed unused variables)
m (syntax highlighting fixup automation)
Line 15: Line 15:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{Trans|Go}}
{{Trans|Go}}
<lang algol68>BEGIN # find some self numbers numbers n such that there is no g such that g + sum of g's digits = n #
<syntaxhighlight lang="algol68">BEGIN # find some self numbers numbers n such that there is no g such that g + sum of g's digits = n #
INT max number = 1 999 999 999 + 82; # maximum n plus g we will condifer #
INT max number = 1 999 999 999 + 82; # maximum n plus g we will condifer #
# sieve the self numbers up to 1 999 999 999 #
# sieve the self numbers up to 1 999 999 999 #
Line 74: Line 74:
FI
FI
OD
OD
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 97: Line 97:
I couldn't follow the math in the Wikipedia entry, nor the discussion and code here so far. But an initial expedient of generating a list of all the integers from 1 to just over ten times the required number of results and then deleting those that ''could'' be derived by the described method revealed the sequencing pattern on which the code below is based. On the test machine, it completes all three of the tests at the bottom in a total of around a millisecond.
I couldn't follow the math in the Wikipedia entry, nor the discussion and code here so far. But an initial expedient of generating a list of all the integers from 1 to just over ten times the required number of results and then deleting those that ''could'' be derived by the described method revealed the sequencing pattern on which the code below is based. On the test machine, it completes all three of the tests at the bottom in a total of around a millisecond.


<lang applescript>(*
<syntaxhighlight lang="applescript">(*
Base-10 self numbers by index (single or range).
Base-10 self numbers by index (single or range).
Follows an observed sequence pattern whereby, after the initial single-digit odd numbers, self numbers are
Follows an observed sequence pattern whereby, after the initial single-digit odd numbers, self numbers are
Line 202: Line 202:
-- 97,777,777,792nd:
-- 97,777,777,792nd:
selfNumbers(9.7777777792E+10)
selfNumbers(9.7777777792E+10)
--> {9.99999999997E+11}</lang>
--> {9.99999999997E+11}</syntaxhighlight>


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SELF_NUMBERS.AWK
# syntax: GAWK -f SELF_NUMBERS.AWK
# converted from Go (low memory example)
# converted from Go (low memory example)
Line 255: Line 255:
}
}
function max(x,y) { return((x > y) ? x : y) }
function max(x,y) { return((x > y) ? x : y) }
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 278: Line 278:
{{trans|Go}}
{{trans|Go}}
About 25% faster than Go (using GCC 7.5.0 -O3) mainly due to being able to iterate through the sieve using a pointer.
About 25% faster than Go (using GCC 7.5.0 -O3) mainly due to being able to iterate through the sieve using a pointer.
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <time.h>
#include <time.h>
Line 341: Line 341:
printf("Took %lf seconds.\n", (double)(clock() - begin) / CLOCKS_PER_SEC);
printf("Took %lf seconds.\n", (double)(clock() - begin) / CLOCKS_PER_SEC);
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 354: Line 354:
===Extended===
===Extended===
{{trans|Pascal}}
{{trans|Pascal}}
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <time.h>
#include <time.h>
Line 430: Line 430:
printf("\nOverall took %lf seconds.\n", (double)(clock() - begin) / CLOCKS_PER_SEC);
printf("\nOverall took %lf seconds.\n", (double)(clock() - begin) / CLOCKS_PER_SEC);
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 456: Line 456:
=={{header|C++}}==
=={{header|C++}}==
{{trans|Java}}
{{trans|Java}}
<lang cpp>#include <array>
<syntaxhighlight lang="cpp">#include <array>
#include <iomanip>
#include <iomanip>
#include <iostream>
#include <iostream>
Line 508: Line 508:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>The first 50 self numbers are:
<pre>The first 50 self numbers are:
Line 526: Line 526:
=={{header|C#|CSharp}}==
=={{header|C#|CSharp}}==
{{trans|Pascal}}via{{trans|Go}}(third version) Stripped down, as C# limits the size of an array to Int32.MaxValue, so the sieve isn't large enough to hit the 1,000,000,000th value.
{{trans|Pascal}}via{{trans|Go}}(third version) Stripped down, as C# limits the size of an array to Int32.MaxValue, so the sieve isn't large enough to hit the 1,000,000,000th value.
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using static System.Console;
using static System.Console;


Line 558: Line 558:
WriteLine("\nOverall took {0}s", (DateTime.Now - st). TotalSeconds);
WriteLine("\nOverall took {0}s", (DateTime.Now - st). TotalSeconds);
}
}
}</lang>
}</syntaxhighlight>
{{out}}Timing from tio.run
{{out}}Timing from tio.run
<pre>Sieving took 3.4266187s
<pre>Sieving took 3.4266187s
Line 579: Line 579:


=={{header|Elixir}}==
=={{header|Elixir}}==
<lang elixir>
<syntaxhighlight lang="elixir">
defmodule SelfNums do
defmodule SelfNums do


Line 606: Line 606:
end
end
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 616: Line 616:


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Self numbers. Nigel Galloway: October 6th., 2020
// Self numbers. Nigel Galloway: October 6th., 2020
let fN g=let rec fG n g=match n/10 with 0->n+g |i->fG i (g+(n%10)) in fG g g
let fN g=let rec fG n g=match n/10 with 0->n+g |i->fG i (g+(n%10)) in fG g g
Line 623: Line 623:
Self |> Seq.take 50 |> Seq.iter(printf "%d "); printfn ""
Self |> Seq.take 50 |> Seq.iter(printf "%d "); printfn ""
printfn "\n%d" (Seq.item 99999999 Self)
printfn "\n%d" (Seq.item 99999999 Self)
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 634: Line 634:
=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
{{trans|Ring}}
{{trans|Ring}}
<lang freebasic>Print "The first 50 self numbers are:"
<syntaxhighlight lang="freebasic">Print "The first 50 self numbers are:"
Dim As Boolean flag
Dim As Boolean flag
Dim As Ulong m, p, sum, number(), sum2
Dim As Ulong m, p, sum, number(), sum2
Line 669: Line 669:
End If
End If
Loop
Loop
Sleep</lang>
Sleep</syntaxhighlight>




Line 675: Line 675:
===Low memory===
===Low memory===
Simple-minded, uses very little memory (no sieve) but slow - over 2.5 minutes.
Simple-minded, uses very little memory (no sieve) but slow - over 2.5 minutes.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 742: Line 742:
fmt.Println("\nThe 100 millionth self number is", lastSelf)
fmt.Println("\nThe 100 millionth self number is", lastSelf)
fmt.Println("Took", time.Since(st))
fmt.Println("Took", time.Since(st))
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 759: Line 759:


Have also incorporated Enter your username's suggestion (see Talk page) of using partial sums for each loop which improves performance by about 25%.
Have also incorporated Enter your username's suggestion (see Talk page) of using partial sums for each loop which improves performance by about 25%.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 821: Line 821:
}
}
fmt.Println("Took", time.Since(st))
fmt.Println("Took", time.Since(st))
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 835: Line 835:
{{trans|Pascal}}
{{trans|Pascal}}
This uses horst.h's ideas (see Talk page) to find up to the 1 billionth self number in a reasonable time and using less memory than the simple 'sieve based' approach above would have needed.
This uses horst.h's ideas (see Talk page) to find up to the 1 billionth self number in a reasonable time and using less memory than the simple 'sieve based' approach above would have needed.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 910: Line 910:
}
}
fmt.Println("\nOverall took", time.Since(st))
fmt.Println("\nOverall took", time.Since(st))
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 937: Line 937:
The solution is quite straightforward. The length of the foreseeing window in filtering procedure (81) is chosen empirically and doesn't have any theoretical background.
The solution is quite straightforward. The length of the foreseeing window in filtering procedure (81) is chosen empirically and doesn't have any theoretical background.


<lang haskell>import Control.Monad (forM_)
<syntaxhighlight lang="haskell">import Control.Monad (forM_)
import Text.Printf
import Text.Printf


Line 961: Line 961:
print $ take 50 selfs
print $ take 50 selfs
forM_ [1..8] $ \i ->
forM_ [1..8] $ \i ->
printf "1e%v\t%v\n" (i :: Int) (selfs !! (10^i-1))</lang>
printf "1e%v\t%v\n" (i :: Int) (selfs !! (10^i-1))</syntaxhighlight>


<pre>$ ghc -O2 SelfNum.hs && time ./SelfNum
<pre>$ ghc -O2 SelfNum.hs && time ./SelfNum
Line 977: Line 977:
=={{header|Java}}==
=={{header|Java}}==
{{trans|C#}}
{{trans|C#}}
<lang java>public class SelfNumbers {
<syntaxhighlight lang="java">public class SelfNumbers {
private static final int MC = 103 * 1000 * 10000 + 11 * 9 + 1;
private static final int MC = 103 * 1000 * 10000 + 11 * 9 + 1;
private static final boolean[] SV = new boolean[MC + 1];
private static final boolean[] SV = new boolean[MC + 1];
Line 1,023: Line 1,023:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>The first 50 self numbers are:
<pre>The first 50 self numbers are:
Line 1,042: Line 1,042:
'''Adapted from [[#Julia|Julia]]'''
'''Adapted from [[#Julia|Julia]]'''
{{works with|jq}}
{{works with|jq}}
<syntaxhighlight lang="jq">
<lang jq>
def sumdigits: tostring | explode | map([.]|implode|tonumber) | add;
def sumdigits: tostring | explode | map([.]|implode|tonumber) | add;


Line 1,077: Line 1,077:
then "Yes, \(.) is the 100,000,000th self number."
then "Yes, \(.) is the 100,000,000th self number."
else "No, \(.i) is actually the 100,000,000th self number."
else "No, \(.i) is actually the 100,000,000th self number."
end)</lang>
end)</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,093: Line 1,093:
Note that 81 is the window size because the sum of digits of 999,999,999
Note that 81 is the window size because the sum of digits of 999,999,999
(the largest digit sum of a counting number less than 1022727208) is 81.
(the largest digit sum of a counting number less than 1022727208) is 81.
<lang julia>gsum(i) = sum(digits(i)) + i
<syntaxhighlight lang="julia">gsum(i) = sum(digits(i)) + i
isnonself(i) = any(x -> gsum(x) == i, i-1:-1:i-max(1, ndigits(i)*9))
isnonself(i) = any(x -> gsum(x) == i, i-1:-1:i-max(1, ndigits(i)*9))
const last81 = filter(isnonself, 1:5000)[1:81]
const last81 = filter(isnonself, 1:5000)[1:81]
Line 1,119: Line 1,119:


checkselfnumbers()
checkselfnumbers()
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
1 3 5 7 9 20 31 42 53 64 75 86 97 108 110 121 132 143 154 165 176 187 198 209 211 222 233 244 255 266 277 288 299 310 312 323 334 345 356 367 378 389 400 411 413 424 435 446 457 468
1 3 5 7 9 20 31 42 53 64 75 86 97 108 110 121 132 143 154 165 176 187 198 209 211 222 233 244 255 266 277 288 299 310 312 323 334 345 356 367 378 389 400 411 413 424 435 446 457 468
Line 1,128: Line 1,128:
{{trans|Pascal}}
{{trans|Pascal}}
Contains tweaks peculiar to the "10 to the nth" self number. Timings include compilation times.
Contains tweaks peculiar to the "10 to the nth" self number. Timings include compilation times.
<lang julia>const MAXCOUNT = 103 * 10000 * 10000 + 11 * 9 + 1
<syntaxhighlight lang="julia">const MAXCOUNT = 103 * 10000 * 10000 + 11 * 9 + 1


function dosieve!(sieve, digitsum9999)
function dosieve!(sieve, digitsum9999)
Line 1,171: Line 1,171:


@time findselves()
@time findselves()
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Sieve time:
Sieve time:
Line 1,191: Line 1,191:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Java}}
{{trans|Java}}
<lang scala>private const val MC = 103 * 1000 * 10000 + 11 * 9 + 1
<syntaxhighlight lang="scala">private const val MC = 103 * 1000 * 10000 + 11 * 9 + 1
private val SV = BooleanArray(MC + 1)
private val SV = BooleanArray(MC + 1)


Line 1,267: Line 1,267:
i++
i++
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>The first 50 self numbers are:
<pre>The first 50 self numbers are:
Line 1,284: Line 1,284:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">
<lang Mathematica>
sum[g_] := g + Total@IntegerDigits@g
sum[g_] := g + Total@IntegerDigits@g


Line 1,296: Line 1,296:
If[self[x], Sow[x]; t++]; x++]
If[self[x], Sow[x]; t++]; x++]
][[2, 1]]]
][[2, 1]]]
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,311: Line 1,311:


Note that we used a sequence of ten nested loops as in the Go solution but we have not memorized the intermediate sums as the C compiler does a good job to detect the loop invariants (remember, Nim produces C code and this code has proved to be quite optimizable by the C compiler). The ten loops looks a lot better this way, too 🙂.
Note that we used a sequence of ten nested loops as in the Go solution but we have not memorized the intermediate sums as the C compiler does a good job to detect the loop invariants (remember, Nim produces C code and this code has proved to be quite optimizable by the C compiler). The ten loops looks a lot better this way, too 🙂.
<lang Nim>import bitops, strutils, std/monotimes, times
<syntaxhighlight lang="nim">import bitops, strutils, std/monotimes, times


const MaxCount = 2 * 1_000_000_000 + 9 * 9
const MaxCount = 2 * 1_000_000_000 + 9 * 9
Line 1,392: Line 1,392:
echo ($count).align(10), ($n).align(12)
echo ($count).align(10), ($n).align(12)
limit *= 10
limit *= 10
echo "Total time: ", getMonoTime() - t0</lang>
echo "Total time: ", getMonoTime() - t0</syntaxhighlight>


{{out}}
{{out}}
Line 1,419: Line 1,419:
Of course, the program is slower but not in the same proportion that in the previous program: it is about twice slower than the Pascal version. Note that the execution time varies significantly according to the way statements are written. For instance, writing <code>if not sieve[n]: inc count</code> has proved to be more efficient than writing <code>inc count, ord(not sieve[n])</code> or <code>inc count, 1 - ord(sieve[n])</code> which is surprising as the latter forms avoid a jump. Maybe changing some other statements could give better results, but current time is already satisfying.
Of course, the program is slower but not in the same proportion that in the previous program: it is about twice slower than the Pascal version. Note that the execution time varies significantly according to the way statements are written. For instance, writing <code>if not sieve[n]: inc count</code> has proved to be more efficient than writing <code>inc count, ord(not sieve[n])</code> or <code>inc count, 1 - ord(sieve[n])</code> which is surprising as the latter forms avoid a jump. Maybe changing some other statements could give better results, but current time is already satisfying.


<lang Nim>import bitops, strutils, std/monotimes, times
<syntaxhighlight lang="nim">import bitops, strutils, std/monotimes, times


const MaxCount = 103 * 10_000 * 10_000 + 11 * 9 + 1
const MaxCount = 103 * 10_000 * 10_000 + 11 * 9 + 1
Line 1,508: Line 1,508:
echo ($count).align(10), ($n).align(12)
echo ($count).align(10), ($n).align(12)
limit *= 10
limit *= 10
echo "Total time: ", getMonoTime() - t0</lang>
echo "Total time: ", getMonoTime() - t0</syntaxhighlight>


{{out}}
{{out}}
Line 1,533: Line 1,533:
Just "sieving" with only one follower of every number {{trans|Go}}
Just "sieving" with only one follower of every number {{trans|Go}}
Extended to 10.23e9
Extended to 10.23e9
<lang pascal>program selfnumb;
<syntaxhighlight lang="pascal">program selfnumb;
{$IFDEF FPC}
{$IFDEF FPC}
{$MODE Delphi}
{$MODE Delphi}
Line 1,629: Line 1,629:
end;
end;
end;
end;
END.</lang>
END.</syntaxhighlight>
{{out}}
{{out}}
<pre> time ./selfnumb
<pre> time ./selfnumb
Line 1,649: Line 1,649:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature 'say';
use feature 'say';
Line 1,677: Line 1,677:
}
}


say "The first 50 self numbers are:\n" . join ' ', @selfs;</lang>
say "The first 50 self numbers are:\n" . join ' ', @selfs;</syntaxhighlight>
{{out}}
{{out}}
<pre>The first 50 self numbers are:
<pre>The first 50 self numbers are:
Line 1,686: Line 1,686:
Certainly puts my previous rubbish attempts ([[Self_numbers\Phix|archived here]]) to shame.<br>
Certainly puts my previous rubbish attempts ([[Self_numbers\Phix|archived here]]) to shame.<br>
The precise nature of the difference-pattern eludes me, I will admit.
The precise nature of the difference-pattern eludes me, I will admit.
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">--
<span style="color: #000080;font-style:italic;">--
-- Base-10 self numbers by index (single or range).
-- Base-10 self numbers by index (single or range).
Line 1,779: Line 1,779:
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"completed in %s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"completed in %s\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">))</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,793: Line 1,793:
=={{header|Python}}==
=={{header|Python}}==
{{works with|Python|2.7}}
{{works with|Python|2.7}}
<lang python>class DigitSumer :
<syntaxhighlight lang="python">class DigitSumer :
def __init__(self):
def __init__(self):
sumdigit = lambda n : sum( map( int,str( n )))
sumdigit = lambda n : sum( map( int,str( n )))
Line 1,827: Line 1,827:
if i == p :
if i == p :
print '%7.1f sec %9dth = %d'%( time.time()-t,i,s )
print '%7.1f sec %9dth = %d'%( time.time()-t,i,s )
p *= 10</lang>
p *= 10</syntaxhighlight>
{{out}}
{{out}}
<pre>1 3 5 7 9 20 31 42 53 64 75 86 97 108 110 121 132 143 154 165 176 187 198 209 211 222 233 244 255 266 277 288 299 310 312 323 334 345 356 367 378 389 400 411 413 424 435 446 457 468
<pre>1 3 5 7 9 20 31 42 53 64 75 86 97 108 110 121 132 143 154 165 176 187 198 209 211 222 233 244 255 266 277 288 299 310 312 323 334 345 356 367 378 389 400 411 413 424 435 446 457 468
Line 1,841: Line 1,841:
Translated the low memory version of the Go entry but showed only the first 50 self numbers. The machine for running this task (a Xeon E3110+8GB memory) is showing its age as, 1) took over 6 minutes to complete the Go entry, 2) not even able to run the other two Go alternative entries and 3) needed over 47 minutes to reach 1e6 iterations here. Anyway I will try this on an i5 box later to see how it goes.
Translated the low memory version of the Go entry but showed only the first 50 self numbers. The machine for running this task (a Xeon E3110+8GB memory) is showing its age as, 1) took over 6 minutes to complete the Go entry, 2) not even able to run the other two Go alternative entries and 3) needed over 47 minutes to reach 1e6 iterations here. Anyway I will try this on an i5 box later to see how it goes.
{{trans|Go}}
{{trans|Go}}
<lang perl6># 20201127 Raku programming solution
<syntaxhighlight lang="raku" line># 20201127 Raku programming solution


my ( $st, $count, $i, $pow, $digits, $offset, $lastSelf, $done, @selfs) =
my ( $st, $count, $i, $pow, $digits, $offset, $lastSelf, $done, @selfs) =
Line 1,874: Line 1,874:


# say "The 100 millionth self number is ", $lastSelf;
# say "The 100 millionth self number is ", $lastSelf;
# say "Took ", now - $st, " seconds."</lang>
# say "Took ", now - $st, " seconds."</syntaxhighlight>
{{out}}
{{out}}
<pre>The first 50 self numbers are:
<pre>The first 50 self numbers are:
Line 1,881: Line 1,881:
=={{header|REXX}}==
=={{header|REXX}}==
=== first 50 self numbers ===
=== first 50 self numbers ===
<lang rexx>/*REXX program displays N self numbers (aka Colombian or Devlali numbers). OEIS A3052.*/
<syntaxhighlight lang="rexx">/*REXX program displays N self numbers (aka Colombian or Devlali numbers). OEIS A3052.*/
parse arg n . /*obtain optional argument from the CL.*/
parse arg n . /*obtain optional argument from the CL.*/
if n=='' | n=="," then n= 50 /*Not specified? Then use the default.*/
if n=='' | n=="," then n= 50 /*Not specified? Then use the default.*/
Line 1,900: Line 1,900:
/*stick a fork in it, we're all done. */
/*stick a fork in it, we're all done. */
say n " self numbers were found." /*display the title for the output list*/
say n " self numbers were found." /*display the title for the output list*/
if tell then say list /*display list of self numbers ──►term.*/</lang>
if tell then say list /*display list of self numbers ──►term.*/</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>
Line 1,909: Line 1,909:
=== ten millionth self number ===
=== ten millionth self number ===
{{trans|Go (low memory)}}
{{trans|Go (low memory)}}
<lang rexx>/*REXX pgm displays the Nth self number, aka Colombian or Devlali numbers. OEIS A3052.*/
<syntaxhighlight lang="rexx">/*REXX pgm displays the Nth self number, aka Colombian or Devlali numbers. OEIS A3052.*/
numeric digits 20 /*ensure enough decimal digits for #. */
numeric digits 20 /*ensure enough decimal digits for #. */
parse arg high . /*obtain optional argument from the CL.*/
parse arg high . /*obtain optional argument from the CL.*/
Line 1,945: Line 1,945:
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg _; do c=length(_)-3 to 1 by -3; _=insert(',', _, c); end; return _
commas: parse arg _; do c=length(_)-3 to 1 by -3; _=insert(',', _, c); end; return _
th: parse arg th; return word('th st nd rd', 1 +(th//10)*(th//100%10\==1)*(th//10<4))</lang>
th: parse arg th; return word('th st nd rd', 1 +(th//10)*(th//100%10\==1)*(th//10<4))</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
{{out|output|text=&nbsp; when using the default input:}}
<pre>
<pre>
Line 1,952: Line 1,952:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
load "stdlib.ring"
load "stdlib.ring"


Line 1,995: Line 1,995:


see "done..." + nl
see "done..." + nl
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 2,056: Line 2,056:
=={{header|Sidef}}==
=={{header|Sidef}}==
Algorithm by David A. Corneth (OEIS: A003052).
Algorithm by David A. Corneth (OEIS: A003052).
<lang ruby>func is_self_number(n) {
<syntaxhighlight lang="ruby">func is_self_number(n) {


if (n < 30) {
if (n < 30) {
Line 2,079: Line 2,079:
}
}


say is_self_number.first(50).join(' ')</lang>
say is_self_number.first(50).join(' ')</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,087: Line 2,087:
Simpler algorithm (by M. F. Hasler):
Simpler algorithm (by M. F. Hasler):


<lang ruby>func is_self_number(n) {
<syntaxhighlight lang="ruby">func is_self_number(n) {
1..min(n>>1, 9*n.len) -> none {|i| sumdigits(n-i) == i } && (n > 0)
1..min(n>>1, 9*n.len) -> none {|i| sumdigits(n-i) == i } && (n > 0)
}</lang>
}</syntaxhighlight>


=={{header|Standard ML}}==
=={{header|Standard ML}}==
<syntaxhighlight lang="ocaml">
<lang OCaml>
open List;
open List;


Line 2,118: Line 2,118:
app ((fn s => print (s ^ " ")) o Int.toString o selfNumberNr) (tabulate (50,fn i=>i+1)) ;
app ((fn s => print (s ^ " ")) o Int.toString o selfNumberNr) (tabulate (50,fn i=>i+1)) ;
selfNumberNr 100000000 ;
selfNumberNr 100000000 ;
</syntaxhighlight>
</lang>
output
output
<pre>
<pre>
Line 2,132: Line 2,132:


Unsurprisingly, very slow compared to the Go version as Wren is interpreted and uses floating point arithmetic for all numerical work.
Unsurprisingly, very slow compared to the Go version as Wren is interpreted and uses floating point arithmetic for all numerical work.
<lang ecmascript>var sieve = Fn.new {
<syntaxhighlight lang="ecmascript">var sieve = Fn.new {
var sv = List.filled(2*1e9+9*9+1, false)
var sv = List.filled(2*1e9+9*9+1, false)
var n = 0
var n = 0
Line 2,183: Line 2,183:
}
}
}
}
System.print("Took %(System.clock-st) seconds.")</lang>
System.print("Took %(System.clock-st) seconds.")</syntaxhighlight>


{{out}}
{{out}}