Factorions: Difference between revisions
Content added Content deleted
m (→{{header|J}}) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 33:
{{trans|Python}}
<
L(n) 1..11
fact.append(fact[n-1] * n)
Line 48:
I fact_sum == i
print(i, end' ‘ ’)
print("\n")</
{{out}}
Line 67:
=={{header|360 Assembly}}==
<
FACTORIO CSECT
USING FACTORIO,R13 base register
Line 127:
XDEC DS CL12 temp fo xdeco
REGEQU
END FACTORIO </
{{out}}
<pre>
Line 138:
=={{header|ALGOL 68}}==
{{trans|C}}
<
# cache factorials from 0 to 11 #
[ 0 : 11 ]INT fact;
Line 158:
print( ( newline ) )
OD
END</
{{out}}
<pre>
Line 171:
=={{header|Applesoft BASIC}}==
<
110 FACT(0) = 1
120 FOR N = 1 TO 11
Line 190:
300 NEXT I
310 PRINT : PRINT
320 NEXT B</
=={{header|Arturo}}==
<
factorion?: function [n, base][
Line 208:
loop 9..12 'base ->
print ["Base" base "factorions:" select 1..45000 'z -> factorion? z base]
]</
{{out}}
Line 219:
=={{header|AutoHotkey}}==
{{trans|C}}
<
fact[0] := 1
while (A_Index < 12)
Line 241:
}
MsgBox % res
return</
{{out}}
<pre>
Line 250:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f FACTORIONS.AWK
# converted from C
Line 276:
exit(0)
}
</syntaxhighlight>
{{out}}
<pre>
Line 287:
=={{header|C}}==
{{trans|Go}}
<
int main() {
Line 313:
}
return 0;
}</
{{out}}
Line 332:
=={{header|C++}}==
{{trans|C}}
<
class factorion_t {
Line 363:
}
return 0;
}</
{{out}}
<pre>factorions for base 9: 1 2 41282
Line 371:
</pre>
=={{header|Common Lisp}}==
<
(defparameter *limit* 1500000)
Line 401:
(if (/= base 10) (format t " (decimal ~a)" n))
(format t "~%"))
(format t "~%")))</
{{Out}}
Line 431:
{{libheader| System.SysUtils}}
{{Trans|C}}
<syntaxhighlight lang="delphi">
program Factorions;
Line 466:
end;
readln;
end.</
=={{header|F_Sharp|F#}}==
<
// Factorians. Nigel Galloway: October 22nd., 2021
let N=[|let mutable n=1 in yield n; for g in 1..11 do n<-n*g; yield n|]
let fG n g=let rec fN g=function i when i<n->g+N.[i] |i->fN(g+N.[i%n])(i/n) in fN 0 g
{9..12}|>Seq.iter(fun n->printf $"In base %d{n} Factorians are:"; {1..1500000}|>Seq.iter(fun g->if g=fG n g then printf $" %d{g}"); printfn "")
</syntaxhighlight>
{{out}}
<pre>In base 9 Factorians are: 1 2 41282
Line 483:
=={{header|Factor}}==
<
prettyprint sequences ;
IN: rosetta-code.factorions
Line 498:
curry each nl ;
1,500,000 9 12 [a,b] [ show-factorions nl ] with each</
{{out}}
<pre>
Line 523:
=={{header|FreeBASIC}}==
<
fact(0) = 1
For n As Integer = 1 To 11
Line 542:
Print : Print
Next b
Sleep</
{{out}}
<pre>
Line 559:
=={{header|Frink}}==
<
for base = 9 to 12
Line 566:
if n == factorion[n, base]
println["$base\t$n"]
}</
{{out}}
Line 586:
=={{header|Go}}==
<
import (
Line 619:
fmt.Println("\n")
}
}</
{{out}}
Line 636:
</pre>
=={{header|Haskell}}==
<
import Data.List (unfoldr)
import Control.Monad (guard)
Line 650:
where
factorions b = filter (factorion b) [1..]
result n = show . take n . factorions</
{{out}}
<pre>
Line 660:
=={{header|J}}==
<syntaxhighlight lang="j">
index=: $ #: I.@:,
factorion=: 10&$: :(] = [: +/ [: ! #.^:_1)&>
Line 689:
11 5
12 2
</syntaxhighlight>
=={{header|Java}}==
<
public class Factorion {
public static void main(String [] args){
Line 765:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 779:
=={{header|Julia}}==
<
printallfactorian(base) = println("Factorians for base $base: ", [n for n in 1:100000 if isfactorian(n, base)])
foreach(printallfactorian, 9:12)
</
<pre>
Factorians for base 9: [1, 2, 41282]
Line 793:
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
FactorionQ[n_,b_:10]:=Total[IntegerDigits[n,b]!]==n
Select[Range[1500000],FactorionQ[#,9]&]
Select[Range[1500000],FactorionQ[#,10]&]
Select[Range[1500000],FactorionQ[#,11]&]
Select[Range[1500000],FactorionQ[#,12]&]</
{{out}}
<pre>{1, 2, 41282}
Line 807:
=={{header|Nim}}==
Note that the library has precomputed the values of factorial, so there is no need for caching.
<
from strutils import join
Line 834:
for base in 9..12:
echo "Factorions for base ", base, ':'
echo factorions(base, 1_500_000 - 1).join(" ")</
{{out}}
Line 848:
=={{header|OCaml}}==
{{trans|C}}
<
(* cache factorials from 0 to 11 *)
let fact = Array.make 12 0 in
Line 869:
done;
print_string "\n\n";
done</
=={{header|Pascal}}==
modified [[munchhausen numbers#Pascal]].
output in base and 0! == 1!, so in Base 10 40585 has the same digits as 14558.
<
{$IFDEF FPC}{$MODE objFPC}{$Optimization,On,all}{$ELSE}{$APPTYPE CONSOLE}{$ENDIF}
uses
Line 987:
end;
writeln('Check Count ',cnt);
end.</
{{out}}
<pre>
Line 1,063:
{{trans|Raku}}
{{libheader|ntheory}}
<
use warnings;
use ntheory qw/factorial todigits/;
Line 1,086:
}
print "\n\n";
}</
{{out}}
<pre>Factorions in base 9:
Line 1,104:
{{trans|Sidef}}
{{libheader|ntheory}}
<
use ntheory qw(:all);
use experimental qw(signatures);
Line 1,140:
my @r = factorions($base);
say "Factorions in base $base are (@r)";
}</
{{out}}
<pre>
Line 1,161:
{{trans|C}}
As per talk page (ok, ''and'' the task description), this is incorrectly using the base 10 limit for bases 9, 11, and 12.
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">=</span><span style="color: #000000;">9</span> <span style="color: #008080;">to</span> <span style="color: #000000;">12</span> <span style="color: #008080;">do</span>
Line 1,176:
<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;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 1,186:
{{trans|Sidef}}
Using the correct limits and much faster, or at least it was until I upped the bases to 14.
<!--<
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">max_power</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">base</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
Line 1,224:
<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;">"Base %2d factorions: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">base</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">factorions</span><span style="color: #0000FF;">(</span><span style="color: #000000;">base</span><span style="color: #0000FF;">))})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 1,250:
=={{header|PureBasic}}==
{{trans|C}}
<
If OpenConsole() : main() : Else : End 1 : EndIf
Line 1,273:
Print(~"\n\n")
Next
EndProcedure</
{{out}}
<pre>The factorions for base 9 are:
Line 1,289:
=={{header|Python}}==
{{trans|C}}
<
for n in range(1, 12):
fact.append(fact[n-1] * n)
Line 1,305:
print(i, end=" ")
print("\n")
</syntaxhighlight>
{{out}}
Line 1,324:
=={{header|Quackery}}==
<
4 times
[ ' [ stack [ ] ]
Line 1,361:
[ say "Factorions for base "
i^ radix echo say ": "
i^ results take echo cr ]</
{{out}}
Line 1,375:
{{trans|C}}
<
(define fact
Line 1,389:
[(positive? n) (loop (+ sum (fact (modulo n b))) (quotient n b))]
[(= sum i) (printf "~a " i)])))
(newline))</
{{out}}
Line 1,407:
{{works with|Rakudo|2019.07.1}}
<syntaxhighlight lang="raku"
constant $limit = 1500000;
Line 1,439:
}
.say for @result[$bases];</
{{out}}
<pre>Factorions in base 9:
Line 1,455:
=={{header|REXX}}==
{{trans|C}}
<
parse arg LOb HIb lim . /*obtain optional arguments from the CL*/
if LOb=='' | LOb=="," then LOb= 9 /*Not specified? Then use the default.*/
Line 1,479:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
!: procedure; parse arg x; !=1; do j=2 to x; !=!*j; end; return ! /*factorials*/</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,492:
=={{header|Ruby}}==
<
def factorion?(n, base)
n.digits(base).sum{|digit| (1..digit).inject(1, :*)} == n
Line 1,500:
puts "Base #{base} factorions: #{(1..1_500_000).select{|n| factorion?(n, base)}.join(" ")} "
end
</syntaxhighlight>
{{out}}
<pre>Base 9 factorions: 1 2 41282
Line 1,510:
=={{header|Scala}}==
{{trans|C++}}
<
private def is_factorion(i: Int, b: Int): Boolean = {
var sum = 0L
Line 1,529:
println
})
}</
=={{header|Sidef}}==
<
var m = 1
var f = (b-1)!
Line 1,562:
var r = factorions(b)
say "Base #{'%2d' % b} factorions: #{r}"
}</
{{out}}
<pre>
Line 1,582:
{{trans|C}}
<
fact[0] = 1
Line 1,609:
print("\n")
}</
{{out}}
Line 1,627:
=={{header|Vlang}}==
{{trans|Go}}
<
fn main() {
Line 1,655:
println("\n")
}
}</
{{out}}
Line 1,674:
=={{header|Wren}}==
{{trans|C}}
<
var fact = List.filled(12, 0)
fact[0] = 1
Line 1,692:
}
System.print("\n")
}</
{{out}}
Line 1,710:
=={{header|VBScript}}==
<
Dim fact()
nn1=9 : nn2=12
Line 1,732:
Next
Wscript.Echo "the factorions for base "& right(" "& base,2) &" are: "& list
Next </
{{out}}
<pre>
Line 1,744:
=={{header|zkl}}==
{{trans|C}}
<
fcn factorions(base){
fs:=List();
Line 1,756:
}
fs
}</
<
println("The factorions for base %2d are: ".fmt(n),factorions(n).concat(" "));
}</
{{out}}
<pre>
|