Iterated digits squaring: Difference between revisions

 
(3 intermediate revisions by 3 users not shown)
Line 27:
* [[Digital root]]
* [[Digital root/Multiplicative digital root]]
* [[Happy numbers]]
<br><br>
 
Line 1,703 ⟶ 1,704:
 
{{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 3,446 ⟶ 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,781 ⟶ 3,870:
=={{header|Wren}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="ecmascriptwren">var endsWith89 = Fn.new { |n|
var digit = 0
var sum = 0
2,120

edits