Iterated digits squaring: Difference between revisions

No edit summary
 
(5 intermediate revisions by 5 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,659 ⟶ 1,701:
</pre>
 
=={{header|FormulæFōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Iterated_digits_squaring}}
Formulæ 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'''
Programs in Formulæ 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.
 
The following function returns the end value (either 1 or 89) of a given number:
In '''[https://formulae.org/?example=Iterated_digits_squaring this]''' page you can see the program(s) related to this task and their results.
 
[[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,409 ⟶ 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,744 ⟶ 3,870:
=={{header|Wren}}==
{{trans|FreeBASIC}}
<syntaxhighlight lang="ecmascriptwren">var endsWith89 = Fn.new { |n|
var digit = 0
var sum = 0
2,120

edits