Odd and square numbers: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Added Fe)
m (→‎{{header|Wren}}: Changed to Wren S/H)
(10 intermediate revisions by 7 users not shown)
Line 326:
841
961</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
 
procedure ShowOddSquareNumbers(Memo: TMemo);
var I,N: integer;
var Cnt: integer;
var S: string;
begin
Cnt:=0;
for I:=10 to trunc(sqrt(1000)) do
begin
N:=I * I;
if ((N and 1)=1) then
begin
Inc(Cnt);
S:=S+Format('%8D',[N]);
If (Cnt mod 5)=0 then S:=S+CRLF;
end;
end;
Memo.Lines.Add(S);
Memo.Lines.Add('Count='+IntToStr(Cnt));
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
121 169 225 289 361
441 529 625 729 841
961
Count=11
Elapsed Time: 1.975 ms.
</pre>
 
 
=={{header|Draco}}==
Line 352 ⟶ 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 594 ⟶ 632:
841
961
</pre>
 
=={{header|Lua}}==
<syntaxhighlight lang="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 647 ⟶ 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 711 ⟶ 778:
<pre>
121
169
225
289
361
441
529
625
729
841
961
</pre>
 
=={{header|Nim}}==
<syntaxhighlight lang="Nim">import std/math
 
for n in countup(11, sqrt(1000.0).int, 2):
echo n * n
</syntaxhighlight>
 
{{out}}
<pre>121
169
225
Line 741 ⟶ 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 895 ⟶ 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 909 ⟶ 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,019 ⟶ 1,114:
[121,169,225,289,361,441,529,625,729,841,961]
done...
</pre>
 
=={{header|RPL}}==
≪ { } 99 999 '''FOR''' j '''IF''' j √ FP NOT '''THEN''' j + '''END''' 2 '''STEP''' ≫ EVAL
{{out}}
<pre>
1: { 121 169 225 289 361 441 529 625 729 841 961 }
</pre>
 
Line 1,038 ⟶ 1,140:
961
</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">var lo = 100
Line 1,084 ⟶ 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;
pow *= 10;
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,089 ⟶ 1,217:
{{libheader|Wren-iterate}}
{{libheader|Wren-seq}}
<syntaxhighlight lang="ecmascriptwren">import "./iterate" for Stepped
import "./seq" for Lst
 
9,479

edits