Factorions: Difference between revisions

23,731 bytes added ,  6 months ago
m
→‎{{header|Wren}}: Changed to Wren S/H
(R)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(28 intermediate revisions by 19 users not shown)
Line 13:
 
 
It can be shown (see thetalk Wikipedia article belowpage) that no factorion in base '''10''' can exceed   '''1,499,999'''.
 
 
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 170:
1 2</pre>
 
=={{header|Applesoft BASICArturo}}==
 
<lang basic>100 DIM FACT(12)
<syntaxhighlight lang="rebol">factorials: [1 1 2 6 24 120 720 5040 40320 362880 3628800 39916800]
110 FACT(0) = 1
 
120 FOR N = 1 TO 11
factorion?: function [n, base][
130 FACT(N) = FACT(N - 1) * N
try? [
140 NEXT
n = sum map digits.base:base n 'x -> factorials\[x]
200 FOR B = 9 TO 12
]
210 PRINT "THE FACTORIONS ";
else [
215 PRINT "FOR BASE "B" ARE:"
220 FOR I = 1print TO["n:" 1499999n "base:" base]
230 SUM = 0false
]
240 FOR J = I TO 0 STEP 0
]
245 M = INT (J / B)
 
250 D = J - M * B
loop 9..12 'base ->
260 SUM = SUM + FACT(D)
print ["Base" base "factorions:" select 1..45000 'z -> factorion? z base]
270 J = M
]</syntaxhighlight>
280 NEXT J
 
290 IF SU = I THEN PRINT I" ";
{{out}}
300 NEXT I
 
310 PRINT : PRINT
<pre>Base 9 factorions: [1 2 41282]
320 NEXT B</lang>
Base 10 factorions: [1 2 145 40585]
Base 11 factorions: [1 2 26 48 40472]
Base 12 factorions: [1 2]</pre>
 
=={{header|AutoHotkey}}==
{{trans|C}}
<langsyntaxhighlight AutoHotkeylang="autohotkey">fact:=[]
fact[0] := 1
while (A_Index < 12)
Line 216 ⟶ 219:
}
MsgBox % res
return</langsyntaxhighlight>
{{out}}
<pre>
Line 225 ⟶ 228:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f FACTORIONS.AWK
# converted from C
Line 251 ⟶ 254:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 259 ⟶ 262:
base 12 factorions: 1 2
</pre>
 
=={{header|BASIC}}==
==={{header|Applesoft BASIC}}===
<syntaxhighlight lang="basic">100 DIM FACT(12)
110 FACT(0) = 1
120 FOR N = 1 TO 11
130 FACT(N) = FACT(N - 1) * N
140 NEXT
200 FOR B = 9 TO 12
210 PRINT "THE FACTORIONS ";
215 PRINT "FOR BASE "B" ARE:"
220 FOR I = 1 TO 1499999
230 SUM = 0
240 FOR J = I TO 0 STEP 0
245 M = INT (J / B)
250 D = J - M * B
260 SUM = SUM + FACT(D)
270 J = M
280 NEXT J
290 IF SU = I THEN PRINT I" ";
300 NEXT I
310 PRINT : PRINT
320 NEXT B</syntaxhighlight>
 
=={{header|C}}==
{{trans|Go}}
<langsyntaxhighlight lang="c">#include <stdio.h>
 
