Odd and square numbers: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(added Arturo)
m (→‎{{header|Wren}}: Changed to Wren S/H)
(17 intermediate revisions by 9 users not shown)
Line 19:
121 169 225 289 361 441 529 625 729 841 961
</pre>
 
=={{header|8080 Assembly}}==
<syntaxhighlight lang="asm"> org 100h
lxi h,81 ; Holds current square
lxi d,19 ; Holds distance to next square
mvi b,12 ; Loop counter
jmp next
loop: call prhl
next: dad d ; Generate next square (will be even)
inx d ; Increase distance by 2
inx d
dad d ; Generate next square (will be odd)
inx d ; Increase distance by 2
inx d
dcr b
jnz loop
ret
; Print HL as decimal
prhl: push h ; Save all registers
push d
push b
lxi b,pnum ; Store pointer to num string on stack
push b
lxi b,-10 ; Divisor
prdgt: lxi d,-1
prdgtl: inx d ; Divide by 10 through trial subtraction
dad b
jc prdgtl
mvi a,'0'+10
add l ; L = remainder - 10
pop h ; Get pointer from stack
dcx h ; Store digit
mov m,a
push h ; Put pointer back on stack
xchg ; Put quotient in HL
mov a,h ; Check if zero
ora l
jnz prdgt ; If not, next digit
pop d ; Get pointer and put in DE
mvi c,9 ; CP/M print string
call 5
pop b ; Restore registers
pop d
pop h
ret
db '*****' ; Placeholder for number
pnum: db 13,10,'$'</syntaxhighlight>
{{out}}
<pre>121
169
225
289
361
441
529
625
729
841
961</pre>
 
=={{header|ALGOL 68}}==
Line 148 ⟶ 208:
 
Here it's known that the final output should have the transformation <code>ט1+2×⊢</code> applied to it to produce odd squares. The reverse of this transformation is applied to the two bounds 100 and 1000, then <code>↓⟜↕</code> produces a numeric range which is transformed back.
 
=={{header|C}}==
{{trans|Wren}}
<syntaxhighlight lang="c">#include <stdio.h>
#include <math.h>
 
int main() {
int i, p, low, high, pow = 1, osc;
int oddSq[120];
for (p = 0; p < 5; ++p) {
low = (int)ceil(sqrt((double)pow));
if (!(low%2)) ++low;
pow *= 10;
high = (int)sqrt((double)pow);
for (i = low, osc = 0; i <= high; i += 2) {
oddSq[osc++] = i * i;
}
printf("%d odd square from %d to %d:\n", osc, pow/10, pow);
for (i = 0; i < osc; ++i) {
printf("%d ", oddSq[i]);
if (!((i+1)%10)) printf("\n");
}
printf("\n\n");
}
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>
Same as Wren example.
</pre>
 
=={{header|CLU}}==
Line 235 ⟶ 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 257 ⟶ 388:
841
961</pre>
 
=={{header|Euler}}==
Same algorithm as the Algol and other samples.
<br>Note formatted output is not Euler's strong point...
'''begin'''
'''new''' toNext; '''new''' oddSquare; '''label''' again;
toNext &lt;- 0;
oddSquare &lt;- 1;
again:
'''if''' oddSquare &lt; 1000 '''then''' '''begin'''
'''if''' oddSquare &gt; 99 '''then''' '''out''' oddSquare '''else''' 0;
oddSquare &lt;- oddSquare + toNext;
toNext &lt;- toNext + 8;
'''goto''' again
'''end''' '''else''' 0
'''end''' $
{{out}}
<pre>
NUMBER 121
NUMBER 169
NUMBER 225
NUMBER 289
NUMBER 361
NUMBER 441
NUMBER 529
NUMBER 625
NUMBER 729
NUMBER 841
NUMBER 961
</pre>
 
=={{header|F_Sharp|F#}}==
Line 286 ⟶ 449:
<pre>
121 169 225 289 361 441 529 625 729 841 961
</pre>
 
=={{header|Fe}}==
<syntaxhighlight lang="clojure">
(= oddAndSquareNumbers
(fn (minNumber maxNumber)
(let toNext 8)
(let oddSquare 1)
(let lastResult (cons 0 nil)) ; result list with a dummy leading 0
(let result lastResult)
(while (< oddSquare maxNumber)
(if (< minNumber oddSquare)
(do (setcdr lastResult (cons oddSquare nil))
(= lastResult (cdr lastResult))
)
)
(= oddSquare (+ oddSquare toNext))
(= toNext (+ toNext 8))
)
(cdr result) ; return result without the dummy leading 0
)
)
(print (oddAndSquareNumbers 100 1000))
</syntaxhighlight>
{{out}}
<pre>
(121 169 225 289 361 441 529 625 729 841 961)
</pre>
 
Line 443 ⟶ 633:
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>
 
=={{header|MACRO-11}}==
<syntaxhighlight lang="macro11"> .TITLE ODDSQR
.MCALL .TTYOUT,.EXIT
ODDSQR::MOV #^D81,R3
MOV #^D19,R4
BR $2
$1: MOV R3,R0
JSR PC,PR0
$2: ADD R4,R3
ADD #2,R4
ADD R4,R3
ADD #2,R4
CMP R3,#^D1000
BLT $1
.EXIT
 
; PRINT NUMBER IN R0 AS DECIMAL
PR0: MOV #4$,R1
1$: MOV #-1,R2
2$: INC R2
SUB #12,R0
BCC 2$
ADD #72,R0
MOVB R0,-(R1)
MOV R2,R0
BNE 1$
3$: MOVB (R1)+,R0
.TTYOUT
BNE 3$
RTS PC
.ASCII /...../
4$: .BYTE 15,12,0
.END ODDSQR</syntaxhighlight>
{{out}}
<pre>121
169
225
289
361
441
529
625
729
841
961</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Line 450 ⟶ 700:
{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>
 
=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main = [Stdout (show taskresults),
Stdout "\n"]
taskresults = dropwhile (< minimum) (takewhile (< maximum) oddsquares)
oddsquares = map (^ 2) odds
odds = [1, 3..]
minimum = 101
maximum = 1000</syntaxhighlight>
{{out}}
<pre>[121,169,225,289,361,441,529,625,729,841,961]</pre>
 
=={{header|Modula-2}}==
Line 502 ⟶ 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 532 ⟶ 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 686 ⟶ 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 700 ⟶ 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 810 ⟶ 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>
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">lo, hi = 100, 1000
(Integer.sqrt(lo)..Integer.sqrt(hi)).each{|n| puts n*n if n.odd?}</syntaxhighlight>
{{out}}
<pre>
121
169
225
289
361
441
529
625
729
841
961
</pre>
 
Line 858 ⟶ 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 863 ⟶ 1,217:
{{libheader|Wren-iterate}}
{{libheader|Wren-seq}}
<syntaxhighlight lang="ecmascriptwren">import "./iterate" for Stepped
import "./seq" for Lst
 
9,477

edits