Steady squares: Difference between revisions
Content added Content deleted
(→{{header|ALGOL 60}}: Simplified) |
(Added Euler) |
||
Line 656: | Line 656: | ||
625^2 = 390625 |
625^2 = 390625 |
||
9376^2 = 87909376</pre> |
9376^2 = 87909376</pre> |
||
=={{header|Euler}}== |
|||
The original Euler implementations didn't have loops built-in, however they can be constructed using labels and gotos. |
|||
As procedures can take literal procedures as parameters, procedures to provide a loops can be defined. This is used here to define a "while" loop procedure. |
|||
<br> |
|||
Note that text between ` and ' is a procedure literal. The text "new while; while <- `...'" defines a new variable and assigned the while-loop procedure to it. When called, the text of the condition and loop body must be enclosed in ` and ' to make them procedures - otherwise they would not be evaluated each time as requried. |
|||
<br> |
|||
All Euler variables are declared using "new var" (only one variable per "new"), labels must be declared with "label" and procedure parametrers are declared with "formal" (again, only one label or parameter per "label" or "formal" declaration). |
|||
<br> |
|||
Everything is Euler is an expression (apart from new/label/formal) and returns a value (although the value of a "goto" can't be used), so the "else" part of an "if" is not optional, hence the "else 0"s appearing in the code below. |
|||
<syntaxhighlight lang="euler"> |
|||
begin |
|||
new maxNumber; new powerOfTen; new lastDigit; new n; |
|||
new while; |
|||
while <- ` formal condition; formal loopBody; |
|||
begin |
|||
label again; |
|||
again: if condition then begin loopBody; goto again end else 0 |
|||
end |
|||
' |
|||
; |
|||
maxNumber <- 10 000; |
|||
powerOfTen <- 10; |
|||
lastDigit <- ( 1, 5, 6 ); |
|||
n <- -10; |
|||
while |
|||
( ` begin n <- n + 10; n <= maxNumber end ' |
|||
, ` begin |
|||
new d; |
|||
if n = powerOfTen then powerOfTen <- powerOfTen * 10 else 0; |
|||
d <- 0; |
|||
while |
|||
( ` begin d <- d + 1; d <= length lastDigit end ' |
|||
, ` begin |
|||
new nd; new n2; |
|||
nd <- n + lastDigit[ d ]; |
|||
n2 <- nd * nd; |
|||
if n2 mod powerOfTen = nd then out nd else 0 |
|||
end |
|||
' |
|||
) |
|||
end |
|||
' |
|||
) |
|||
end |
|||
$ |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
NUMBER 1 |
|||
NUMBER 5 |
|||
NUMBER 6 |
|||
NUMBER 25 |
|||
NUMBER 76 |
|||
NUMBER 376 |
|||
NUMBER 625 |
|||
NUMBER 9376 |
|||
</pre> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |