Smith numbers: Difference between revisions
Content added Content deleted
No edit summary |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 30: | Line 30: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F factors(=n) |
||
[Int] rt |
[Int] rt |
||
V f = 2 |
V f = 2 |
||
Line 77: | Line 77: | ||
print() |
print() |
||
print(‘Last 12 Smith Numbers below 10000:’) |
print(‘Last 12 Smith Numbers below 10000:’) |
||
print_elements(sn[(len)-12..])</ |
print_elements(sn[(len)-12..])</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 92: | Line 92: | ||
=={{header|360 Assembly}}== |
=={{header|360 Assembly}}== |
||
{{trans|Rexx}} |
{{trans|Rexx}} |
||
< |
<syntaxhighlight lang="360asm">* Smith numbers - 02/05/2017 |
||
SMITHNUM CSECT |
SMITHNUM CSECT |
||
USING SMITHNUM,R13 base register |
USING SMITHNUM,R13 base register |
||
Line 238: | Line 238: | ||
XDEC DS CL12 temp |
XDEC DS CL12 temp |
||
YREGS |
YREGS |
||
END SMITHNUM</ |
END SMITHNUM</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 270: | Line 270: | ||
=={{header|Action!}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang="action!">CARD FUNC SumDigits(CARD n) |
||
CARD res,a |
CARD res,a |
||
Line 320: | Line 320: | ||
Poke(77,0) ;turn off the attract mode |
Poke(77,0) ;turn off the attract mode |
||
OD |
OD |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Smith_numbers.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Smith_numbers.png Screenshot from Atari 8-bit computer] |
||
Line 344: | Line 344: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
{{works with|Ada|2012}} |
{{works with|Ada|2012}} |
||
< |
<syntaxhighlight lang="ada"> |
||
with Ada.Text_IO; |
with Ada.Text_IO; |
||
Line 372: | Line 372: | ||
end loop; |
end loop; |
||
end smith; |
end smith; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
< |
<syntaxhighlight lang="algol68"># sieve of Eratosthene: sets s[i] to TRUE if i is prime, FALSE otherwise # |
||
PROC sieve = ( REF[]BOOL s )VOID: |
PROC sieve = ( REF[]BOOL s )VOID: |
||
BEGIN |
BEGIN |
||
Line 447: | Line 447: | ||
OD; |
OD; |
||
print( ( newline, "THere are ", whole( smith count, -7 ), " Smith numbers below ", whole( max number, -7 ), newline ) ) |
print( ( newline, "THere are ", whole( smith count, -7 ), " Smith numbers below ", whole( max number, -7 ), newline ) ) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 460: | Line 460: | ||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
< |
<syntaxhighlight lang="rebol">digitSum: function [v][ |
||
n: new v |
n: new v |
||
result: new 0 |
result: new 0 |
||
Line 483: | Line 483: | ||
] |
] |
||
] |
] |
||
print ""</ |
print ""</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 527: | Line 527: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f SMITH_NUMBERS.AWK |
# syntax: GAWK -f SMITH_NUMBERS.AWK |
||
# converted from C |
# converted from C |
||
Line 605: | Line 605: | ||
return(sum) |
return(sum) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 636: | Line 636: | ||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
< |
<syntaxhighlight lang="gwbasic">10 DEFINT A-Z |
||
20 DIM F(32) |
20 DIM F(32) |
||
30 FOR I=2 TO 9999 |
30 FOR I=2 TO 9999 |
||
Line 656: | Line 656: | ||
190 NEXT |
190 NEXT |
||
200 PRINT |
200 PRINT |
||
210 PRINT "Found";C;"Smith numbers."</ |
210 PRINT "Found";C;"Smith numbers."</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 |
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 |
||
Line 685: | Line 685: | ||
=={{header|BCPL}}== |
=={{header|BCPL}}== |
||
< |
<syntaxhighlight lang="bcpl">get "libhdr" |
||
// Find the sum of the digits of N |
// Find the sum of the digits of N |
||
Line 736: | Line 736: | ||
$) |
$) |
||
writef("*NFound %N Smith numbers.*N", count) |
writef("*NFound %N Smith numbers.*N", count) |
||
$)</ |
$)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 |
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 |
||
Line 766: | Line 766: | ||
=={{header|C}}== |
=={{header|C}}== |
||
{{trans|C++}} |
{{trans|C++}} |
||
<syntaxhighlight lang="c"> |
|||
<lang c> |
|||
#include <stdlib.h> |
#include <stdlib.h> |
||
#include <stdio.h> |
#include <stdio.h> |
||
Line 845: | Line 845: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 877: | Line 877: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
{{trans|java}} |
{{trans|java}} |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
Line 933: | Line 933: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 4 22 27 58 85 94 166 202 |
<pre> 4 22 27 58 85 94 166 202 |
||
Line 984: | Line 984: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp"> |
||
#include <iostream> |
#include <iostream> |
||
#include <vector> |
#include <vector> |
||
Line 1,030: | Line 1,030: | ||
return 0; |
return 0; |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,041: | Line 1,041: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
< |
<syntaxhighlight lang="clojure">(defn divisible? [a b] |
||
(zero? (mod a b))) |
(zero? (mod a b))) |
||
Line 1,066: | Line 1,066: | ||
(sum-digits (clojure.string/join "" (prime-factors n)))))) |
(sum-digits (clojure.string/join "" (prime-factors n)))))) |
||
(filter smith-number? (range 1 10000))</ |
(filter smith-number? (range 1 10000))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,076: | Line 1,076: | ||
=={{header|CLU}}== |
=={{header|CLU}}== |
||
< |
<syntaxhighlight lang="clu">% Get all digits of a number |
||
digits = iter (n: int) yields (int) |
digits = iter (n: int) yields (int) |
||
while n > 0 do |
while n > 0 do |
||
Line 1,137: | Line 1,137: | ||
end |
end |
||
stream$putl(po, "\nFound " || int$unparse(count) || " Smith numbers.") |
stream$putl(po, "\nFound " || int$unparse(count) || " Smith numbers.") |
||
end start_up</ |
end start_up</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 |
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 |
||
Line 1,166: | Line 1,166: | ||
=={{header|Cowgol}}== |
=={{header|Cowgol}}== |
||
< |
<syntaxhighlight lang="cowgol">include "cowgol.coh"; |
||
typedef N is uint16; # 16-bit math is good enough |
typedef N is uint16; # 16-bit math is good enough |
||
Line 1,247: | Line 1,247: | ||
print("Found "); |
print("Found "); |
||
print_i32(count as uint32); |
print_i32(count as uint32); |
||
print(" Smith numbers.\n");</ |
print(" Smith numbers.\n");</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 |
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 |
||
Line 1,277: | Line 1,277: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Java}} mostly |
{{trans|Java}} mostly |
||
< |
<syntaxhighlight lang="d">import std.stdio; |
||
void main() { |
void main() { |
||
Line 1,329: | Line 1,329: | ||
} |
} |
||
return sum; |
return sum; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,374: | Line 1,374: | ||
See [https://rosettacode.org/wiki/Smith_numbers#Pascal Pascal]. |
See [https://rosettacode.org/wiki/Smith_numbers#Pascal Pascal]. |
||
=={{header|Draco}}== |
=={{header|Draco}}== |
||
< |
<syntaxhighlight lang="draco">/* Find the sum of the digits of a number */ |
||
proc nonrec digitsum(word n) word: |
proc nonrec digitsum(word n) word: |
||
word sum; |
word sum; |
||
Line 1,443: | Line 1,443: | ||
writeln(); |
writeln(); |
||
writeln("Found ", count, " Smith numbers.") |
writeln("Found ", count, " Smith numbers.") |
||
corp</ |
corp</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 |
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 |
||
Line 1,472: | Line 1,472: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
< |
<syntaxhighlight lang="elixir">defmodule Smith do |
||
def number?(n) do |
def number?(n) do |
||
d = decomposition(n) |
d = decomposition(n) |
||
Line 1,492: | Line 1,492: | ||
IO.puts "#{length(smith)} smith numbers below #{m}:" |
IO.puts "#{length(smith)} smith numbers below #{m}:" |
||
IO.puts "First 10: #{Enum.take(smith,10) |> Enum.join(", ")}" |
IO.puts "First 10: #{Enum.take(smith,10) |> Enum.join(", ")}" |
||
IO.puts "Last 10: #{Enum.take(smith,-10) |> Enum.join(", ")}"</ |
IO.puts "Last 10: #{Enum.take(smith,-10) |> Enum.join(", ")}"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,503: | Line 1,503: | ||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)] |
This task uses [http://www.rosettacode.org/wiki/Extensible_prime_generator#The_function Extensible Prime Generator (F#)] |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Generate Smith Numbers. Nigel Galloway: November 6th., 2020 |
// 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 |
let fN g=Seq.unfold(fun n->match n with 0->None |_->Some(n%10,n/10)) g |> Seq.sum |
||
Line 1,510: | 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.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 "") |
|>Seq.chunkBySize 20|>Seq.iter(fun n->Seq.iter(printf "%4d ") n; printfn "") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,534: | Line 1,534: | ||
</pre> |
</pre> |
||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
<lang>USING: formatting grouping io kernel math.primes.factors |
<syntaxhighlight lang="text">USING: formatting grouping io kernel math.primes.factors |
||
math.ranges math.text.utils sequences sequences.deep ; |
math.ranges math.text.utils sequences sequences.deep ; |
||
Line 1,545: | Line 1,545: | ||
10,000 [1,b] [ smith? ] filter 10 group |
10,000 [1,b] [ smith? ] filter 10 group |
||
[ [ "%4d " printf ] each nl ] each</ |
[ [ "%4d " printf ] each nl ] each</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,595: | 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]] |
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. < |
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. <syntaxhighlight lang="fortran"> MODULE FACTORISE !Produce a little list... |
||
USE PRIMEBAG !This is a common need. |
USE PRIMEBAG !This is a common need. |
||
INTEGER LASTP !Some size allowances. |
INTEGER LASTP !Some size allowances. |
||
Line 1,719: | Line 1,719: | ||
13 FORMAT (I9," found.") !Just in case. |
13 FORMAT (I9," found.") !Just in case. |
||
END DO !On to the next base. |
END DO !On to the next base. |
||
END !That was strange.</ |
END !That was strange.</syntaxhighlight> |
||
Output: selecting the base ten result: |
Output: selecting the base ten result: |
||
Line 1,767: | Line 1,767: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
< |
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64 |
||
Sub getPrimeFactors(factors() As UInteger, n As UInteger) |
Sub getPrimeFactors(factors() As UInteger, n As UInteger) |
||
Line 1,821: | Line 1,821: | ||
Print |
Print |
||
Print "Press any key to quit" |
Print "Press any key to quit" |
||
Sleep</ |
Sleep</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,865: | Line 1,865: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|C}} |
{{trans|C}} |
||
<syntaxhighlight lang="go"> |
|||
<lang Go> |
|||
package main |
package main |
||
Line 1,949: | Line 1,949: | ||
fmt.Println() |
fmt.Println() |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}}<pre> |
{{out}}<pre> |
||
All the Smith Numbers less than 10000 are: |
All the Smith Numbers less than 10000 are: |
||
Line 1,956: | Line 1,956: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.Numbers.Primes (primeFactors) |
||
import Data.List (unfoldr) |
import Data.List (unfoldr) |
||
import Data.Tuple (swap) |
import Data.Tuple (swap) |
||
Line 1,989: | Line 1,989: | ||
, "\nLast 12 Smith Numbers below 10k:" |
, "\nLast 12 Smith Numbers below 10k:" |
||
, unwords (show <$> drop (lowSmithCount - 12) lowSmiths) |
, unwords (show <$> drop (lowSmithCount - 12) lowSmiths) |
||
]</ |
]</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Count of Smith Numbers below 10k: |
<pre>Count of Smith Numbers below 10k: |
||
Line 2,002: | Line 2,002: | ||
=={{header|J}}== |
=={{header|J}}== |
||
Implementation: |
Implementation: |
||
< |
<syntaxhighlight lang="j">digits=: 10&#.inv |
||
sumdig=: +/@,@digits |
sumdig=: +/@,@digits |
||
notprime=: -.@(1&p:) |
notprime=: -.@(1&p:) |
||
smith=: #~ notprime * (=&sumdig q:)every</ |
smith=: #~ notprime * (=&sumdig q:)every</syntaxhighlight> |
||
Task example: |
Task example: |
||
< |
<syntaxhighlight lang="j"> #smith }.i.10000 |
||
376 |
376 |
||
q:376 |
q:376 |
||
Line 2,059: | Line 2,059: | ||
9598 9633 9634 9639 9648 9657 9684 9708 |
9598 9633 9634 9639 9648 9657 9684 9708 |
||
9717 9735 9742 9760 9778 9840 9843 9849 |
9717 9735 9742 9760 9778 9840 9843 9849 |
||
9861 9880 9895 9924 9942 9968 9975 9985</ |
9861 9880 9895 9924 9942 9968 9975 9985</syntaxhighlight> |
||
(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.) |
(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: | Line 2,065: | ||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{works with|Java|7}} |
{{works with|Java|7}} |
||
< |
<syntaxhighlight lang="java">import java.util.*; |
||
public class SmithNumbers { |
public class SmithNumbers { |
||
Line 2,109: | Line 2,109: | ||
return sum; |
return sum; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
<pre>4 |
<pre>4 |
||
22 |
22 |
||
Line 2,128: | Line 2,128: | ||
{{Trans|Haskell}} |
{{Trans|Haskell}} |
||
{{Trans|Python}} |
{{Trans|Python}} |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
'use strict'; |
'use strict'; |
||
Line 2,297: | Line 2,297: | ||
return main(); |
return main(); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Count of Smith Numbers below 10k: |
<pre>Count of Smith Numbers below 10k: |
||
Line 2,313: | Line 2,313: | ||
''' Preliminaries''' |
''' Preliminaries''' |
||
< |
<syntaxhighlight lang="jq">def is_prime: |
||
. as $n |
. as $n |
||
| if ($n < 2) then false |
| if ($n < 2) then false |
||
Line 2,341: | Line 2,341: | ||
| select(($num % .) == 0 and is_prime) |
| select(($num % .) == 0 and is_prime) |
||
| m(.)); |
| m(.)); |
||
</syntaxhighlight> |
|||
</lang> |
|||
'''The task''' |
'''The task''' |
||
< |
<syntaxhighlight lang="jq"># input should be an integer |
||
def is_smith: |
def is_smith: |
||
def sumdigits: |
def sumdigits: |
||
Line 2,352: | Line 2,352: | ||
"Smith numbers up to 10000:\n", |
"Smith numbers up to 10000:\n", |
||
(range(1; 10000) | select(is_smith)) |
(range(1; 10000) | select(is_smith)) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,369: | Line 2,369: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia"># v0.6 |
||
function sumdigits(n::Integer) |
function sumdigits(n::Integer) |
||
Line 2,384: | Line 2,384: | ||
smithnumbers = collect(n for n in 2:10000 if issmith(n)) |
smithnumbers = collect(n for n in 2:10000 if issmith(n)) |
||
println("Smith numbers up to 10000:\n$smithnumbers")</ |
println("Smith numbers up to 10000:\n$smithnumbers")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,412: | Line 2,412: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
< |
<syntaxhighlight lang="scala">// version 1.0.6 |
||
fun getPrimeFactors(n: Int): MutableList<Int> { |
fun getPrimeFactors(n: Int): MutableList<Int> { |
||
Line 2,461: | Line 2,461: | ||
} |
} |
||
println("\n\n$count numbers found") |
println("\n\n$count numbers found") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,495: | Line 2,495: | ||
=={{header|Lua}}== |
=={{header|Lua}}== |
||
Slightly long-winded prime factor function but it's a bit faster than the 'easy' way. |
Slightly long-winded prime factor function but it's a bit faster than the 'easy' way. |
||
< |
<syntaxhighlight lang="lua">-- Returns a boolean indicating whether n is prime |
||
function isPrime (n) |
function isPrime (n) |
||
if n < 2 then return false end |
if n < 2 then return false end |
||
Line 2,545: | Line 2,545: | ||
for n = 1, 10000 do |
for n = 1, 10000 do |
||
if isSmith(n) then io.write(n .. "\t") end |
if isSmith(n) then io.write(n .. "\t") end |
||
end</ |
end</syntaxhighlight> |
||
Seems silly to paste in all 376 numbers but rest assured the output agrees with https://oeis.org/A006753 |
Seems silly to paste in all 376 numbers but rest assured the output agrees with https://oeis.org/A006753 |
||
Line 2,559: | 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) |
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"> |
|||
<lang M2000 Interpreter> |
|||
Module Checkit { |
Module Checkit { |
||
Set Fast ! |
Set Fast ! |
||
Line 2,617: | Line 2,617: | ||
} |
} |
||
Checkit |
Checkit |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|MAD}}== |
=={{header|MAD}}== |
||
< |
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER |
||
PRINT COMMENT$ SMITH NUMBERS$ |
PRINT COMMENT$ SMITH NUMBERS$ |
||
Line 2,672: | Line 2,672: | ||
TRANSFER TO LOOP |
TRANSFER TO LOOP |
||
END OF FUNCTION |
END OF FUNCTION |
||
END OF PROGRAM</ |
END OF PROGRAM</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,054: | Line 3,054: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">isSmith := proc(n::posint) |
||
local factors, sumofDigits, sumofFactorDigits, x; |
local factors, sumofDigits, sumofFactorDigits, x; |
||
if isprime(n) then |
if isprime(n) then |
||
Line 3,069: | Line 3,069: | ||
end proc: |
end proc: |
||
findSmith(10000);</ |
findSmith(10000);</syntaxhighlight> |
||
{{out}} |
{{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> |
<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}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">smithQ[n_] := Not[PrimeQ[n]] && |
||
Total[IntegerDigits[n]] == Total[IntegerDigits /@ Flatten[ConstantArray @@@ FactorInteger[n]],2]; |
Total[IntegerDigits[n]] == Total[IntegerDigits /@ Flatten[ConstantArray @@@ FactorInteger[n]],2]; |
||
Select[Range[2, 10000], smithQ]</ |
Select[Range[2, 10000], smithQ]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,082: | Line 3,082: | ||
=={{header|Modula-2}}== |
=={{header|Modula-2}}== |
||
< |
<syntaxhighlight lang="modula2">MODULE SmithNumbers; |
||
FROM FormatString IMPORT FormatString; |
FROM FormatString IMPORT FormatString; |
||
FROM Terminal IMPORT WriteString,WriteLn,ReadChar; |
FROM Terminal IMPORT WriteString,WriteLn,ReadChar; |
||
Line 3,141: | Line 3,141: | ||
ReadChar; |
ReadChar; |
||
END SmithNumbers.</ |
END SmithNumbers.</syntaxhighlight> |
||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">import strformat |
||
func primeFactors(n: int): seq[int] = |
func primeFactors(n: int): seq[int] = |
||
Line 3,183: | Line 3,183: | ||
cnt = 0 |
cnt = 0 |
||
stdout.write("\n") |
stdout.write("\n") |
||
echo()</ |
echo()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,227: | Line 3,227: | ||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
< |
<syntaxhighlight lang="objeck">use Collection; |
||
class Test { |
class Test { |
||
Line 3,276: | Line 3,276: | ||
return sum; |
return sum; |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
<pre> |
<pre> |
||
Line 3,294: | Line 3,294: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
< |
<syntaxhighlight lang="parigp">isSmith(n)=my(f=factor(n)); if(#f~==1 && f[1,2]==1, return(0)); sum(i=1, #f~, sumdigits(f[i, 1])*f[i, 2]) == sumdigits(n); |
||
select(isSmith, [1..9999])</ |
select(isSmith, [1..9999])</syntaxhighlight> |
||
{{out}} |
{{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> |
<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: | Line 3,301: | ||
{{works with|PARI/GP|2.6.0+}} |
{{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. |
2.6.0 introduced the <code>forcomposite</code> iterator, removing the need to check each term for primality. |
||
< |
<syntaxhighlight lang="parigp">forcomposite(n=4,9999, f=factor(n); if(#f~==1 && f[1,2]==1, next); if(sum(i=1, #f~, sumdigits(f[i, 1])*f[i, 2]) == sumdigits(n), print1(n" ")))</syntaxhighlight> |
||
{{works with|PARI/GP|2.10.0+}} |
{{works with|PARI/GP|2.10.0+}} |
||
2.10.0 gave us <code>forfactored</code> which speeds the process up by sieving for factors. |
2.10.0 gave us <code>forfactored</code> which speeds the process up by sieving for factors. |
||
< |
<syntaxhighlight lang="parigp">forfactored(n=4,9999, f=n[2]; if(#f~==1 && f[1,2]==1, next); if(sum(i=1, #f~, sumdigits(f[i, 1])*f[i, 2]) == sumdigits(n[1]), print1(n[1]" ")))</syntaxhighlight> |
||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
Line 3,314: | Line 3,314: | ||
the function IncDgtSum delivers the next sum of digits very fast (2.6 s for 1 to 1e9 ) |
the function IncDgtSum delivers the next sum of digits very fast (2.6 s for 1 to 1e9 ) |
||
< |
<syntaxhighlight lang="pascal">program SmithNum; |
||
{$IFDEF FPC} |
{$IFDEF FPC} |
||
{$MODE objFPC} //result and useful for x64 |
{$MODE objFPC} //result and useful for x64 |
||
Line 3,596: | Line 3,596: | ||
write(s:8,' smith-numbers up to ',actualNo.dgtnum:10); |
write(s:8,' smith-numbers up to ',actualNo.dgtnum:10); |
||
end. |
end. |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre>64-Bit FPC 3.1.1 -O3 -Xs i4330 3.5 Ghz |
<pre>64-Bit FPC 3.1.1 -O3 -Xs i4330 3.5 Ghz |
||
6 smith-numbers up to 100 |
6 smith-numbers up to 100 |
||
Line 3,614: | Line 3,614: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
< |
<syntaxhighlight lang="perl">use ntheory qw/:all/; |
||
my @smith; |
my @smith; |
||
forcomposites { |
forcomposites { |
||
Line 3,620: | Line 3,620: | ||
} 10000-1; |
} 10000-1; |
||
say scalar(@smith), " Smith numbers below 10000."; |
say scalar(@smith), " Smith numbers below 10000."; |
||
say "@smith";</ |
say "@smith";</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>376 Smith numbers below 10000. |
<pre>376 Smith numbers below 10000. |
||
Line 3,627: | Line 3,627: | ||
{{works with|ntheory|0.71+}} |
{{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>. |
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>. |
||
< |
<syntaxhighlight lang="perl">use ntheory ":all"; |
||
my $t=0; |
my $t=0; |
||
forfactored { $t++ if @_ > 1 && sumdigits($_) == sumdigits(join "",@_); } 10**8; |
forfactored { $t++ if @_ > 1 && sumdigits($_) == sumdigits(join "",@_); } 10**8; |
||
say $t;</ |
say $t;</syntaxhighlight> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<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> |
<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: | 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: #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> |
<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> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,661: | Line 3,661: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(de factor (N) |
||
(make |
(make |
||
(let (D 2 L (1 2 2 . (4 2 4 2 4 6 2 6 .)) M (sqrt N)) |
(let (D 2 L (1 2 2 . (4 2 4 2 4 6 2 6 .)) M (sqrt N)) |
||
Line 3,682: | Line 3,682: | ||
(println 'first-10 (head 10 L)) |
(println 'first-10 (head 10 L)) |
||
(println 'last-10 (tail 10 L)) |
(println 'last-10 (tail 10 L)) |
||
(println 'all (length L)) )</ |
(println 'all (length L)) )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>first-10 (4 22 27 58 85 94 121 166 202 265) |
<pre>first-10 (4 22 27 58 85 94 121 166 202 265) |
||
Line 3,689: | Line 3,689: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang="pli">smith: procedure options(main); |
||
/* find the digit sum of N */ |
/* find the digit sum of N */ |
||
digitSum: procedure(nn) returns(fixed); |
digitSum: procedure(nn) returns(fixed); |
||
Line 3,748: | Line 3,748: | ||
end; |
end; |
||
put skip list('Found', cnt, 'Smith numbers.'); |
put skip list('Found', cnt, 'Smith numbers.'); |
||
end smith;</ |
end smith;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 |
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 |
||
Line 3,777: | Line 3,777: | ||
=={{header|PL/M}}== |
=={{header|PL/M}}== |
||
< |
<syntaxhighlight lang="pli">100H: |
||
/* CP/M BDOS FUNCTIONS */ |
/* CP/M BDOS FUNCTIONS */ |
||
Line 3,866: | Line 3,866: | ||
CALL PRINT(.' SMITH NUMBERS.$'); |
CALL PRINT(.' SMITH NUMBERS.$'); |
||
CALL EXIT; |
CALL EXIT; |
||
EOF</ |
EOF</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 |
<pre> 4 22 27 58 85 94 121 166 202 265 274 319 346 355 378 382 |
||
Line 3,895: | Line 3,895: | ||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
< |
<syntaxhighlight lang="purebasic">DisableDebugger |
||
#ECHO=#True ; #True: Print all results |
#ECHO=#True ; #True: Print all results |
||
Global NewList f.i() |
Global NewList f.i() |
||
Line 3,975: | Line 3,975: | ||
Next |
Next |
||
Print(~"\n"+Str(sn)+" Smith number up to "+Str(upto)) |
Print(~"\n"+Str(sn)+" Smith number up to "+Str(upto)) |
||
Return</ |
Return</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>. |
<pre>. |
||
Line 4,011: | Line 4,011: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
===Procedural=== |
===Procedural=== |
||
< |
<syntaxhighlight lang="python">from sys import stdout |
||
Line 4,058: | Line 4,058: | ||
# entry point |
# entry point |
||
list_smith_numbers(10_000)</ |
list_smith_numbers(10_000)</syntaxhighlight> |
||
{{out}}<pre> |
{{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 |
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: | Line 4,067: | ||
===Functional=== |
===Functional=== |
||
{{Works with|Python|3.7}} |
{{Works with|Python|3.7}} |
||
< |
<syntaxhighlight lang="python">'''Smith numbers''' |
||
from itertools import dropwhile |
from itertools import dropwhile |
||
Line 4,177: | Line 4,177: | ||
# MAIN --- |
# MAIN --- |
||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
main()</ |
main()</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Count of Smith Numbers below 10k: |
<pre>Count of Smith Numbers below 10k: |
||
Line 4,189: | Line 4,189: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(require math/number-theory) |
(require math/number-theory) |
||
Line 4,215: | Line 4,215: | ||
(let-values (([l r] (split-at ns (min (length ns) 15)))) |
(let-values (([l r] (split-at ns (min (length ns) 15)))) |
||
(displayln l) |
(displayln l) |
||
(loop r)))))</ |
(loop r)))))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,228: | Line 4,228: | ||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
(formerly Perl 6) |
(formerly Perl 6) |
||
<lang |
<syntaxhighlight lang="raku" line>constant @primes = 2, |(3, 5, 7 ... *).grep: *.is-prime; |
||
multi factors ( 1 ) { 1 } |
multi factors ( 1 ) { 1 } |
||
Line 4,256: | Line 4,256: | ||
say "{@s.elems} Smith numbers below 10_000"; |
say "{@s.elems} Smith numbers below 10_000"; |
||
say 'First 10: ', @s[ ^10 ]; |
say 'First 10: ', @s[ ^10 ]; |
||
say 'Last 10: ', @s[ *-10 .. * ];</ |
say 'Last 10: ', @s[ *-10 .. * ];</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>376 Smith numbers below 10_000 |
<pre>376 Smith numbers below 10_000 |
||
Line 4,264: | Line 4,264: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
===unoptimized=== |
===unoptimized=== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program finds (and maybe displays) Smith (or joke) numbers up to a given N.*/ |
||
parse arg N . /*obtain optional argument from the CL.*/ |
parse arg N . /*obtain optional argument from the CL.*/ |
||
if N=='' | N=="," then N=10000 /*Not specified? Then use the default.*/ |
if N=='' | N=="," then N=10000 /*Not specified? Then use the default.*/ |
||
Line 4,294: | Line 4,294: | ||
if z\==1 then do; f=f+1; $=$+sumD(z); end /*Residual? Then add Z*/ |
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#*/ |
if f<2 then return 0 /*Prime? Not a Smith#*/ |
||
return $ /*else return sum digs.*/</ |
return $ /*else return sum digs.*/</syntaxhighlight> |
||
{{out|output|text= when using the default input:}} |
{{out|output|text= when using the default input:}} |
||
Line 4,316: | Line 4,316: | ||
This REXX version uses a faster version of the '''sumFactr''' function; it's over '''20''' times faster than the |
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'''. |
<br>unoptimized version using a (negative) one million for '''N'''. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program finds (and maybe displays) Smith (or joke) numbers up to a given N.*/ |
||
parse arg N . /*obtain optional argument from the CL.*/ |
parse arg N . /*obtain optional argument from the CL.*/ |
||
if N=='' | N=="," then N=10000 /*Not specified? Then use the default.*/ |
if N=='' | N=="," then N=10000 /*Not specified? Then use the default.*/ |
||
Line 4,353: | Line 4,353: | ||
if z\==1 then do; f=f+1; $=$+sumD(z); end /*if a residual, then add Z*/ |
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#*/ |
if f<2 then return 0 /*Is prime? It's not Smith#*/ |
||
return $ /*else, return sum of digs.*/</ |
return $ /*else, return sum of digs.*/</syntaxhighlight> |
||
{{out|output|text= when using the input of (negative) one million: <tt> -1000000 </tt>}} |
{{out|output|text= when using the input of (negative) one million: <tt> -1000000 </tt>}} |
||
<pre> |
<pre> |
||
Line 4,363: | Line 4,363: | ||
{{incorrect|Ring| <br><br> This program does not find (nor show) all the Smith numbers <big> < </big> 10,000. <br><br>}} |
{{incorrect|Ring| <br><br> This program does not find (nor show) all the Smith numbers <big> < </big> 10,000. <br><br>}} |
||
< |
<syntaxhighlight lang="ring"> |
||
# Project : Smith numbers |
# Project : Smith numbers |
||
Line 4,419: | Line 4,419: | ||
end |
end |
||
return sum |
return sum |
||
</syntaxhighlight> |
|||
</lang> |
|||
Output: |
Output: |
||
<pre> |
<pre> |
||
Line 4,427: | Line 4,427: | ||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">require "prime" |
||
class Integer |
class Integer |
||
Line 4,442: | Line 4,442: | ||
puts "#{res.size} smith numbers below #{n}: |
puts "#{res.size} smith numbers below #{n}: |
||
#{res.first(5).join(", ")},... #{res.last(5).join(", ")}"</ |
#{res.first(5).join(", ")},... #{res.last(5).join(", ")}"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,450: | Line 4,450: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust">fn main () { |
||
//We just need the primes below 100 |
//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]; |
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: | Line 4,477: | ||
} |
} |
||
println!("Smith numbers below 10000 ({}) : {:?}",solution.len(), solution); |
println!("Smith numbers below 10000 ({}) : {:?}",solution.len(), solution); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,487: | Line 4,487: | ||
=={{header|Scala}}== |
=={{header|Scala}}== |
||
< |
<syntaxhighlight lang="scala">object SmithNumbers extends App { |
||
def sumDigits(_n: Int): Int = { |
def sumDigits(_n: Int): Int = { |
||
Line 4,528: | Line 4,528: | ||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
{{trans|Raku}} |
{{trans|Raku}} |
||
< |
<syntaxhighlight lang="ruby">var primes = Enumerator({ |callback| |
||
static primes = Hash() |
static primes = Hash() |
||
var p = 2 |
var p = 2 |
||
Line 4,567: | Line 4,567: | ||
say "#{s.len} Smith numbers below 10_000" |
say "#{s.len} Smith numbers below 10_000" |
||
say "First 10: #{s.first(10)}" |
say "First 10: #{s.first(10)}" |
||
say "Last 10: #{s.last(10)}"</ |
say "Last 10: #{s.last(10)}"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,576: | Line 4,576: | ||
=={{header|Stata}}== |
=={{header|Stata}}== |
||
< |
<syntaxhighlight lang="stata">function factor(_n) { |
||
n = _n |
n = _n |
||
a = J(14, 2, .) |
a = J(14, 2, .) |
||
Line 4,648: | Line 4,648: | ||
+-----------------------------------------------------------------------+ |
+-----------------------------------------------------------------------+ |
||
1 | 9843 9849 9861 9880 9895 9924 9942 9968 9975 9985 | |
1 | 9843 9849 9861 9880 9895 9924 9942 9968 9975 9985 | |
||
+-----------------------------------------------------------------------+</ |
+-----------------------------------------------------------------------+</syntaxhighlight> |
||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">extension BinaryInteger { |
||
@inlinable |
@inlinable |
||
public var isSmith: Bool { |
public var isSmith: Bool { |
||
Line 4,699: | Line 4,699: | ||
print("First 10 smith numbers: \(Array(smiths.prefix(10)))") |
print("First 10 smith numbers: \(Array(smiths.prefix(10)))") |
||
print("Last 10 smith numbers below 10,000: \(Array(smiths.suffix(10)))") |
print("Last 10 smith numbers below 10,000: \(Array(smiths.suffix(10)))") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 4,708: | Line 4,708: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">proc factors {x} { |
||
# list the prime factors of x in ascending order |
# list the prime factors of x in ascending order |
||
set result [list] |
set result [list] |
||
Line 4,749: | Line 4,749: | ||
puts ...[lrange $smiths end-12 end] |
puts ...[lrange $smiths end-12 end] |
||
puts "([llength $smiths] total)" |
puts "([llength $smiths] total)" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 4,758: | Line 4,758: | ||
=={{header|Vlang}}== |
=={{header|Vlang}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="vlang">fn num_prime_factors(xx int) int { |
||
mut p := 2 |
mut p := 2 |
||
mut pf := 0 |
mut pf := 0 |
||
Line 4,841: | Line 4,841: | ||
println('') |
println('') |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}}<pre> |
{{out}}<pre> |
||
All the Smith Numbers less than 10000 are: |
All the Smith Numbers less than 10000 are: |
||
Line 4,851: | Line 4,851: | ||
{{libheader|Wren-fmt}} |
{{libheader|Wren-fmt}} |
||
{{libheader|Wren-seq}} |
{{libheader|Wren-seq}} |
||
< |
<syntaxhighlight lang="ecmascript">import "/math" for Int |
||
import "/fmt" for Fmt |
import "/fmt" for Fmt |
||
import "/seq" for Lst |
import "/seq" for Lst |
||
Line 4,874: | Line 4,874: | ||
} |
} |
||
} |
} |
||
for (chunk in Lst.chunks(smiths, 16)) Fmt.print("$4d", chunk)</ |
for (chunk in Lst.chunks(smiths, 16)) Fmt.print("$4d", chunk)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,906: | Line 4,906: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">func SumDigits(N); \Return sum of digits in N |
||
int N, S; |
int N, S; |
||
[S:= 0; |
[S:= 0; |
||
Line 4,937: | Line 4,937: | ||
if rem(C/20) = 0 then CrLf(0); |
if rem(C/20) = 0 then CrLf(0); |
||
]; |
]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,964: | Line 4,964: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
Uses the code (primeFactors) from [[Prime decomposition#zkl]]. |
Uses the code (primeFactors) from [[Prime decomposition#zkl]]. |
||
< |
<syntaxhighlight lang="zkl">fcn smithNumbers(N=0d10_000){ // -->(Smith numbers to N) |
||
[2..N].filter(fcn(n){ |
[2..N].filter(fcn(n){ |
||
(pfs:=primeFactors(n)).len()>1 and |
(pfs:=primeFactors(n)).len()>1 and |
||
n.split().sum(0)==primeFactors(n).apply("split").flatten().sum(0) |
n.split().sum(0)==primeFactors(n).apply("split").flatten().sum(0) |
||
}) |
}) |
||
}</ |
}</syntaxhighlight> |
||
< |
<syntaxhighlight lang="zkl">sns:=smithNumbers(); |
||
sns.toString(*).println(" ",sns.len()," numbers");</ |
sns.toString(*).println(" ",sns.len()," numbers");</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |