Odd and square numbers: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Added Vala)
m (→‎{{header|Wren}}: Changed to Wren S/H)
(6 intermediate revisions by 4 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 &lt;- 0;
 
toNext oddSquare <&lt;- 01;
again:
oddSquare <- 1;
'''if''' oddSquare &lt; 1000 '''then''' '''begin'''
again:
'''if''' oddSquare <&gt; 100099 '''then''' '''out''' oddSquare '''else''' begin0;
if oddSquare > 99 then out&lt;- oddSquare else+ 0toNext;
oddSquare <toNext &lt;- oddSquaretoNext + toNext8;
toNext '''goto''' <- toNext + 8;again
'''end''' '''else''' 0
goto again
end else 0
'''end''' $
 
end $
</syntaxhighlight>
{{out}}
<pre>
Line 634 ⟶ 632:
841
961
</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="eulerlua">
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 odd_square xseq_odd_squares =
let rec next n a () = Seq.Cons (n, next (n + a) (a + 8)) in
if x land 1 = 0
thennext None1 8
else Some (x * x)
 
let () =
seq_odd_squares |> Seq.drop_while ((>) 100) |> Seq.take_while ((>) 1000)
Seq.(ints 10 |> filter_map odd_square |> take_while ((>) 1000) |> iter (Printf.printf " %u"))</syntaxhighlight>
|> 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,int( math.ceil(math.sqrtisqrt(limit - 1))) + 1, 2):
num = i*i
if (num < 1000 and num > 99):
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 j 2 MOD AND '''THEN''' j + '''END''' NEXT2 '''STEP''' ≫ EVAL
{{out}}
<pre>
Line 1,183 ⟶ 1,217:
{{libheader|Wren-iterate}}
{{libheader|Wren-seq}}
<syntaxhighlight lang="ecmascriptwren">import "./iterate" for Stepped
import "./seq" for Lst
 
9,477

edits