Iterated digits squaring: Difference between revisions
→{{header|Fōrmulæ}}
Thundergnat (talk | contribs) m (fix markup) |
|||
(8 intermediate revisions by 8 users not shown) | |||
Line 27:
* [[Digital root]]
* [[Digital root/Multiplicative digital root]]
* [[Happy numbers]]
<br><br>
Line 1,104 ⟶ 1,105:
*
</pre>
=={{header|Craft Basic}}==
<syntaxhighlight lang="basic">for i = 1 to 1000
let j = i
do
let k = 0
do
let k = int(k + (j % 10) ^ 2)
let j = int(j / 10)
wait
loop j <> 0
let j = k
loopuntil j = 89 or j = 1
if j > 1 then
let n = n + 1
endif
print "iterations: ", i
next i
print "count result: ", n
end</syntaxhighlight>
{{out}}
<pre>
857
</pre>
Line 1,318 ⟶ 1,360:
}</syntaxhighlight>
With a small back-porting (to run it with the Phobos of LDC2 2.065) it runs in about 15.5 seconds.
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
Takes 16 seconds to complete on an a Ryzen 7 Win11 machine
<syntaxhighlight lang="Delphi">
function SumSquaredDigits(N: integer): integer;
{Sum the squares of the digits in a number}
var T: integer;
begin
Result:=0;
repeat
begin
T:=N mod 10;
N:=N div 10;
Result:=Result+T*T;
end
until N<1;
end;
function TestNumber(N: integer): integer;
{Sum the squares of the digits of number, and do it again}
{with tne new number until the result is either 89 or 1}
begin
Result:=N;
repeat Result:=SumSquaredDigits(Result);
until (Result=89) or (Result=1);
end;
procedure TestSquareDigitsSum(Memo: TMemo);
{Count the number of square digit sums end up 89}
var I,Cnt: integer;
begin
Cnt:=0;
for I:=1 to 100000000 do
if TestNumber(I)=89 then Inc(Cnt);
Memo.Lines.Add(IntToStr(Cnt));
end;
</syntaxhighlight>
{{out}}
<pre>
85744333
</pre>
=={{header|ERRE}}==
Line 1,610 ⟶ 1,701:
</pre>
=={{header|
{{FormulaeEntry|page=https://formulae.org/?script=examples/Iterated_digits_squaring}}
'''Solution'''
The following function returns the end value (either 1 or 89) of a given number:
[[File:Fōrmulæ - Iterated digits squaring 01.png]]
The following function calculates the number of 89 endings, from 1 to a given number:
[[File:Fōrmulæ - Iterated digits squaring 02.png]]
'''Test case'''
[[File:Fōrmulæ - Iterated digits squaring 03.png]]
[[File:Fōrmulæ - Iterated digits squaring 04.png]]
=={{header|Go}}==
Line 1,685 ⟶ 1,788:
But note that while the task description claims "you always end with either 1 or 89", that claim is somewhat arbitrary.
:But only somewhat the loop is 89 ? 145 ? 42 ? 20 ? 4 ? 16 ? 37 ? 58 ? 89, so it only ends with 1 or one of the numbers in this loop. 42 is of course far more significant and the one I would choose!!--[[User:Nigel Galloway|Nigel Galloway]] ([[User talk:Nigel Galloway|talk]]) 10:12, 16 September 2014 (UTC)
:: You should move this comment (and my reply here) to the talk page. --[[User:Rdm|Rdm]] ([[User talk:Rdm|talk]]) 03:01, 26 September 2022 (UTC)
<syntaxhighlight lang="j"> sumdigsq^:(i.16) 15
15 26 40 16 37 58 89 145 42 20 4 16 37 58 89 145</syntaxhighlight>
Here, after the initial three values, the final digit repeats with a period of 8 (so it does not end).
<syntaxhighlight lang=J> 10 8$sumdigsq^:(i.80) 15
15 26 40 16 37 58 89 145
42 20 4 16 37 58 89 145
42 20 4 16 37 58 89 145
42 20 4 16 37 58 89 145
42 20 4 16 37 58 89 145
42 20 4 16 37 58 89 145
42 20 4 16 37 58 89 145
42 20 4 16 37 58 89 145
42 20 4 16 37 58 89 145
42 20 4 16 37 58 89 145</syntaxhighlight>
You could just as easily claim that you always end with either 1 or 4. So here's a routine which repeats the sum-square process until the sequence converges, or until it reaches the value 4:
Line 3,345 ⟶ 3,463:
Total under 1000 is : 41
</pre>
=={{header|RPL}}==
{{trans|C}}
The <code>IS89?</code> subroutine handles unsigned integers rather than floating point numbers: it is a little bit longer, but rounding errors are then avoided.
{{works with|Halcyon Calc|4.2.9}}
{| class="wikitable" ≪
! RPL code
! Comment
|-
|
≪ { # 1d # 89d } → goal
≪ R→B '''DO'''
0 SWAP
'''WHILE''' DUP # 0d ≠ '''REPEAT'''
DUP 10 / SWAP OVER 10 * -
DUP * ROT + SWAP
'''END''' DROP
'''UNTIL''' goal OVER POS '''END'''
# 89d ==
≫ ≫ '<span style="color:blue">IS89?</span>' STO
≪ → ndig
≪ { } ndig 81 * 1 + + 0 CON 1 1 PUT '<span style="color:green">SUMS</span>' STO
1 ndig '''FOR''' n
n 81 * 1 '''FOR''' ii
1 9 '''FOR''' j
j SQ
'''IF''' DUP ii > '''THEN''' DROP 9 'j' STO
'''ELSE'''
<span style="color:green">SUMS</span> ii 1 + GET LAST 4 ROLL - GET +
'<span style="color:green">SUMS</span>' ii 1 + ROT PUT
'''END'''
'''NEXT''' -1 '''STEP NEXT'''
0
2 <span style="color:green">SUMS</span> SIZE 1 GET '''FOR''' j
'''IF''' j 1 - <span style="color:blue">IS89?</span> '''THEN''' SUMS j GET + '''END NEXT'''
≫ ≫ '<span style="color:blue">CNT89</span>' STO
|
<span style="color:blue">IS89?</span> ''( x → boolean )''
convert to integer and loop
s = 0
while x ≠ 0
get last digit
square it and add it to s
clean stack
until x = 1 or x = 89
return boolean
<span style="color:blue">CNT89</span> ''( nb_digits → count )''
sums[32*81 + 1] = {1, 0};
for (int n = 1; ; n++) {
for (int i = n*81; i; i--) {
for (int j = 1; j < 10; j++) {
int s = j*j;
if (s > i) break;
sums[i+1] += sums[i-s+1];
count89 = 0;
for (int j = 1; j < n*81 + 1; j++) {
if (!js89(i)) continue; count89 += sums[j+1];
}
|}
8 <span style="color:blue">CNT89</span>
{{out}}
<pre>
1: 85744333
</pre>
Runs in 49 seconds on a iPhone Xr. A basic HP-28 needs 930 seconds for <code>3 CNT89</code>, giving 85, whilst the emulator needs only 5 seconds, meaning that the 8 digits would probably need 2 h 30 on the HP-28.
=={{header|Ruby}}==
Line 3,680 ⟶ 3,870:
=={{header|Wren}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="
var digit = 0
var sum = 0
Line 3,782 ⟶ 3,972:
ret
</syntaxhighlight>
=={{header|XPL0}}==
Simple minded brute force takes 30 seconds on Pi4.
<syntaxhighlight lang "XPL0">int C, N, M, S;
[C:= 0;
for N:= 1 to 100_000_000-1 do
[M:= N;
loop [S:= 0;
repeat M:= M/10;
S:= S + rem(0)*rem(0);
until M = 0;
if S = 89 then
[C:= C+1; quit];
if S = 1 then quit;
M:= S;
];
];
IntOut(0, C);
]</syntaxhighlight>
{{out}}
<pre>
85744333</pre>
=={{header|zkl}}==
|