Factorions: Difference between revisions

m
syntax highlighting fixup automation
m (syntax highlighting fixup automation)
Line 33:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V fact = [1]
L(n) 1..11
fact.append(fact[n-1] * n)
Line 48:
I fact_sum == i
print(i, end' ‘ ’)
print("\n")</langsyntaxhighlight>
 
{{out}}
Line 67:
 
=={{header|360 Assembly}}==
<langsyntaxhighlight lang="360asm">* Factorions 26/04/2020
FACTORIO CSECT
USING FACTORIO,R13 base register
Line 127:
XDEC DS CL12 temp fo xdeco
REGEQU
END FACTORIO </langsyntaxhighlight>
{{out}}
<pre>
Line 138:
=={{header|ALGOL 68}}==
{{trans|C}}
<langsyntaxhighlight lang="algol68">BEGIN
# cache factorials from 0 to 11 #
[ 0 : 11 ]INT fact;
Line 158:
print( ( newline ) )
OD
END</langsyntaxhighlight>
{{out}}
<pre>
Line 171:
 
=={{header|Applesoft BASIC}}==
<langsyntaxhighlight lang="basic">100 DIM FACT(12)
110 FACT(0) = 1
120 FOR N = 1 TO 11
Line 190:
300 NEXT I
310 PRINT : PRINT
320 NEXT B</langsyntaxhighlight>
 
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">factorials: [1 1 2 6 24 120 720 5040 40320 362880 3628800 39916800]
 
factorion?: function [n, base][
Line 208:
loop 9..12 'base ->
print ["Base" base "factorions:" select 1..45000 'z -> factorion? z base]
]</langsyntaxhighlight>
 
{{out}}
Line 219:
=={{header|AutoHotkey}}==
{{trans|C}}
<langsyntaxhighlight AutoHotkeylang="autohotkey">fact:=[]
fact[0] := 1
while (A_Index < 12)
Line 241:
}
MsgBox % res
return</langsyntaxhighlight>
{{out}}
<pre>
Line 250:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f FACTORIONS.AWK
# converted from C
Line 276:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 287:
=={{header|C}}==
{{trans|Go}}
<langsyntaxhighlight lang="c">#include <stdio.h>
 
int main() {
Line 313:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 332:
=={{header|C++}}==
{{trans|C}}
<langsyntaxhighlight lang="cpp">#include <iostream>
 
class factorion_t {
Line 363:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>factorions for base 9: 1 2 41282
Line 371:
</pre>
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">(defparameter *bases* '(9 10 11 12))
(defparameter *limit* 1500000)
 
Line 401:
(if (/= base 10) (format t " (decimal ~a)" n))
(format t "~%"))
(format t "~%")))</langsyntaxhighlight>
 
{{Out}}
Line 431:
{{libheader| System.SysUtils}}
{{Trans|C}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Factorions;
 
Line 466:
end;
readln;
end.</langsyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// 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>
</lang>
{{out}}
<pre>In base 9 Factorians are: 1 2 41282
Line 483:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: formatting io kernel math math.parser math.ranges memoize
prettyprint sequences ;
IN: rosetta-code.factorions
Line 498:
curry each nl ;
 
1,500,000 9 12 [a,b] [ show-factorions nl ] with each</langsyntaxhighlight>
{{out}}
<pre>
Line 523:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">Dim As Integer fact(12), suma, d, j
fact(0) = 1
For n As Integer = 1 To 11
Line 542:
Print : Print
Next b
Sleep</langsyntaxhighlight>
{{out}}
<pre>
Line 559:
 
=={{header|Frink}}==
<langsyntaxhighlight lang="frink">factorion[n, base] := sum[map["factorial", integerDigits[n, base]]]
 
for base = 9 to 12
Line 566:
if n == factorion[n, base]
println["$base\t$n"]
}</langsyntaxhighlight>
 
{{out}}
Line 586:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 619:
fmt.Println("\n")
}
}</langsyntaxhighlight>
 
{{out}}
Line 636:
</pre>
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Text.Printf (printf)
import Data.List (unfoldr)
import Control.Monad (guard)
Line 650:
where
factorions b = filter (factorion b) [1..]
result n = show . take n . factorions</langsyntaxhighlight>
{{out}}
<pre>
Line 660:
 
