Factorions: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(11 intermediate revisions by 9 users not shown) | |||
Line 169:
The factorions for base 12 are:
1 2</pre>
=={{header|Arturo}}==
Line 284 ⟶ 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}}==
Line 516 ⟶ 517:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Factorions}}
'''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]]
[[File:Fōrmulæ - Factorions 04.png]]
=={{header|FreeBASIC}}==
Line 776 ⟶ 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>
Line 790 ⟶ 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>
Line 1,489 ⟶ 1,671:
The factorions for base 12 are: 1 2
</pre>
=={{header|RPL}}==
{{trans|C}}
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! Code
! Comments
|-
|
≪
{ } 1 11 '''FOR''' n n FACT + '''NEXT''' → base fact
≪ { } 1 1500000 '''FOR''' n
0 n '''WHILE''' DUP '''REPEAT'''
fact OVER base MOD 1 MAX GET
ROT + SWAP
base / IP
'''END''' DROP
'''IF''' n == '''THEN''' n + '''END'''
'''NEXT'''
≫ ≫ ‘FTRION’ STO
|
''( base -- { factorions } )''
Cache 1! to 11!
Loop until all digits scanned
Get (last digit)! even if last digit = 0
Add to sum of digits
prepare next loop
Store factorion
|}
The following lines of command deliver what is required:
9 FTRION
10 FTRION
11 FTRION
12 FTRION
{{out}}
<pre>
4: { 1 2 41282 }
3: { 1 2 145 40585 }
2: { 1 2 26 48 40472 }
1: { 1 2 }
</pre>
Line 1,579 ⟶ 1,806:
=={{header|Swift}}==
{{trans|C}}
<syntaxhighlight lang="swift">var fact = Array(repeating: 0, count: 12)
Line 1,625 ⟶ 1,850:
1 2</pre>
=={{header|
{{trans|FreeBASIC}}
It will take some time, but it will get there.
<syntaxhighlight lang="uBasic/4tH">Dim @f(12)
@f(0) = 1: For n = 1 To 11 : @f(n) = @f(n-1) * n : Next
For b = 9 To 12
Print "The factorions for base ";b;" are: "
For i = 1 To 1499999
s = 0
j = i
Do While j > 0
d = j % b
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() {
Line 1,670 ⟶ 1,932:
The factorions for base 12 are:
1 2
</pre>
=={{header|VBScript}}==
<syntaxhighlight lang="vb">' Factorions - VBScript - PG - 26/04/2020
Dim fact()
nn1=9 : nn2=12
lim=1499999
ReDim fact(nn2)
fact(0)=1
For i=1 To nn2
fact(i)=fact(i-1)*i
Next
For base=nn1 To nn2
list=""
For i=1 To lim
s=0
t=i
Do While t<>0
d=t Mod base
s=s+fact(d)
t=t\base
Loop
If s=i Then list=list &" "& i
Next
Wscript.Echo "the factorions for base "& right(" "& base,2) &" are: "& list
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
</pre>
=={{header|Wren}}==
{{trans|C}}
<syntaxhighlight lang="
var fact = List.filled(12, 0)
fact[0] = 1
Line 1,709 ⟶ 2,003:
</pre>
=={{header|
{{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>
1 2 41282
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}}==
|