Smith numbers: Difference between revisions
m
syntax highlighting fixup automation
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 30:
{{trans|Python}}
<
[Int] rt
V f = 2
Line 77:
print()
print(‘Last 12 Smith Numbers below 10000:’)
print_elements(sn[(len)-12..])</
{{out}}
Line 92:
=={{header|360 Assembly}}==
{{trans|Rexx}}
<
SMITHNUM CSECT
USING SMITHNUM,R13 base register
Line 238:
XDEC DS CL12 temp
YREGS
END SMITHNUM</
{{out}}
<pre>
Line 270:
=={{header|Action!}}==
Calculations on a real Atari 8-bit computer take quite long time. It is recommended to use an emulator capable with increasing speed of Atari CPU.
<
CARD res,a
Line 320:
Poke(77,0) ;turn off the attract mode
OD
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Smith_numbers.png Screenshot from Atari 8-bit computer]
Line 344:
=={{header|Ada}}==
{{works with|Ada|2012}}
<
with Ada.Text_IO;
Line 372:
end loop;
end smith;
</syntaxhighlight>
=={{header|ALGOL 68}}==
<
PROC sieve = ( REF[]BOOL s )VOID:
BEGIN
Line 447:
OD;
print( ( newline, "THere are ", whole( smith count, -7 ), " Smith numbers below ", whole( max number, -7 ), newline ) )
</syntaxhighlight>
{{out}}
<pre>
Line 460:
=={{header|Arturo}}==
<
n: new v
result: new 0
Line 483:
]
]
print ""</
{{out}}
Line 527:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f SMITH_NUMBERS.AWK
# converted from C
Line 605:
return(sum)
}
</syntaxhighlight>
{{out}}
<pre>
Line 636:
=={{header|BASIC}}==
<
20 DIM F(32)
30 FOR I=2 TO 9999
Line 656:
190 NEXT
200 PRINT
210 PRINT "Found";C;"Smith numbers."</
{{out}}
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382
Line 685:
=={{header|BCPL}}==
<
// Find the sum of the digits of N
Line 736:
$)
writef("*NFound %N Smith numbers.*N", count)
$)</
{{out}}
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382
Line 766:
=={{header|C}}==
{{trans|C++}}
<syntaxhighlight lang="c">
#include <stdlib.h>
#include <stdio.h>
Line 845:
return 0;
}
</syntaxhighlight>
{{out}}
<pre>
Line 877:
=={{header|C sharp|C#}}==
{{trans|java}}
<
using System.Collections.Generic;
Line 933:
}
}
}</
{{out}}
<pre> 4 22 27 58 85 94 166 202
Line 984:
=={{header|C++}}==
<
#include <iostream>
#include <vector>
Line 1,030:
return 0;
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,041:
=={{header|Clojure}}==
<
(zero? (mod a b)))
Line 1,066:
(sum-digits (clojure.string/join "" (prime-factors n))))))
(filter smith-number? (range 1 10000))</
{{out}}
Line 1,076:
=={{header|CLU}}==
<
digits = iter (n: int) yields (int)
while n > 0 do
Line 1,137:
end
stream$putl(po, "\nFound " || int$unparse(count) || " Smith numbers.")
end start_up</
{{out}}
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382
Line 1,166:
=={{header|Cowgol}}==
<
typedef N is uint16; # 16-bit math is good enough
Line 1,247:
print("Found ");
print_i32(count as uint32);
print(" Smith numbers.\n");</
{{out}}
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382
Line 1,277:
=={{header|D}}==
{{trans|Java}} mostly
<
void main() {
Line 1,329:
}
return sum;
}</
{{out}}
Line 1,374:
See [https://rosettacode.org/wiki/Smith_numbers#Pascal Pascal].
=={{header|Draco}}==
<
proc nonrec digitsum(word n) word:
word sum;
Line 1,443:
writeln();
writeln("Found ", count, " Smith numbers.")
corp</
{{out}}
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382
Line 1,472:
=={{header|Elixir}}==
<
def number?(n) do
d = decomposition(n)
Line 1,492:
IO.puts "#{length(smith)} smith numbers below #{m}:"
IO.puts "First 10: #{Enum.take(smith,10) |> Enum.join(", ")}"
IO.puts "Last 10: #{Enum.take(smith,-10) |> Enum.join(", ")}"</
{{out}}
Line 1,503:
=={{header|F_Sharp|F#}}==
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)]
<
// Generate Smith Numbers. Nigel Galloway: November 6th., 2020
let fN g=Seq.unfold(fun n->match n with 0->None |_->Some(n%10,n/10)) g |> Seq.sum
Line 1,510:
|>Seq.filter(fun g->fN g=fst(primes32()|>Seq.scan(fun n g->fG n g)(0,g)|>Seq.find(fun(_,n)->n=1)))
|>Seq.chunkBySize 20|>Seq.iter(fun n->Seq.iter(printf "%4d ") n; printfn "")
</syntaxhighlight>
{{out}}
<pre>
Line 1,534:
</pre>
=={{header|Factor}}==
<syntaxhighlight lang="text">USING: formatting grouping io kernel math.primes.factors
math.ranges math.text.utils sequences sequences.deep ;
Line 1,545:
10,000 [1,b] [ smith? ] filter 10 group
[ [ "%4d " printf ] each nl ] each</
{{out}}
<pre>
Line 1,595:
The factorisation is represented in a data aggregate, which is returned by function FACTOR. This is a facility introduced with F90, and before that one would have to use a collection of ordinary arrays to identify the list of primes and powers of a factorisation because functions could only return simple variables. Also, earlier compilers did not allow the use of the function's name as a variable within the function, or might allow this but produce incorrect results. However, modern facilities are not always entirely beneficial. Here, the function returns a full set of data for type FACTORED, even though often only the first few elements of the arrays will be needed and the rest could be ignored. It is possible to declare the arrays of type FACTORED to be "allocatable" with their size being determined at run time for each invocation of function FACTOR, at the cost of a lot of additional syntax and statements, plus the common annoyance of not knowing "how big" until ''after'' the list has been produced. Alas, such arrangements incur a performance penalty with ''every'' reference to the allocatable entities. See for example [[Sequence_of_primorial_primes#Run-time_allocation]]
For layout purposes, the numbers found were stashed in a line buffer rather than attempt to mess with the latter-day facilities of "non-advancing" output. This should be paramaterised for documentation purposes with say <code>MBUF = 20</code> rather than just using the magic constant of 20, however getting that into the FORMAT statement would require <code>FORMAT(<MBUF>I6)</code> and this <n> facility may not be recognised. Alternatively, one could put <code>FORMAT(666I6)</code> and hope that MBUF would never exceed 666. <
USE PRIMEBAG !This is a common need.
INTEGER LASTP !Some size allowances.
Line 1,719:
13 FORMAT (I9," found.") !Just in case.
END DO !On to the next base.
END !That was strange.</
Output: selecting the base ten result:
Line 1,767:
=={{header|FreeBASIC}}==
<
Sub getPrimeFactors(factors() As UInteger, n As UInteger)
Line 1,821:
Print
Print "Press any key to quit"
Sleep</
{{out}}
Line 1,865:
=={{header|Go}}==
{{trans|C}}
<syntaxhighlight lang="go">
package main
Line 1,949:
fmt.Println()
}
</syntaxhighlight>
{{out}}<pre>
All the Smith Numbers less than 10000 are:
Line 1,956:
=={{header|Haskell}}==
<
import Data.List (unfoldr)
import Data.Tuple (swap)
Line 1,989:
, "\nLast 12 Smith Numbers below 10k:"
, unwords (show <$> drop (lowSmithCount - 12) lowSmiths)
]</
{{Out}}
<pre>Count of Smith Numbers below 10k:
Line 2,002:
=={{header|J}}==
Implementation:
<
sumdig=: +/@,@digits
notprime=: -.@(1&p:)
smith=: #~ notprime * (=&sumdig q:)every</
Task example:
<
376
q:376
Line 2,059:
9598 9633 9634 9639 9648 9657 9684 9708
9717 9735 9742 9760 9778 9840 9843 9849
9861 9880 9895 9924 9942 9968 9975 9985</
(first we count how many smith numbers are in our result, then we look at the prime factors of that count - turns out that 8 columns of 47 numbers each is perfect for this task.)
Line 2,065:
=={{header|Java}}==
{{works with|Java|7}}
<
public class SmithNumbers {
Line 2,109:
return sum;
}
}</
<pre>4
22
Line 2,128:
{{Trans|Haskell}}
{{Trans|Python}}
<
'use strict';
Line 2,297:
return main();
})();</
{{Out}}
<pre>Count of Smith Numbers below 10k:
Line 2,313:
''' Preliminaries'''
<
. as $n
| if ($n < 2) then false
Line 2,341:
| select(($num % .) == 0 and is_prime)
| m(.));
</syntaxhighlight>
'''The task'''
<
def is_smith:
def sumdigits:
Line 2,352:
"Smith numbers up to 10000:\n",
(range(1; 10000) | select(is_smith))
</syntaxhighlight>
{{out}}
<pre>
Line 2,369:
=={{header|Julia}}==
<
function sumdigits(n::Integer)
Line 2,384:
smithnumbers = collect(n for n in 2:10000 if issmith(n))
println("Smith numbers up to 10000:\n$smithnumbers")</
{{out}}
Line 2,412:
=={{header|Kotlin}}==
{{trans|FreeBASIC}}
<
fun getPrimeFactors(n: Int): MutableList<Int> {
Line 2,461:
}
println("\n\n$count numbers found")
}</
{{out}}
Line 2,495:
=={{header|Lua}}==
Slightly long-winded prime factor function but it's a bit faster than the 'easy' way.
<
function isPrime (n)
if n < 2 then return false end
Line 2,545:
for n = 1, 10000 do
if isSmith(n) then io.write(n .. "\t") end
end</
Seems silly to paste in all 376 numbers but rest assured the output agrees with https://oeis.org/A006753
Line 2,559:
At the end we get a list (an inventory object with keys only). Print statement prints all keys (normally data, but if key isn't paired with data,then key is read only data)
<syntaxhighlight lang="m2000 interpreter">
Module Checkit {
Set Fast !
Line 2,617:
}
Checkit
</syntaxhighlight>
=={{header|MAD}}==
<
PRINT COMMENT$ SMITH NUMBERS$
Line 2,672:
TRANSFER TO LOOP
END OF FUNCTION
END OF PROGRAM</
{{out}}
Line 3,054:
=={{header|Maple}}==
<
local factors, sumofDigits, sumofFactorDigits, x;
if isprime(n) then
Line 3,069:
end proc:
findSmith(10000);</
{{out}}
<pre>[4, 22, 27, 58, 85, 94, 121, 166, 202, 265, 274, 319, 346, 355, 378, 382, 391, 438, 454, 483, 517, 526, 535, 562, 576, 588, 627, 634, 636, 645, 648, 654, 663, 666, 690, 706, 728, 729, 762, 778, 825, 852, 861, 895, 913, 915, 922, 958, 985, 1086, 1111, 1165, 1219, 1255, 1282, 1284, 1376, 1449, 1507, 1581, 1626, 1633, 1642, 1678, 1736, 1755, 1776, 1795, 1822, 1842, 1858, 1872, 1881, 1894, 1903, 1908, 1921, 1935, 1952, 1962, 1966, 2038, 2067, 2079, 2155, 2173, 2182, 2218, 2227, 2265, 2286, 2326, 2362, 2366, 2373, 2409, 2434, 2461, 2475, 2484, 2515, 2556, 2576, 2578, 2583, 2605, 2614, 2679, 2688, 2722, 2745, 2751, 2785, 2839, 2888, 2902, 2911, 2934, 2944, 2958, 2964, 2965, 2970, 2974, 3046, 3091, 3138, 3168, 3174, 3226, 3246, 3258, 3294, 3345, 3366, 3390, 3442, 3505, 3564, 3595, 3615, 3622, 3649, 3663, 3690, 3694, 3802, 3852, 3864, 3865, 3930, 3946, 3973, 4054, 4126, 4162, 4173, 4185, 4189, 4191, 4198, 4209, 4279, 4306, 4369, 4414, 4428, 4464, 4472, 4557, 4592, 4594, 4702, 4743, 4765, 4788, 4794, 4832, 4855, 4880, 4918, 4954, 4959, 4960, 4974, 4981, 5062, 5071, 5088, 5098, 5172, 5242, 5248, 5253, 5269, 5298, 5305, 5386, 5388, 5397, 5422, 5458, 5485, 5526, 5539, 5602, 5638, 5642, 5674, 5772, 5818, 5854, 5874, 5915, 5926, 5935, 5936, 5946, 5998, 6036, 6054, 6084, 6096, 6115, 6171, 6178, 6187, 6188, 6252, 6259, 6295, 6315, 6344, 6385, 6439, 6457, 6502, 6531, 6567, 6583, 6585, 6603, 6684, 6693, 6702, 6718, 6760, 6816, 6835, 6855, 6880, 6934, 6981, 7026, 7051, 7062, 7068, 7078, 7089, 7119, 7136, 7186, 7195, 7227, 7249, 7287, 7339, 7402, 7438, 7447, 7465, 7503, 7627, 7674, 7683, 7695, 7712, 7726, 7762, 7764, 7782, 7784, 7809, 7824, 7834, 7915, 7952, 7978, 8005, 8014, 8023, 8073, 8077, 8095, 8149, 8154, 8158, 8185, 8196, 8253, 8257, 8277, 8307, 8347, 8372, 8412, 8421, 8466, 8518, 8545, 8568, 8628, 8653, 8680, 8736, 8754, 8766, 8790, 8792, 8851, 8864, 8874, 8883, 8901, 8914, 9015, 9031, 9036, 9094, 9166, 9184, 9193, 9229, 9274, 9276, 9285, 9294, 9296, 9301, 9330, 9346, 9355, 9382, 9386, 9387, 9396, 9414, 9427, 9483, 9522, 9535, 9571, 9598, 9633, 9634, 9639, 9648, 9657, 9684, 9708, 9717, 9735, 9742, 9760, 9778, 9840, 9843, 9849, 9861, 9880, 9895, 9924, 9942, 9968, 9975, 9985]</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
Total[IntegerDigits[n]] == Total[IntegerDigits /@ Flatten[ConstantArray @@@ FactorInteger[n]],2];
Select[Range[2, 10000], smithQ]</
{{out}}
Line 3,082:
=={{header|Modula-2}}==
<
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
Line 3,141:
ReadChar;
END SmithNumbers.</
=={{header|Nim}}==
<
func primeFactors(n: int): seq[int] =
Line 3,183:
cnt = 0
stdout.write("\n")
echo()</
{{out}}
Line 3,227:
=={{header|Objeck}}==
<
class Test {
Line 3,276:
return sum;
}
}</
<pre>
Line 3,294:
=={{header|PARI/GP}}==
<
select(isSmith, [1..9999])</
{{out}}
<pre>%1 = [4, 22, 27, 58, 85, 94, 121, 166, 202, 265, 274, 319, 346, 355, 378, 382, 391, 438, 454, 483, 517, 526, 535, 562, 576, 588, 627, 634, 636, 645, 648, 654, 663, 666, 690, 706, 728, 729, 762, 778, 825, 852, 861, 895, 913, 915, 922, 958, 985, 1086, 1111, 1165, 1219, 1255, 1282, 1284, 1376, 1449, 1507, 1581, 1626, 1633, 1642, 1678, 1736, 1755, 1776, 1795, 1822, 1842, 1858, 1872, 1881, 1894, 1903, 1908, 1921, 1935, 1952, 1962, 1966, 2038, 2067, 2079, 2155, 2173, 2182, 2218, 2227, 2265, 2286, 2326, 2362, 2366, 2373, 2409, 2434, 2461, 2475, 2484, 2515, 2556, 2576, 2578, 2583, 2605, 2614, 2679, 2688, 2722, 2745, 2751, 2785, 2839, 2888, 2902, 2911, 2934, 2944, 2958, 2964, 2965, 2970, 2974, 3046, 3091, 3138, 3168, 3174, 3226, 3246, 3258, 3294, 3345, 3366, 3390, 3442, 3505, 3564, 3595, 3615, 3622, 3649, 3663, 3690, 3694, 3802, 3852, 3864, 3865, 3930, 3946, 3973, 4054, 4126, 4162, 4173, 4185, 4189, 4191, 4198, 4209, 4279, 4306, 4369, 4414, 4428, 4464, 4472, 4557, 4592, 4594, 4702, 4743, 4765, 4788, 4794, 4832, 4855, 4880, 4918, 4954, 4959, 4960, 4974, 4981, 5062, 5071, 5088, 5098, 5172, 5242, 5248, 5253, 5269, 5298, 5305, 5386, 5388, 5397, 5422, 5458, 5485, 5526, 5539, 5602, 5638, 5642, 5674, 5772, 5818, 5854, 5874, 5915, 5926, 5935, 5936, 5946, 5998, 6036, 6054, 6084, 6096, 6115, 6171, 6178, 6187, 6188, 6252, 6259, 6295, 6315, 6344, 6385, 6439, 6457, 6502, 6531, 6567, 6583, 6585, 6603, 6684, 6693, 6702, 6718, 6760, 6816, 6835, 6855, 6880, 6934, 6981, 7026, 7051, 7062, 7068, 7078, 7089, 7119, 7136, 7186, 7195, 7227, 7249, 7287, 7339, 7402, 7438, 7447, 7465, 7503, 7627, 7674, 7683, 7695, 7712, 7726, 7762, 7764, 7782, 7784, 7809, 7824, 7834, 7915, 7952, 7978, 8005, 8014, 8023, 8073, 8077, 8095, 8149, 8154, 8158, 8185, 8196, 8253, 8257, 8277, 8307, 8347, 8372, 8412, 8421, 8466, 8518, 8545, 8568, 8628, 8653, 8680, 8736, 8754, 8766, 8790, 8792, 8851, 8864, 8874, 8883, 8901, 8914, 9015, 9031, 9036, 9094, 9166, 9184, 9193, 9229, 9274, 9276, 9285, 9294, 9296, 9301, 9330, 9346, 9355, 9382, 9386, 9387, 9396, 9414, 9427, 9483, 9522, 9535, 9571, 9598, 9633, 9634, 9639, 9648, 9657, 9684, 9708, 9717, 9735, 9742, 9760, 9778, 9840, 9843, 9849, 9861, 9880, 9895, 9924, 9942, 9968, 9975, 9985]</pre>
Line 3,301:
{{works with|PARI/GP|2.6.0+}}
2.6.0 introduced the <code>forcomposite</code> iterator, removing the need to check each term for primality.
<
{{works with|PARI/GP|2.10.0+}}
2.10.0 gave us <code>forfactored</code> which speeds the process up by sieving for factors.
<
=={{header|Pascal}}==
Line 3,314:
the function IncDgtSum delivers the next sum of digits very fast (2.6 s for 1 to 1e9 )
<
{$IFDEF FPC}
{$MODE objFPC} //result and useful for x64
Line 3,596:
write(s:8,' smith-numbers up to ',actualNo.dgtnum:10);
end.
</
<pre>64-Bit FPC 3.1.1 -O3 -Xs i4330 3.5 Ghz
6 smith-numbers up to 100
Line 3,614:
=={{header|Perl}}==
{{libheader|ntheory}}
<
my @smith;
forcomposites {
Line 3,620:
} 10000-1;
say scalar(@smith), " Smith numbers below 10000.";
say "@smith";</
{{out}}
<pre>376 Smith numbers below 10000.
Line 3,627:
{{works with|ntheory|0.71+}}
Version 0.71 of the <code>ntheory</code> module added <code>forfactored</code>, similar to Pari/GP's 2.10.0 addition. For large inputs this can halve the time taken compared to <code>forcomposites</code>.
<
my $t=0;
forfactored { $t++ if @_ > 1 && sumdigits($_) == sumdigits(join "",@_); } 10**8;
say $t;</
=={{header|Phix}}==
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">sum_digits</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">=</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
Line 3,654:
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10000</span><span style="color: #0000FF;">),</span><span style="color: #000000;">smith</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">sprint</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;">"%d smith numbers found: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">),</span><span style="color: #008000;">", "</span><span style="color: #0000FF;">)})</span>
<!--</
{{out}}
<pre>
Line 3,661:
=={{header|PicoLisp}}==
<
(make
(let (D 2 L (1 2 2 . (4 2 4 2 4 6 2 6 .)) M (sqrt N))
Line 3,682:
(println 'first-10 (head 10 L))
(println 'last-10 (tail 10 L))
(println 'all (length L)) )</
{{out}}
<pre>first-10 (4 22 27 58 85 94 121 166 202 265)
Line 3,689:
=={{header|PL/I}}==
<
/* find the digit sum of N */
digitSum: procedure(nn) returns(fixed);
Line 3,748:
end;
put skip list('Found', cnt, 'Smith numbers.');
end smith;</
{{out}}
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382
Line 3,777:
=={{header|PL/M}}==
<
/* CP/M BDOS FUNCTIONS */
Line 3,866:
CALL PRINT(.' SMITH NUMBERS.$');
CALL EXIT;
EOF</
{{out}}
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382
Line 3,895:
=={{header|PureBasic}}==
<
#ECHO=#True ; #True: Print all results
Global NewList f.i()
Line 3,975:
Next
Print(~"\n"+Str(sn)+" Smith number up to "+Str(upto))
Return</
{{out}}
<pre>.
Line 4,011:
=={{header|Python}}==
===Procedural===
<
Line 4,058:
# entry point
list_smith_numbers(10_000)</
{{out}}<pre>
4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 391 438 454 483 517 526 535 562 576 588 627 634 636 645 648 654 663 666
Line 4,067:
===Functional===
{{Works with|Python|3.7}}
<
from itertools import dropwhile
Line 4,177:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>Count of Smith Numbers below 10k:
Line 4,189:
=={{header|Racket}}==
<
(require math/number-theory)
Line 4,215:
(let-values (([l r] (split-at ns (min (length ns) 15))))
(displayln l)
(loop r)))))</
{{out}}
Line 4,228:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
multi factors ( 1 ) { 1 }
Line 4,256:
say "{@s.elems} Smith numbers below 10_000";
say 'First 10: ', @s[ ^10 ];
say 'Last 10: ', @s[ *-10 .. * ];</
{{out}}
<pre>376 Smith numbers below 10_000
Line 4,264:
=={{header|REXX}}==
===unoptimized===
<
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N=10000 /*Not specified? Then use the default.*/
Line 4,294:
if z\==1 then do; f=f+1; $=$+sumD(z); end /*Residual? Then add Z*/
if f<2 then return 0 /*Prime? Not a Smith#*/
return $ /*else return sum digs.*/</
{{out|output|text= when using the default input:}}
Line 4,316:
This REXX version uses a faster version of the '''sumFactr''' function; it's over '''20''' times faster than the
<br>unoptimized version using a (negative) one million for '''N'''.
<
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N=10000 /*Not specified? Then use the default.*/
Line 4,353:
if z\==1 then do; f=f+1; $=$+sumD(z); end /*if a residual, then add Z*/
if f<2 then return 0 /*Is prime? It's not Smith#*/
return $ /*else, return sum of digs.*/</
{{out|output|text= when using the input of (negative) one million: <tt> -1000000 </tt>}}
<pre>
Line 4,363:
{{incorrect|Ring| <br><br> This program does not find (nor show) all the Smith numbers <big> < </big> 10,000. <br><br>}}
<
# Project : Smith numbers
Line 4,419:
end
return sum
</syntaxhighlight>
Output:
<pre>
Line 4,427:
=={{header|Ruby}}==
<
class Integer
Line 4,442:
puts "#{res.size} smith numbers below #{n}:
#{res.first(5).join(", ")},... #{res.last(5).join(", ")}"</
{{out}}
<pre>
Line 4,450:
=={{header|Rust}}==
<
//We just need the primes below 100
let primes = vec![2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];
Line 4,477:
}
println!("Smith numbers below 10000 ({}) : {:?}",solution.len(), solution);
}</
{{out}}
Line 4,487:
=={{header|Scala}}==
<
def sumDigits(_n: Int): Int = {
Line 4,528:
}
}</
=={{header|Sidef}}==
{{trans|Raku}}
<
static primes = Hash()
var p = 2
Line 4,567:
say "#{s.len} Smith numbers below 10_000"
say "First 10: #{s.first(10)}"
say "Last 10: #{s.last(10)}"</
{{out}}
<pre>
Line 4,576:
=={{header|Stata}}==
<
n = _n
a = J(14, 2, .)
Line 4,648:
+-----------------------------------------------------------------------+
1 | 9843 9849 9861 9880 9895 9924 9942 9968 9975 9985 |
+-----------------------------------------------------------------------+</
=={{header|Swift}}==
<
@inlinable
public var isSmith: Bool {
Line 4,699:
print("First 10 smith numbers: \(Array(smiths.prefix(10)))")
print("Last 10 smith numbers below 10,000: \(Array(smiths.suffix(10)))")
</syntaxhighlight>
{{out}}
Line 4,708:
=={{header|Tcl}}==
<
# list the prime factors of x in ascending order
set result [list]
Line 4,749:
puts ...[lrange $smiths end-12 end]
puts "([llength $smiths] total)"
</syntaxhighlight>
{{out}}
Line 4,758:
=={{header|Vlang}}==
{{trans|Go}}
<
mut p := 2
mut pf := 0
Line 4,841:
println('')
}
</syntaxhighlight>
{{out}}<pre>
All the Smith Numbers less than 10000 are:
Line 4,851:
{{libheader|Wren-fmt}}
{{libheader|Wren-seq}}
<
import "/fmt" for Fmt
import "/seq" for Lst
Line 4,874:
}
}
for (chunk in Lst.chunks(smiths, 16)) Fmt.print("$4d", chunk)</
{{out}}
Line 4,906:
=={{header|XPL0}}==
<
int N, S;
[S:= 0;
Line 4,937:
if rem(C/20) = 0 then CrLf(0);
];
]</
{{out}}
Line 4,964:
=={{header|zkl}}==
Uses the code (primeFactors) from [[Prime decomposition#zkl]].
<
[2..N].filter(fcn(n){
(pfs:=primeFactors(n)).len()>1 and
n.split().sum(0)==primeFactors(n).apply("split").flatten().sum(0)
})
}</
<
sns.toString(*).println(" ",sns.len()," numbers");</
{{out}}
<pre>
|