=={{header|J}}==
<syntaxhighlight lang="j">
<lang J>
index=: $ #: I.@:,
factorion=: 10&$: :(] = [: +/ [: ! #.^:_1)&>
Line 689:
11 5
12 2
</syntaxhighlight>
</lang>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
public class Factorion {
public static void main(String [] args){
Line 765:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 779:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">isfactorian(n, base) = mapreduce(factorial, +, map(c -> parse(Int, c, base=16), split(string(n, base=base), ""))) == n
 
printallfactorian(base) = println("Factorians for base $base: ", [n for n in 1:100000 if isfactorian(n, base)])
 
foreach(printallfactorian, 9:12)
</langsyntaxhighlight>{{out}}
<pre>
Factorians for base 9: [1, 2, 41282]
Line 793:
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[FactorionQ]
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]&]</langsyntaxhighlight>
{{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.
<langsyntaxhighlight Nimlang="nim">from math import fac
from strutils import join
 
Line 834:
for base in 9..12:
echo "Factorions for base ", base, ':'
echo factorions(base, 1_500_000 - 1).join(" ")</langsyntaxhighlight>
 
{{out}}
Line 848:
=={{header|OCaml}}==
{{trans|C}}
<langsyntaxhighlight lang="ocaml">let () =
(* cache factorials from 0 to 11 *)
let fact = Array.make 12 0 in
Line 869:
done;
print_string "\n\n";
done</langsyntaxhighlight>
=={{header|Pascal}}==
modified [[munchhausen numbers#Pascal]].
output in base and 0! == 1!, so in Base 10 40585 has the same digits as 14558.
<langsyntaxhighlight lang="pascal">program munchhausennumber;
{$IFDEF FPC}{$MODE objFPC}{$Optimization,On,all}{$ELSE}{$APPTYPE CONSOLE}{$ENDIF}
uses
Line 987:
end;
writeln('Check Count ',cnt);
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 1,063:
{{trans|Raku}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use ntheory qw/factorial todigits/;
Line 1,086:
}
print "\n\n";
}</langsyntaxhighlight>
{{out}}
<pre>Factorions in base 9:
Line 1,104:
{{trans|Sidef}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use 5.020;
use ntheory qw(:all);
use experimental qw(signatures);
Line 1,140:
my @r = factorions($base);
say "Factorions in base $base are (@r)";
}</langsyntaxhighlight>
{{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.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{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.
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,250:
=={{header|PureBasic}}==
{{trans|C}}
<langsyntaxhighlight PureBasiclang="purebasic">Declare main()
 
If OpenConsole() : main() : Else : End 1 : EndIf
Line 1,273:
Print(~"\n\n")
Next
EndProcedure</langsyntaxhighlight>
{{out}}
<pre>The factorions for base 9 are:
Line 1,289:
=={{header|Python}}==
{{trans|C}}
<langsyntaxhighlight Pythonlang="python">fact = [1] # cache factorials from 0 to 11
for n in range(1, 12):
fact.append(fact[n-1] * n)
Line 1,305:
print(i, end=" ")
print("\n")
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,324:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ table ] is results ( n --> s )
4 times
[ ' [ stack [ ] ]
Line 1,361:
[ say "Factorions for base "
i^ radix echo say ": "
i^ results take echo cr ]</langsyntaxhighlight>
 
{{out}}
Line 1,375:
 
{{trans|C}}
<langsyntaxhighlight lang="racket">#lang racket
 
(define fact
Line 1,389:
[(positive? n) (loop (+ sum (fact (modulo n b))) (quotient n b))]
[(= sum i) (printf "~a " i)])))
(newline))</langsyntaxhighlight>
 
{{out}}
Line 1,407:
{{works with|Rakudo|2019.07.1}}
 
<syntaxhighlight lang="raku" perl6line>constant @factorial = 1, |[\*] 1..*;
 
constant $limit = 1500000;
Line 1,439:
}
 
.say for @result[$bases];</langsyntaxhighlight>
{{out}}
<pre>Factorions in base 9:
Line 1,455:
=={{header|REXX}}==
{{trans|C}}
<langsyntaxhighlight lang="rexx">/*REXX program calculates and displays factorions in bases nine ───► twelve. */
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*/</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,492:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="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>
</lang>
{{out}}
<pre>Base 9 factorions: 1 2 41282
Line 1,510:
=={{header|Scala}}==
{{trans|C++}}
<langsyntaxhighlight lang="scala">object Factorion extends App {
private def is_factorion(i: Int, b: Int): Boolean = {
var sum = 0L
Line 1,529:
println
})
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func max_power(b = 10) {
var m = 1
var f = (b-1)!
Line 1,562:
var r = factorions(b)
say "Base #{'%2d' % b} factorions: #{r}"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,582:
{{trans|C}}
 
<langsyntaxhighlight lang="swift">var fact = Array(repeating: 0, count: 12)
 
fact[0] = 1
Line 1,609:
 
print("\n")
}</langsyntaxhighlight>
 
{{out}}
Line 1,627:
=={{header|Vlang}}==
{{trans|Go}}
<langsyntaxhighlight lang="vlang">import strconv
 
fn main() {
Line 1,655:
println("\n")
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,674:
=={{header|Wren}}==
{{trans|C}}
<langsyntaxhighlight lang="ecmascript">// cache factorials from 0 to 11
var fact = List.filled(12, 0)
fact[0] = 1
Line 1,692:
}
System.print("\n")
}</langsyntaxhighlight>
 
{{out}}
Line 1,710:
 
=={{header|VBScript}}==
<langsyntaxhighlight lang="vb">' Factorions - VBScript - PG - 26/04/2020
Dim fact()
nn1=9 : nn2=12
Line 1,732:
Next
Wscript.Echo "the factorions for base "& right(" "& base,2) &" are: "& list
Next </langsyntaxhighlight>
{{out}}
<pre>
Line 1,744:
=={{header|zkl}}==
{{trans|C}}
<langsyntaxhighlight lang="zkl">var facts=[0..12].pump(List,fcn(n){ (1).reduce(n,fcn(N,n){ N*n },1) }); #(1,1,2,6....)
fcn factorions(base){
fs:=List();
Line 1,756:
}
fs
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">foreach n in ([9..12]){
println("The factorions for base %2d are: ".fmt(n),factorions(n).concat(" "));
}</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits