Odd and square numbers: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
(Created Nim solution.) |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(7 intermediate revisions by 5 users not shown) | |||
Line 392:
Same algorithm as the Algol and other samples.
<br>Note formatted output is not Euler's strong point...
'''begin'''▼
<syntaxhighlight lang="euler">▼
'''new''' toNext; '''new''' oddSquare; '''label''' again;▼
▲begin
▲ new toNext; new oddSquare; label again;
toNext <- 0;
again:▼
'''if''' oddSquare < 1000 '''then''' '''begin'''
▲again:
'''if''' oddSquare
'''end''' '''else''' 0▼
goto again▼
▲ end else 0
'''end''' $▼
▲end $
</syntaxhighlight>▼
{{out}}
<pre>
Line 634 ⟶ 632:
841
961
</pre>
=={{header|Lua}}==
for i = 1, math.sqrt( 1000 ), 2 do
local i2 = i * i
if i2 > 99 then
io.write( " ", i2 )
end
end
▲</syntaxhighlight>
{{out}}
<pre>
121 169 225 289 361 441 529 625 729 841 961
</pre>
Line 687 ⟶ 699:
{{out}}<pre>
{121,169,225,289,361,441,529,625,729,841,961}
</pre>
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
block(
[count:99,odd_square:[]],
while count<1000 do (
i:lambda([x],oddp(x) and integerp(sqrt(x)))(count),
if i then odd_square:endcons(count,odd_square),
count:count+1),
odd_square);
</syntaxhighlight>
{{out}}
<pre>
[121,169,225,289,361,441,529,625,729,841,961]
</pre>
Line 802 ⟶ 829:
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let
let rec next n a () = Seq.Cons (n, next (n + a) (a + 8)) in
let () =
seq_odd_squares |> Seq.drop_while ((>) 100) |> Seq.take_while ((>) 1000)
|> Seq.iter (Printf.printf " %u") |> print_newline</syntaxhighlight>
{{out}}<pre> 121 169 225 289 361 441 529 625 729 841 961</pre>
Line 956 ⟶ 983:
<syntaxhighlight lang="python">
import math
szamok = []
limit = 1000
for i in range(1,
num = i*i
if (
szamok.append(num)
Line 970 ⟶ 997:
[121, 169, 225, 289, 361, 441, 529, 625, 729, 841, 961]
</pre>
;By using itertools
<syntaxhighlight lang="python">from itertools import accumulate, count, dropwhile, takewhile
print(*takewhile(lambda x: x<1000, dropwhile(lambda x: x<100, accumulate(count(8, 8), initial=1))))</syntaxhighlight>
{{out}}
<pre>121 169 225 289 361 441 529 625 729 841 961</pre>
=={{header|Quackery}}==
Line 1,083 ⟶ 1,117:
=={{header|RPL}}==
≪ { } 99 999 '''FOR''' j '''IF''' j √ FP NOT
{{out}}
<pre>
Line 1,153 ⟶ 1,187:
<pre>
Same as Wren example
</pre>
=={{header|Vala}}==
{{trans|Wren}}
<syntaxhighlight lang="vala">void main() {
double pow = 1;
for (int p = 0; p < 5; ++p) {
int low = (int)Math.ceil(Math.sqrt(pow));
if (low % 2 == 0) ++low;
int high = (int)Math.floor(Math.sqrt(pow));
int[] odd_square = {};
for (int i = low; i <= high; i += 2) odd_square += i * i;
print(@"$(odd_square.length) odd squares from $(pow/10) to $pow:\n");
for (int i = 0; i < odd_square.length; ++i) {
print("%d ", odd_square[i]);
if ((i + 1) % 10 == 0) print("\n");
}
print("\n\n");
}
}</syntaxhighlight>
{{out}}
<pre>
Same as Wren example.
</pre>
Line 1,158 ⟶ 1,217:
{{libheader|Wren-iterate}}
{{libheader|Wren-seq}}
<syntaxhighlight lang="
import "./seq" for Lst
|