int main() {
Line 288 ⟶ 314:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 307 ⟶ 333:
=={{header|C++}}==
{{trans|C}}
<langsyntaxhighlight lang="cpp">#include <iostream>
 
class factorion_t {
Line 338 ⟶ 364:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>factorions for base 9: 1 2 41282
Line 345 ⟶ 371:
factorions for base 12: 1 2
</pre>
=={{header|Common Lisp}}==
<syntaxhighlight lang="lisp">(defparameter *bases* '(9 10 11 12))
(defparameter *limit* 1500000)
 
(defun ! (n) (apply #'* (loop for i from 2 to n collect i)))
 
(defparameter *digit-factorials* (mapcar #'! (loop for i from 0 to (1- (apply #'max *bases*)) collect i)))
 
(defun fact (n) (nth n *digit-factorials*))
 
(defun digit-value (digit)
(let ((decimal (digit-char-p digit)))
(cond ((not (null decimal)) decimal)
((char>= #\Z digit #\A) (+ (char-code digit) (- (char-code #\A)) 10))
((char>= #\z digit #\a) (+ (char-code digit) (- (char-code #\a)) 10))
(t nil))))
 
(defun factorionp (n &optional (base 10))
(= n (apply #'+
(mapcar #'fact
(map 'list #'digit-value
(write-to-string n :base base))))))
 
(loop for base in *bases* do
(let ((factorions
(loop for i from 1 while (< i *limit*) if (factorionp i base) collect i)))
(format t "In base ~a there are ~a factorions:~%" base (list-length factorions))
(loop for n in factorions do
(format t "~c~a" #\Tab (write-to-string n :base base))
(if (/= base 10) (format t " (decimal ~a)" n))
(format t "~%"))
(format t "~%")))</syntaxhighlight>
 
{{Out}}
<pre>In base 9 there are 3 factorions:
1 (decimal 1)
2 (decimal 2)
62558 (decimal 41282)
 
In base 10 there are 4 factorions:
1
2
145
40585
 
In base 11 there are 5 factorions:
1 (decimal 1)
2 (decimal 2)
24 (decimal 26)
44 (decimal 48)
28453 (decimal 40472)
 
In base 12 there are 2 factorions:
1 (decimal 1)
2 (decimal 2)
</pre>
 
 
=={{header|Delphi}}==
{{libheader| System.SysUtils}}
{{Trans|C}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Factorions;
 
Line 383 ⟶ 467:
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 398 ⟶ 482:
In base 12 Factorians are: 1 2
</pre>
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: formatting io kernel math math.parser math.ranges memoize
prettyprint sequences ;
IN: rosetta-code.factorions
Line 414 ⟶ 499:
curry each nl ;
 
1,500,000 9 12 [a,b] [ show-factorions nl ] with each</langsyntaxhighlight>
{{out}}
<pre>
Line 432 ⟶ 517:
=={{header|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Factorions}}
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation &mdash;i.e. XML, JSON&mdash; they are intended for storage and transfer purposes more than visualization and edition.
 
'''Solution'''
 
Definitions:
 
[[File:Fōrmulæ - Factorions 01.png]]
 
[[File:Fōrmulæ - Factorions 02.png]]
 
The following calculates factorion lists from bases 9 to 12, with a limit of 1,499,999
 
[[File:Fōrmulæ - Factorions 03.png]]
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used.
 
[[File:Fōrmulæ - Factorions 04.png]]
In '''[https://formulae.org/?example=Factorions this]''' page you can see the program(s) related to this task and their results.
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">Dim As Integer fact(12), suma, d, j
fact(0) = 1
For n As Integer = 1 To 11
Line 458 ⟶ 553:
Print : Print
Next b
Sleep</langsyntaxhighlight>
{{out}}
<pre>
Line 475 ⟶ 570:
 
=={{header|Frink}}==
<langsyntaxhighlight lang="frink">factorion[n, base] := sum[map["factorial", integerDigits[n, base]]]
 
for base = 9 to 12
Line 482 ⟶ 577:
if n == factorion[n, base]
println["$base\t$n"]
}</langsyntaxhighlight>
 
{{out}}
Line 502 ⟶ 597:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 535 ⟶ 630:
fmt.Println("\n")
}
}</langsyntaxhighlight>
 
{{out}}
Line 552 ⟶ 647:
</pre>
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Text.Printf (printf)
import Data.List (unfoldr)
import Control.Monad (guard)
Line 566 ⟶ 661:
where
factorions b = filter (factorion b) [1..]
result n = show . take n . factorions</langsyntaxhighlight>
{{out}}
<pre>
Line 576 ⟶ 671:
 
=={{header|J}}==
<syntaxhighlight lang="j">
<lang J>
index=: $ #: I.@:,
factorion=: 10&$: :(] = [: +/ [: ! #.^:_1)&>
Line 582 ⟶ 677:
FACTORIONS=: 9 0 +"1 index Q=: 9 10 11 12 factorion/ i. 1500000
 
NB. columns: base, factorion expressed in basesbase 10, andfactorion in base
(,. ".@:((Num_j_,26}.Alpha_j_) {~ #.inv/)"1) FACTORIONS
9 1 1
Line 605 ⟶ 700:
11 5
12 2
</syntaxhighlight>
</lang>
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">
public class Factorion {
public static void main(String [] args){
Line 681 ⟶ 776:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 692 ⟶ 787:
Base 12:
1 2
</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Also works with gojq, the Go implementation of jq, and with fq.'''
 
The main difficulty in computing the factorions of an arbitrary base
is obtaining a tight limit on the maximum value a factorion can
have in that base. The present entry accordingly does at least provide a function,
`sufficient`, for computing an upper bound with respect to a particular base, and uses it to compute
the factorions of all bases from 2 through 9.
 
However, the algorithm used by `sufficient` is too simplistic to be of much practical use for bases 10 or higher.
For base 10, the task description provides a value with a link to a justification. For bases 11 and 12, we use limits that are known to be sufficient, as per (*) [https://web.archive.org/web/20151220095834/https://en.wikipedia.org/wiki/Factorion].
 
<syntaxhighlight lang=jq>
# A stream of factorials
# [N|factorials][n] is n!
def factorials:
select(. > 0)
| 1,
foreach range(1; .) as $n(1; . * $n);
 
# The base-$b factorions less than or equal to $max
def factorions($b; $max):
($max // 1500000) as $max
| [$b|factorials] as $fact
| range(1; $max) as $i
| {sum: 0, j: $i}
| until( .j == 0 or .sum > $i;
( .j % $b) as $d
| .sum += $fact[$d]
| .j = ((.j/$b)|floor) )
| select(.sum == $i)
| $i ;
 
# input: base
# output: an upper bound for the factorions in that base
def sufficient:
. as $base
| [12|factorials] as $fact
| $fact[$base-1] as $f
| { digits: 1, value: $base}
| until ( (.value > ($f * .digits) );
.digits += 1
| .value *= $base ) ;
 
# Show the factorions for all based from 2 through 12:
(range(2;10)
| . as $base
| sufficient.value as $max
| {$base, factorions: ([factorions($base; $max)] | join(" "))}),
{base: 10, factorions: ([factorions(10; 1500000)] | join(" "))}, # limit per the task description
{base: 11, factorions: ([factorions(11; 50000)] | join(" "))}, # a limit known to be sufficient per (*)
{base: 12, factorions: ([factorions(12; 50000)] | join(" "))} # a limit known to be sufficient per (*)
 
</syntaxhighlight>
{{output}}
<pre>
{"base":2,"factorions":"1 2"}
{"base":3,"factorions":"1 2"}
{"base":4,"factorions":"1 2 7"}
{"base":5,"factorions":"1 2 49"}
{"base":6,"factorions":"1 2 25 26"}
{"base":7,"factorions":"1 2"}
{"base":8,"factorions":"1 2"}
{"base":9,"factorions":"1 2 41282"}
{"base":10,"factorions":"1 2 145 40585"}
{"base":11,"factorions":"1 2 26 48 40472"}
{"base":12,"factorions":"1 2"}
</pre>
 
=={{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 706 ⟶ 871:
Factorians for base 11: [1, 2, 26, 48, 40472]
Factorians for base 12: [1, 2]
</pre>
 
=={{header|Lambdatalk}}==
<syntaxhighlight lang="scheme">
 
{def facts
{S.first
{S.map {{lambda {:a :i}
{A.addlast! {* {A.get {- :i 1} :a} :i} :a}
} {A.new 1}}
{S.serie 1 11}}}}
-> facts
 
{def sumfacts
{def sumfacts.r
{lambda {:base :sum :i}
{if {> :i 0}
then {sumfacts.r :base
{+ :sum {A.get {% :i :base} {facts}}}
{floor {/ :i :base}}}
else :sum }}}
{lambda {:base :n}
{sumfacts.r :base 0 :n}}}
-> sumfacts
 
{def show
{lambda {:base}
{S.replace \s by space in
{S.map {{lambda {:base :i}
{if {= {sumfacts :base :i} :i} then :i else}
} :base}
{S.serie 1 50000}}}}}
-> show
 
{S.map {lambda {:base}
{div}factorions for base :base: {show :base}}
9 10 11 12}
->
factorions for base 9: 1 2 41282
factorions for base 10: 1 2 145 40585
factorions for base 11: 1 2 26 48 40472
factorions for base 12: 1 2
 
</syntaxhighlight>
 
=={{header|Lang}}==
{{trans|Python}}
<syntaxhighlight lang="lang">
# Enabling raw variable names boosts the performance massivly [DO NOT RUN WITHOUT enabling raw variable names]
lang.rawVariableNames = 1
 
# Cache factorials from 0 to 11
&fact = fn.listOf(1)
$n = 1
while($n < 12) {
&fact += &fact[-|$n] * $n
$n += 1
}
 
$b = 9
while($b <= 12) {
fn.printf(The factorions for base %d are:%n, $b)
$i = 1
while($i < 1500000) {
$sum = 0
$j = $i
while($j > 0) {
$d $= $j % $b
$sum += &fact[$d]
$j //= $b
}
if($sum == $i) {
fn.print($i\s)
}
$i += 1
}
fn.println(\n)
$b += 1
}
</syntaxhighlight>
{{out}}
<pre>
The factorions for base 9 are:
1 2 41282
 
The factorions for base 10 are:
1 2 145 40585
 
The factorions for base 11 are:
1 2 26 48 40472
 
The factorions for base 12 are:
1 2
 
</pre>
 
=={{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 723 ⟶ 989:
=={{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 750 ⟶ 1,016:
for base in 9..12:
echo "Factorions for base ", base, ':'
echo factorions(base, 1_500_000 - 1).join(" ")</langsyntaxhighlight>
 
{{out}}
Line 764 ⟶ 1,030:
=={{header|OCaml}}==
{{trans|C}}
<langsyntaxhighlight lang="ocaml">let () =
(* cache factorials from 0 to 11 *)
let fact = Array.make 12 0 in
Line 785 ⟶ 1,051:
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.
<syntaxhighlight lang="pascal">program munchhausennumber;
<lang pascal>
{$IFDEF FPC}{$MODE objFPC}{$Optimization,On,all}{$ELSE}{$APPTYPE CONSOLE}{$ENDIF}
program munchhausennumber;
{$IFDEF FPC}{$MODE objFPC}{$ELSE}{$APPTYPE CONSOLE}{$ENDIF}
uses
sysutils;
Line 797 ⟶ 1,062:
tdigit = byte;
const
MAXBASE = 1417;
 
var
Line 836 ⟶ 1,101:
For i := 2 to Base-1 do
result := result AND (dgtCnt[i]=0);
result := result AND (dgtCnt[0]+dgtCnt[1]=0);
 
end;
 
Line 895 ⟶ 1,162:
begin
cnt := 0;
For base := 2 to 14MAXBASE do
begin
writeln('Base = ',base);
InitDgtPotDgt(base);
Munch(0,0,0,base-1,base);
end;
writeln('Check Count ',cnt);
end.</langsyntaxhighlight>
{{out}}
<pre>
TIO.RUN Real time: 45.701 s User time: 44.968 s Sys. time: 0.055 s CPU share: 98.51 %
Base = 2
1 1 1
Line 955 ⟶ 1,223:
2 2 2
12973363226 8B0DD409C 11489BCDD
Base = 15
Check Count 13027729
1 1 1
2 2 2
1441 661 166
1442 662 266
Base = 16
1 1 1
2 2 2
2615428934649 260F3B66BF9 1236669BBFF
Base = 17
1 1 1
2 2 2
40465 8405 1458
43153254185213 146F2G8500G4 111244568FGG
43153254226251 146F2G8586G4 124456688FGG
Check Count 1571990934
</pre>
 
=={{header|Perl}}==
===Raku version===
{{trans|Raku}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use ntheory qw/factorial todigits/;
Line 984 ⟶ 1,268:
}
print "\n\n";
}</langsyntaxhighlight>
{{out}}
<pre>Factorions in base 9:
Line 998 ⟶ 1,282:
1 2</pre>
 
===Sidef version===
Alternatively, a more efficient approach:
{{trans|Sidef}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use 5.020;
use ntheory qw(:all);
use experimental qw(signatures);
Line 1,037 ⟶ 1,322:
my @r = factorions($base);
say "Factorions in base $base are (@r)";
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,057 ⟶ 1,342:
=={{header|Phix}}==
{{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.
<lang Phix>-- cache factorials from 0 to 11
<!--<syntaxhighlight lang="phix">(phixonline)-->
sequence fact = repeat(1,12)
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
for n=2 to length(fact) do
<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>
fact[n] = fact[n-1]*(n-1)
<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;">"The factorions for base %d are: "</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1499999</span> <span style="color: #008080;">do</span>
 
<span style="color: #004080;">atom</span> <span style="color: #000000;">total</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">j</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">d</span>
for b=9 to 12 do
<span style="color: #008080;">while</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">and</span> <span style="color: #000000;">total</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">i</span> <span style="color: #008080;">do</span>
printf(1,"The factorions for base %d are:\n", b)
<span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">j</span><span style="color: #0000FF;">,</span><span style="color: #000000;">base</span><span style="color: #0000FF;">)</span>
for i=1 to 1499999 do
<span style="color: #000000;">total</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8;">factorial</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
atom total = 0, j = i, d
<span style="color: #000000;">j</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">j</span><span style="color: #0000FF;">/</span><span style="color: #000000;">base</span><span style="color: #0000FF;">)</span>
while j>0 and total<=i do
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
d = remainder(j,b)
<span style="color: #008080;">if</span> <span style="color: #000000;">total</span><span style="color: #0000FF;">==</span><span style="color: #000000;">i</span> <span style="color: #008080;">then</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 "</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
total += fact[d+1]
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
j = floor(j/b)
<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>
end while
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
if total==i then printf(1,"%d ", i) end if
<!--</syntaxhighlight>-->
end for
printf(1,"\n\n")
end for</lang>
{{out}}
<pre>
The factorions for base 9 are: 1 2 41282
The factorions for base 10 are: 1 2 145 40585
1 2 41282
The factorions for base 11 are: 1 2 26 48 40472
 
The factorions for base 1012 are: 1 2
</pre>
1 2 145 40585
{{trans|Sidef}}
 
Using the correct limits and much faster, or at least it was until I upped the bases to 14.
The factorions for base 11 are:
<!--<syntaxhighlight lang="phix">(phixonline)-->
1 2 26 48 40472
<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>
The factorions for base 12 are:
<span style="color: #004080;">integer</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
1 2
<span style="color: #004080;">atom</span> <span style="color: #000000;">f</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">factorial</span><span style="color: #0000FF;">(</span><span style="color: #000000;">base</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">*</span><span style="color: #000000;">f</span> <span style="color: #0000FF;">>=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">base</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">m</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">digits</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"0123456789abcd"</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">fcomb</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">res</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;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">at</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">fsum</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">chosen</span><span style="color: #0000FF;">=</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">chosen</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">n</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">fs</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%a"</span><span style="color: #0000FF;">,{{</span><span style="color: #000000;">base</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fsum</span><span style="color: #0000FF;">}}))</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">fs</span><span style="color: #0000FF;">=</span><span style="color: #000000;">chosen</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fsum</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">else</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">at</span> <span style="color: #008080;">to</span> <span style="color: #000000;">base</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">fcomb</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">base</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">i</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fsum</span><span style="color: #0000FF;">+</span><span style="color: #7060A8;">factorial</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span><span style="color: #000000;">chosen</span><span style="color: #0000FF;">&</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">factorions</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>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">result</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">max_power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">base</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">result</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">fcomb</span><span style="color: #0000FF;">({},</span><span style="color: #000000;">base</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">result</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">14</span> <span style="color: #008080;">do</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;">"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>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Base 2 factorions: 1 2
Base 3 factorions: 1 2
Base 4 factorions: 1 2 7
Base 5 factorions: 1 2 49
Base 6 factorions: 1 2 25 26
Base 7 factorions: 1 2
Base 8 factorions: 1 2
Base 9 factorions: 1 2 41282
Base 10 factorions: 1 2 145 40585
Base 11 factorions: 1 2 26 48 40472
Base 12 factorions: 1 2
Base 13 factorions: 1 2 519326767
Base 14 factorions: 1 2 12973363226
</pre>
It will in fact go all the way to 17, though I don't recommend it:
<pre>
Base 15 factorions: 1 2 1441 1442
Base 16 factorions: 1 2 2615428934649
Base 17 factorions: 1 2 40465 43153254185213 43153254226251
</pre>
 
=={{header|PureBasic}}==
{{trans|C}}
<langsyntaxhighlight PureBasiclang="purebasic">Declare main()
 
If OpenConsole() : main() : Else : End 1 : EndIf
Line 1,116 ⟶ 1,455:
Print(~"\n\n")
Next
EndProcedure</langsyntaxhighlight>
{{out}}
<pre>The factorions for base 9 are:
Line 1,132 ⟶ 1,471:
=={{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,148 ⟶ 1,487:
print(i, end=" ")
print("\n")
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,167 ⟶ 1,506:
=={{header|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> [ table ] is results ( n --> s )
4 times
[ ' [ stack [ ] ]
Line 1,204 ⟶ 1,543:
[ say "Factorions for base "
i^ radix echo say ": "
i^ results take echo cr ]</langsyntaxhighlight>
 
{{out}}
Line 1,218 ⟶ 1,557:
 
{{trans|C}}
<langsyntaxhighlight lang="racket">#lang racket
 
(define fact
Line 1,232 ⟶ 1,571:
[(positive? n) (loop (+ sum (fact (modulo n b))) (quotient n b))]
[(= sum i) (printf "~a " i)])))
(newline))</langsyntaxhighlight>
 
{{out}}
Line 1,250 ⟶ 1,589:
{{works with|Rakudo|2019.07.1}}
 
<syntaxhighlight lang="raku" perl6line>constant @factorial = 1, |[\*] 1..*;
 
constant $limit = 1500000;
Line 1,258 ⟶ 1,597:
my @result;
 
$bases.race(:1batch).map: -> $base {
 
@result[$base] = "\nFactorions in base $base:\n1 2";
Line 1,282 ⟶ 1,621:
}
 
.say for @result[$bases];</langsyntaxhighlight>
{{out}}
<pre>Factorions in base 9:
Line 1,298 ⟶ 1,637:
=={{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,322 ⟶ 1,661:
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,334 ⟶ 1,673:
</pre>
 
=={{header|RingRPL}}==
{{trans|C}}
<lang Ring>
{{works with|Halcyon Calc|4.2.7}}
load "stdlib.ring"
{| class="wikitable"
see "working..." + nl
! Code
 
! Comments
for n = 1 to 100000
|-
Digits = []
|
sumFact = 0
strn = string(n)
{ } 1 11 '''FOR''' n n FACT + '''NEXT''' → base fact
for m = 1 to len(strn)
≪ { } 1 1500000 '''FOR''' n
fact = factorial(number(strn[m]))
0 n '''WHILE''' DUP '''REPEAT'''
sumFact += fact
add(Digits, fact) OVER base MOD 1 MAX GET
ROT + SWAP
next
if sumFact = n base / IP
for ind'''END''' =DROP 1 to len(strn)-1
'''IF''' n == see'''THEN''' ""n + strn[ind]'''END''' + "! + "
'''NEXT''' next
≫ ≫ ‘FTRION’ STO
see "" + strn[ind] + "! = "
|
for p = 1 to len(Digits) - 1
''( base -- { factorions } )''
see "" + Digits[p] + " + "
Cache 1! to next11!
factLast = Digits[p]
Loop until all digits scanned
see "" + factLast + " = " + n + nl
Get (last digit)! even if last digit = 0
ok
Add to sum of digits
next
prepare next loop
 
see "done..." + nl
Store factorion
</lang>
|}
The following lines of command deliver what is required:
9 FTRION
10 FTRION
11 FTRION
12 FTRION
{{out}}
<pre>
4: { 1 2 41282 }
working...
1!3: ={ 1 =2 1145 40585 }
2!: ={ 1 2 =26 248 40472 }
1: { 1 2 }
1! + 4! + 5! = 1 + 24 + 120 = 145
4! + 0! + 5! + 8! + 5! = 24 + 1 + 120 + 40320 + 120 = 40585
done...
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">
def factorion?(n, base)
n.digits(base).sum{|digit| (1..digit).inject(1, :*)} == n
Line 1,381 ⟶ 1,727:
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,391 ⟶ 1,737:
=={{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,410 ⟶ 1,756:
println
})
}</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func max_power(b = 10) {
var m = 1
var f = (b-1)!
Line 1,443 ⟶ 1,789:
var r = factorions(b)
say "Base #{'%2d' % b} factorions: #{r}"
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,460 ⟶ 1,806:
 
=={{header|Swift}}==
 
{{trans|C}}
<syntaxhighlight lang="swift">var fact = Array(repeating: 0, count: 12)
 
<lang swift>var fact = Array(repeating: 0, count: 12)
 
fact[0] = 1
Line 1,490 ⟶ 1,834:
 
print("\n")
}</langsyntaxhighlight>
 
{{out}}
Line 1,506 ⟶ 1,850:
1 2</pre>
 
=={{header|WrenuBasic/4tH}}==
{{trans|CFreeBASIC}}
It will take some time, but it will get there.
<lang ecmascript>// cache factorials from 0 to 11
<syntaxhighlight lang="uBasic/4tH">Dim @f(12)
var fact = List.filled(12, 0)
fact[0] = 1
for (n in 1..11) fact[n] = fact[n-1] * n
 
@f(0) = 1: For n = 1 To 11 : @f(n) = @f(n-1) * n : Next
for (b in 9..12) {
 
System.print("The factorions for base %(b) are:")
For b = 9 To 12
for (i in 1...1500000) {
Print "The factorions for base ";b;" are: "
var sum = 0
For i = 1 To var j = i1499999
s while (j >= 0) {
var dj = j % bi
Do While j > 0
sum = sum + fact[d]
d = j =% (j/b).floor
s = s + @f(d)
j = j / b
Loop
If s = i Then Print i;" ";
Next
Print : Print
Next</syntaxhighlight>
{{Out}}
<pre>The factorions for base 9 are:
1 2 41282
 
The factorions for base 10 are:
1 2 145 40585
 
The factorions for base 11 are:
1 2 26 48 40472
 
The factorions for base 12 are:
1 2
 
 
0 OK, 0:379</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">import strconv
 
fn main() {
// cache factorials from 0 to 11
mut fact := [12]u64{}
fact[0] = 1
for n := u64(1); n < 12; n++ {
fact[n] = fact[n-1] * n
}
for b := 9; b <= 12; b++ {
println("The factorions for base $b are:")
for i := u64(1); i < 1500000; i++ {
digits := strconv.format_uint(i, b)
mut sum := u64(0)
for digit in digits {
if digit < `a` {
sum += fact[digit-`0`]
} else {
sum += fact[digit+10-`a`]
}
}
if sum == i {
print("$i ")
}
}
if (sum == i) System.writeprintln("%(i) \n")
}
}</syntaxhighlight>
System.print("\n")
}</lang>
 
{{out}}
Line 1,540 ⟶ 1,931:
 
The factorions for base 12 are:
1 2
</pre>
 
=={{header|VBScript}}==
<langsyntaxhighlight lang="vb">' Factorions - VBScript - PG - 26/04/2020
Dim fact()
nn1=9 : nn2=12
Line 1,566 ⟶ 1,957:
Next
Wscript.Echo "the factorions for base "& right(" "& base,2) &" are: "& list
Next </langsyntaxhighlight>
{{out}}
<pre>
Line 1,575 ⟶ 1,966:
</pre>
 
=={{header|Wren}}==
{{trans|C}}
<syntaxhighlight lang="wren">// cache factorials from 0 to 11
var fact = List.filled(12, 0)
fact[0] = 1
for (n in 1..11) fact[n] = fact[n-1] * n
 
for (b in 9..12) {
System.print("The factorions for base %(b) are:")
for (i in 1...1500000) {
var sum = 0
var j = i
while (j > 0) {
var d = j % b
sum = sum + fact[d]
j = (j/b).floor
}
if (sum == i) System.write("%(i) ")
}
System.print("\n")
}</syntaxhighlight>
 
{{out}}
<pre>
The factorions for base 9 are:
1 2 41282
 
The factorions for base 10 are:
1 2 145 40585
 
The factorions for base 11 are:
1 2 26 48 40472
 
The factorions for base 12 are:
1 2
</pre>
 
=={{header|XPL0}}==
{{trans|C}}
<syntaxhighlight lang "XPL0">int N, Base, Digit, I, J, Sum, Factorial(12);
[Factorial(0):= 1; \cache factorials from 0 to 11
for N:= 1 to 12-1 do
Factorial(N):= Factorial(N-1)*N;
for Base:= 9 to 12 do
[Text(0, "The factorions for base "); IntOut(0, Base); Text(0, " are:^m^j");
for I:= 1 to 1_499_999 do
[Sum:= 0;
J:= I;
while J > 0 do
[Digit:= rem(J/Base);
Sum:= Sum + Factorial(Digit);
J:= J/Base;
];
if Sum = I then [IntOut(0, I); ChOut(0, ^ )];
];
CrLf(0); CrLf(0);
];
]</syntaxhighlight>
{{out}}
<pre>
The factorions for base 9 are:
1 2 41282
 
The factorions for base 10 are:
1 2 145 40585
 
The factorions for base 11 are:
1 2 26 48 40472
 
The factorions for base 12 are:
1 2
</pre>
 
=={{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,590 ⟶ 2,053:
}
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>
9,482

edits