Odd and square numbers: Difference between revisions
Not a robot (talk | contribs) (Add BCPL) |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(35 intermediate revisions by 18 users not shown) | |||
Line 8: | Line 8: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">V limit = 1000 |
||
L(i) (1 .< Int(ceil(sqrt(limit)))).step(2) |
L(i) (1 .< Int(ceil(sqrt(limit)))).step(2) |
||
V num = i * i |
V num = i * i |
||
I num < limit & num > 99 |
I num < limit & num > 99 |
||
print(num, end' ‘ ’)</ |
print(num, end' ‘ ’)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 19: | Line 19: | ||
121 169 225 289 361 441 529 625 729 841 961 |
121 169 225 289 361 441 529 625 729 841 961 |
||
</pre> |
</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}}== |
=={{header|ALGOL 68}}== |
||
< |
<syntaxhighlight lang="algol68">BEGIN # print odd suares between 100 and 1000 # |
||
# if 2m + 1 and 2m - 1 are consecutive odd numbers, the difference between their squares is 8m # |
# if 2m + 1 and 2m - 1 are consecutive odd numbers, the difference between their squares is 8m # |
||
INT to next := 8; |
INT to next := 8; |
||
Line 32: | Line 92: | ||
to next +:= 8 |
to next +:= 8 |
||
OD |
OD |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 40: | Line 100: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
{{Trans|PL/M}} which is based on the Algol 68 sample. |
{{Trans|PL/M}} which is based on the Algol 68 sample. |
||
< |
<syntaxhighlight lang="algolw">begin % print odd squares between 100 and 1000 % |
||
integer oddSquare, nextGap; |
integer oddSquare, nextGap; |
||
oddSquare := 1; |
oddSquare := 1; |
||
Line 53: | Line 113: | ||
nextGap := nextGap + 8 |
nextGap := nextGap + 8 |
||
end while_oddSquare_lt_1000 |
end while_oddSquare_lt_1000 |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
121 169 225 289 361 441 529 625 729 841 961 |
121 169 225 289 361 441 529 625 729 841 961 |
||
</pre> |
</pre> |
||
=={{header|Arturo}}== |
|||
<syntaxhighlight lang="arturo">100..1000 | select => odd? |
|||
| select 'x -> zero? (sqrt x) % 1 |
|||
| print</syntaxhighlight> |
|||
{{out}} |
|||
<pre>121 169 225 289 361 441 529 625 729 841 961</pre> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f ODD_AND_SQUARE_NUMBERS.AWK |
# syntax: GAWK -f ODD_AND_SQUARE_NUMBERS.AWK |
||
BEGIN { |
BEGIN { |
||
Line 74: | Line 144: | ||
exit(0) |
exit(0) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 83: | Line 153: | ||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
< |
<syntaxhighlight lang="basic">10 DEFINT A-Z |
||
20 N=10 |
20 N=10 |
||
30 S=N*N |
30 S=N*N |
||
Line 89: | Line 159: | ||
50 IF S AND 1 THEN PRINT S |
50 IF S AND 1 THEN PRINT S |
||
60 N=N+1 |
60 N=N+1 |
||
70 GOTO 30</ |
70 GOTO 30</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 121 |
<pre> 121 |
||
Line 104: | Line 174: | ||
=={{header|BCPL}}== |
=={{header|BCPL}}== |
||
< |
<syntaxhighlight lang="bcpl">get "libhdr" |
||
let start() be |
let start() be |
||
Line 113: | Line 183: | ||
n := n + 1 |
n := n + 1 |
||
$) repeat |
$) repeat |
||
$)</ |
$)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>121 |
<pre>121 |
||
Line 129: | Line 199: | ||
=={{header|BQN}}== |
=={{header|BQN}}== |
||
<lang |
<syntaxhighlight lang="bqn">ט11+2×↕11</syntaxhighlight> |
||
Generate odd numbers from 11 to 31 and square them. |
Generate odd numbers from 11 to 31 and square them. |
||
Line 135: | Line 205: | ||
An alternate version uses more code, but doesn't require any arithmetic to derive: |
An alternate version uses more code, but doesn't require any arithmetic to derive: |
||
< |
<syntaxhighlight lang="bqn">100 ↓⟜↕○⌈⌾((ט1+2×⊢)⁼) 1000</syntaxhighlight> |
||
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. |
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}}== |
=={{header|CLU}}== |
||
< |
<syntaxhighlight lang="clu">start_up = proc () |
||
po: stream := stream$primary_output() |
po: stream := stream$primary_output() |
||
n: int := 10 |
n: int := 10 |
||
Line 149: | Line 250: | ||
n := n+1 |
n := n+1 |
||
end |
end |
||
end start_up</ |
end start_up</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>121 |
<pre>121 |
||
Line 164: | Line 265: | ||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
< |
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION. |
||
PROGRAM-ID. ODD-AND-SQUARE. |
PROGRAM-ID. ODD-AND-SQUARE. |
||
Line 186: | Line 287: | ||
CHECK. |
CHECK. |
||
MULTIPLY N BY N GIVING SQR. |
MULTIPLY N BY N GIVING SQR. |
||
IF ODD, MOVE SQR TO FMT, DISPLAY FMT.</ |
IF ODD, MOVE SQR TO FMT, DISPLAY FMT.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>121 |
<pre>121 |
||
Line 201: | Line 302: | ||
=={{header|Cowgol}}== |
=={{header|Cowgol}}== |
||
< |
<syntaxhighlight lang="cowgol">include "cowgol.coh"; |
||
var n: uint16 := 10; |
var n: uint16 := 10; |
||
Line 212: | Line 313: | ||
end if; |
end if; |
||
n := n+1; |
n := n+1; |
||
end loop;</ |
end loop;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>121 |
<pre>121 |
||
Line 225: | Line 326: | ||
841 |
841 |
||
961</pre> |
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}}== |
|||
<syntaxhighlight lang="draco">proc nonrec main() void: |
|||
word i, sq; |
|||
i := 11; |
|||
while sq := i * i; sq < 1000 do |
|||
writeln(sq); |
|||
i := i + 2 |
|||
od |
|||
corp</syntaxhighlight> |
|||
{{out}} |
|||
<pre>121 |
|||
169 |
|||
225 |
|||
289 |
|||
361 |
|||
441 |
|||
529 |
|||
625 |
|||
729 |
|||
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 <- 0; |
|||
oddSquare <- 1; |
|||
again: |
|||
'''if''' oddSquare < 1000 '''then''' '''begin''' |
|||
'''if''' oddSquare > 99 '''then''' '''out''' oddSquare '''else''' 0; |
|||
oddSquare <- oddSquare + toNext; |
|||
toNext <- 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#}}== |
=={{header|F_Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Odd and square numbers. Nigel Galloway: November 23rd., 2021 |
// Odd and square numbers. Nigel Galloway: November 23rd., 2021 |
||
Seq.initInfinite((*)2>>(+)11)|>Seq.map(fun n->n*n)|>Seq.takeWhile((>)1000)|>Seq.iter(printfn "%d") |
Seq.initInfinite((*)2>>(+)11)|>Seq.map(fun n->n*n)|>Seq.takeWhile((>)1000)|>Seq.iter(printfn "%d") |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 248: | Line 443: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.99 2021-06-02}} |
{{works with|Factor|0.99 2021-06-02}} |
||
< |
<syntaxhighlight lang="factor">USING: io math math.functions math.ranges prettyprint sequences ; |
||
11 1000 sqrt 2 <range> [ bl ] [ sq pprint ] interleave nl</ |
11 1000 sqrt 2 <range> [ bl ] [ sq pprint ] interleave nl</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
121 169 225 289 361 441 529 625 729 841 961 |
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> |
</pre> |
||
=={{header|Fermat}}== |
=={{header|Fermat}}== |
||
< |
<syntaxhighlight lang="fermat">Func Oddsq(j)=(2*j-1)^2.; |
||
i:=1; |
i:=1; |
||
n:=1; |
n:=1; |
||
Line 264: | Line 486: | ||
i:+; |
i:+; |
||
n:=Oddsq(i); |
n:=Oddsq(i); |
||
od;</ |
od;</syntaxhighlight> |
||
=={{header|FOCAL}}== |
=={{header|FOCAL}}== |
||
< |
<syntaxhighlight lang="focal">01.10 S N=10 |
||
01.20 S S=N*N |
01.20 S S=N*N |
||
01.30 I (1000-S)1.8 |
01.30 I (1000-S)1.8 |
||
Line 274: | Line 496: | ||
01.60 S N=N+1 |
01.60 S N=N+1 |
||
01.70 G 1.2 |
01.70 G 1.2 |
||
01.80 Q</ |
01.80 Q</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>= 121 |
<pre>= 121 |
||
Line 290: | Line 512: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
Squares without squaring. |
Squares without squaring. |
||
< |
<syntaxhighlight lang="freebasic">dim as integer i=1, n=1 |
||
while n<1000 |
while n<1000 |
||
if n>100 then print n |
if n>100 then print n |
||
n+=8*i |
n+=8*i |
||
i+=1 |
i+=1 |
||
wend</ |
wend</syntaxhighlight> |
||
=={{header|Go}}== |
|||
{{trans|Wren}} |
|||
<syntaxhighlight lang="go">package main |
|||
import ( |
|||
"fmt" |
|||
"math" |
|||
) |
|||
func main() { |
|||
pow := 1 |
|||
for p := 0; p < 5; p++ { |
|||
low := int(math.Ceil(math.Sqrt(float64(pow)))) |
|||
if low%2 == 0 { |
|||
low++ |
|||
} |
|||
pow *= 10 |
|||
high := int(math.Sqrt(float64(pow))) |
|||
var oddSq []int |
|||
for i := low; i <= high; i += 2 { |
|||
oddSq = append(oddSq, i*i) |
|||
} |
|||
fmt.Println(len(oddSq), "odd squares from", pow/10, "to", pow, "\b:") |
|||
for i := 0; i < len(oddSq); i++ { |
|||
fmt.Printf("%d ", oddSq[i]) |
|||
if (i+1)%10 == 0 { |
|||
fmt.Println() |
|||
} |
|||
} |
|||
fmt.Println("\n") |
|||
} |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
Same as Wren example |
|||
</pre> |
|||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">main :: IO () |
||
main = print $ takeWhile (<1000) $ filter odd $ map (^2) $ [10..]</ |
main = print $ takeWhile (<1000) $ filter odd $ map (^2) $ [10..]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>[121,169,225,289,361,441,529,625,729,841,961]</pre> |
<pre>[121,169,225,289,361,441,529,625,729,841,961]</pre> |
||
=={{header|J}}== |
|||
Example implementation: |
|||
<syntaxhighlight lang="j"> (#~ (1=2|])*(=<.)@%:*>&99) i.1000 |
|||
121 169 225 289 361 441 529 625 729 841 961</syntaxhighlight> |
|||
Note that we could have instead used cascading filters (which would be roughly analogous to short circuit operators) for example: |
|||
<syntaxhighlight lang="j"> (#~ 1=2|]) (#~ (=<.)@%:) 99}. i.1000 |
|||
121 169 225 289 361 441 529 625 729 841 961</syntaxhighlight> |
|||
Or, we could instead have opted to not use filters at all, because the values are their own indices in the initial selection we were working with: |
|||
<syntaxhighlight lang="j"> I.((1=2|])*(=<.)@%:*>&99) i.1000 |
|||
121 169 225 289 361 441 529 625 729 841 961</syntaxhighlight> |
|||
=={{header|jq}}== |
=={{header|jq}}== |
||
Line 307: | Line 584: | ||
'''Works with gojq, the Go implementation of jq''' |
'''Works with gojq, the Go implementation of jq''' |
||
====Basic Task==== |
====Basic Task==== |
||
< |
<syntaxhighlight lang="jq"># Output: a stream up to but less than $upper |
||
def oddSquares($upper): |
def oddSquares($upper): |
||
label $out |
label $out |
||
Line 315: | Line 592: | ||
oddSquares(1000) | select(. > 100) |
oddSquares(1000) | select(. > 100) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
As for [[#Julia]]. |
As for [[#Julia]]. |
||
Line 321: | Line 598: | ||
====Extended Example==== |
====Extended Example==== |
||
{{trans|Wren}} |
{{trans|Wren}} |
||
< |
<syntaxhighlight lang="jq"># input: an array |
||
# output: a stream of arrays of size size except possibly for the last array |
# output: a stream of arrays of size size except possibly for the last array |
||
def group(size): |
def group(size): |
||
Line 333: | Line 610: | ||
[range(.low; 1 + (.pow|sqrt|floor); 2) | . * . ] as $oddSq |
[range(.low; 1 + (.pow|sqrt|floor); 2) | . * . ] as $oddSq |
||
| "\($oddSq|length) odd squares from \(.pow/10) to \(.pow):", |
| "\($oddSq|length) odd squares from \(.pow/10) to \(.pow):", |
||
( $oddSq | group(10) | join(" ")), "" )</ |
( $oddSq | group(10) | join(" ")), "" )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
As for [[#Wren]]. |
As for [[#Wren]]. |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
<syntaxhighlight lang="julia">oddsquares(lim) = [i^2 for i ∈ Int.(range((√).(lim)...)) if isodd(i)] |
|||
{{trans|FreeBASIC}} |
|||
oddsquares((100, 999)) |
|||
<lang julia>julia> i = n = 1 |
|||
</syntaxhighlight> |
|||
1 |
|||
{{Out}} |
|||
<pre> |
|||
11-element Vector{Int64}: |
|||
121 |
|||
169 |
|||
225 |
|||
289 |
|||
361 |
|||
441 |
|||
529 |
|||
625 |
|||
729 |
|||
841 |
|||
961 |
|||
</pre> |
|||
=={{header|Lua}}== |
|||
julia> while n < 1000 |
|||
<syntaxhighlight lang="lua"> |
|||
n > 100 && println(n) |
|||
for i = 1, math.sqrt( 1000 ), 2 do |
|||
i |
local i2 = i * i |
||
if i2 > 99 then |
|||
io.write( " ", i2 ) |
|||
121 |
|||
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 |
169 |
||
225 |
225 |
||
Line 357: | Line 692: | ||
729 |
729 |
||
841 |
841 |
||
961 |
961</pre> |
||
</lang> |
|||
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
=={{header|Mathematica}} / {{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">Cases[Range[100, 1000], _?(IntegerQ[Sqrt@#] && OddQ[#] &)]</syntaxhighlight> |
||
{{out}}<pre> |
{{out}}<pre> |
||
{121,169,225,289,361,441,529,625,729,841,961} |
{121,169,225,289,361,441,529,625,729,841,961} |
||
</pre> |
</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}}== |
=={{header|Modula-2}}== |
||
< |
<syntaxhighlight lang="modula2">MODULE OddSquare; |
||
FROM InOut IMPORT WriteCard, WriteLn; |
FROM InOut IMPORT WriteCard, WriteLn; |
||
VAR n, square: CARDINAL; |
VAR n, square: CARDINAL; |
||
Line 381: | Line 742: | ||
n := n + 1 |
n := n + 1 |
||
END |
END |
||
END OddSquare.</ |
END OddSquare.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>121 |
<pre>121 |
||
Line 394: | Line 755: | ||
841 |
841 |
||
961</pre> |
961</pre> |
||
=={{header|Modula-3}}== |
|||
{{trans|Modula-2}} |
|||
<syntaxhighlight lang="modula3">MODULE OddSquare EXPORTS Main; |
|||
IMPORT IO; |
|||
VAR N,Square:CARDINAL; |
|||
BEGIN |
|||
N := 10; |
|||
LOOP |
|||
Square := N * N; |
|||
IF Square > 1000 THEN EXIT END; |
|||
IF Square MOD 2 = 1 THEN |
|||
IO.PutInt(Square); |
|||
IO.Put("\n"); |
|||
END; |
|||
N := N + 1 |
|||
END |
|||
END OddSquare.</syntaxhighlight> |
|||
<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 |
|||
289 |
|||
361 |
|||
441 |
|||
529 |
|||
625 |
|||
729 |
|||
841 |
|||
961 |
|||
</pre> |
|||
=={{header|Objeck}}== |
=={{header|Objeck}}== |
||
< |
<syntaxhighlight lang="objeck">class OddSquare { |
||
function : Main(args : String[]) ~ Nil { |
function : Main(args : String[]) ~ Nil { |
||
i:=n:=1; |
i:=n:=1; |
||
Line 405: | Line 821: | ||
""->PrintLine(); |
""->PrintLine(); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
Line 411: | Line 827: | ||
121 169 225 289 361 441 529 625 729 841 961 |
121 169 225 289 361 441 529 625 729 841 961 |
||
</pre> |
</pre> |
||
=={{header|OCaml}}== |
|||
<syntaxhighlight lang="ocaml">let seq_odd_squares = |
|||
let rec next n a () = Seq.Cons (n, next (n + a) (a + 8)) in |
|||
next 1 8 |
|||
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> |
|||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict; |
use strict; |
||
Line 420: | Line 846: | ||
use ntheory qw( is_square ); |
use ntheory qw( is_square ); |
||
print join( ' ', grep $_ & 1 && is_square($_), 100 .. 999 ), "\n";</ |
print join( ' ', grep $_ & 1 && is_square($_), 100 .. 999 ), "\n";</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 427: | Line 853: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_power</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">)),</span><span style="color: #000000;">11</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span><span style="color: #000000;">2</span><span style="color: #0000FF;">))</span> |
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_power</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">)),</span><span style="color: #000000;">11</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span><span style="color: #000000;">2</span><span style="color: #0000FF;">))</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
{121,169,225,289,361,441,529,625,729,841,961} |
{121,169,225,289,361,441,529,625,729,841,961} |
||
</pre> |
</pre> |
||
=={{header|PILOT}}== |
|||
<syntaxhighlight lang="pilot">C :n=9 |
|||
*loop |
|||
C :n=#n+1 |
|||
C :sq=#n*#n |
|||
C :sr=(#sq/2)*2 |
|||
T (sq<>sr):#sq |
|||
J (sq<1000):*loop</syntaxhighlight> |
|||
{{out}} |
|||
<pre>121 |
|||
169 |
|||
225 |
|||
289 |
|||
361 |
|||
441 |
|||
529 |
|||
625 |
|||
729 |
|||
841 |
|||
961</pre> |
|||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
Line 443: | Line 890: | ||
Based on the Algol 68 sample. |
Based on the Algol 68 sample. |
||
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator) |
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator) |
||
< |
<syntaxhighlight lang="pli">100H: /* PRINT ODD SQUARES BETWEEN 100 AND 1000 */ |
||
/* CP/M BDOS SYSTEM CALL */ |
/* CP/M BDOS SYSTEM CALL */ |
||
Line 478: | Line 925: | ||
END; |
END; |
||
EOF</ |
EOF</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 491: | Line 938: | ||
<br> |
<br> |
||
The PL/I include file "pg.inc" can be found on the [[Polyglot:PL/I and PL/M]] page. Note the use of text in column 81 onwards to hide the PL/I specifics from the PL/M compiler. |
The PL/I include file "pg.inc" can be found on the [[Polyglot:PL/I and PL/M]] page. Note the use of text in column 81 onwards to hide the PL/I specifics from the PL/M compiler. |
||
< |
<syntaxhighlight lang="pli">/* PRINT ODD SQUARES BETWEEN 100 AND 1000 */ |
||
odd_squares_100H: procedure options (main); |
odd_squares_100H: procedure options (main); |
||
Line 527: | Line 974: | ||
END; |
END; |
||
EOF: end odd_squares_100H;</ |
EOF: end odd_squares_100H;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 534: | Line 981: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
< |
<syntaxhighlight lang="python"> |
||
import math |
import math |
||
szamok=[] |
szamok = [] |
||
limit = 1000 |
limit = 1000 |
||
for i in range(1, |
for i in range(1, math.isqrt(limit - 1) + 1, 2): |
||
num = i*i |
num = i*i |
||
if ( |
if (num > 99): |
||
szamok.append(num) |
szamok.append(num) |
||
print(szamok) |
print(szamok) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
[121, 169, 225, 289, 361, 441, 529, 625, 729, 841, 961] |
[121, 169, 225, 289, 361, 441, 529, 625, 729, 841, 961] |
||
</pre> |
</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}}== |
|||
<syntaxhighlight lang="Quackery"> [] 1 0 |
|||
[ 8 + dup dip + |
|||
over 100 > until ] |
|||
[ dip |
|||
[ tuck join swap ] |
|||
8 + dup dip + |
|||
over 1000 > until ] |
|||
2drop |
|||
echo</syntaxhighlight> |
|||
{{out}} |
|||
<pre>[ 121 169 225 289 361 441 529 625 729 841 961 ]</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
Vote for deletion: trivial. But if we gotta keep it, at least make it ''slightly'' interesting. |
Vote for deletion: trivial. But if we gotta keep it, at least make it ''slightly'' interesting. |
||
<lang |
<syntaxhighlight lang="raku" line>for 1..5 { |
||
my $max = exp $_, 10; |
my $max = exp $_, 10; |
||
put "\n{+$_} odd squares from {$max / 10} to $max:\n{ .batch(10).join: "\n" }" |
put "\n{+$_} odd squares from {$max / 10} to $max:\n{ .batch(10).join: "\n" }" |
||
given ({(2 × $++ + 1)²} … * > $max).grep: $max / 10 ≤ * ≤ $max |
given ({(2 × $++ + 1)²} … * > $max).grep: $max / 10 ≤ * ≤ $max |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>2 odd squares from 1 to 10: |
<pre>2 odd squares from 1 to 10: |
||
Line 589: | Line 1,059: | ||
=={{header|Red}}== |
=={{header|Red}}== |
||
< |
<syntaxhighlight lang="rebol">Red[] |
||
n: 11 |
n: 11 |
||
Line 596: | Line 1,066: | ||
print n * n |
print n * n |
||
n: n + 2 |
n: n + 2 |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 613: | Line 1,083: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
see "working..." + nl |
see "working..." + nl |
||
limit = 1000 |
limit = 1000 |
||
Line 638: | Line 1,108: | ||
txt = txt + "]" |
txt = txt + "]" |
||
see txt |
see txt |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 644: | Line 1,114: | ||
[121,169,225,289,361,441,529,625,729,841,961] |
[121,169,225,289,361,441,529,625,729,841,961] |
||
done... |
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> |
|||
=={{header|Sidef}}== |
|||
<syntaxhighlight lang="ruby">var lo = 100 |
|||
var hi = 1_000 |
|||
say gather { |
|||
for k in (lo.isqrt .. hi.isqrt) { |
|||
take(k**2) if k.is_odd |
|||
} |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
[121, 169, 225, 289, 361, 441, 529, 625, 729, 841, 961] |
|||
</pre> |
|||
=={{header|V (Vlang)}}== |
|||
{{trans|Go}} |
|||
<syntaxhighlight lang="v (vlang)">import math |
|||
fn main() { |
|||
mut pow := 1 |
|||
for _ in 0..5 { |
|||
mut low := int(math.ceil(math.sqrt(f64(pow)))) |
|||
if low%2 == 0 { |
|||
low++ |
|||
} |
|||
pow *= 10 |
|||
high := int(math.sqrt(f64(pow))) |
|||
mut odd_sq := []int{} |
|||
for i := low; i <= high; i += 2 { |
|||
odd_sq << i*i |
|||
} |
|||
println("$odd_sq.len odd squares from ${pow/10} to $pow, \b:") |
|||
for i in 0..odd_sq.len { |
|||
print("${odd_sq[i]} ") |
|||
if (i+1)%10 == 0 { |
|||
println('') |
|||
} |
|||
} |
|||
println("\n") |
|||
} |
|||
}</syntaxhighlight> |
|||
{{out}} |
|||
<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> |
</pre> |
||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren- |
{{libheader|Wren-iterate}} |
||
{{libheader|Wren-seq}} |
{{libheader|Wren-seq}} |
||
< |
<syntaxhighlight lang="wren">import "./iterate" for Stepped |
||
import "./seq" for Lst |
import "./seq" for Lst |
||
Line 662: | Line 1,230: | ||
for (chunk in Lst.chunks(oddSq, 10)) System.print(chunk.join(" ")) |
for (chunk in Lst.chunks(oddSq, 10)) System.print(chunk.join(" ")) |
||
System.print() |
System.print() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 697: | Line 1,265: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">int N2, N; |
||
[for N2:= 101 to 999 do |
[for N2:= 101 to 999 do |
||
[N:= sqrt(N2); |
[N:= sqrt(N2); |
||
Line 703: | Line 1,271: | ||
[IntOut(0, N2); ChOut(0, ^ )]; |
[IntOut(0, N2); ChOut(0, ^ )]; |
||
]; |
]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
Revision as of 09:08, 8 January 2024
- Task
Find odd and square numbers (>99) under 1.000
11l
V limit = 1000
L(i) (1 .< Int(ceil(sqrt(limit)))).step(2)
V num = i * i
I num < limit & num > 99
print(num, end' ‘ ’)
- Output:
121 169 225 289 361 441 529 625 729 841 961
8080 Assembly
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,'$'
- Output:
121 169 225 289 361 441 529 625 729 841 961
ALGOL 68
BEGIN # print odd suares between 100 and 1000 #
# if 2m + 1 and 2m - 1 are consecutive odd numbers, the difference between their squares is 8m #
INT to next := 8;
INT odd square := 1;
WHILE odd square < 1000 DO
IF odd square > 99 THEN
print( ( " ", whole( odd square, 0 ) ) )
FI;
odd square +:= to next;
to next +:= 8
OD
END
- Output:
121 169 225 289 361 441 529 625 729 841 961
ALGOL W
which is based on the Algol 68 sample.
begin % print odd squares between 100 and 1000 %
integer oddSquare, nextGap;
oddSquare := 1;
nextGap := 8;
while oddSquare < 100 do begin
oddSquare := oddSquare + nextGap;
nextGap := nextGap + 8
end while_oddSuare_lt_100 ;
while oddSquare < 1000 do begin
writeon( i_w := s_w := 1, oddSquare );
oddSquare := oddSquare + nextGap;
nextGap := nextGap + 8
end while_oddSquare_lt_1000
end.
- Output:
121 169 225 289 361 441 529 625 729 841 961
Arturo
100..1000 | select => odd?
| select 'x -> zero? (sqrt x) % 1
| print
- Output:
121 169 225 289 361 441 529 625 729 841 961
AWK
# syntax: GAWK -f ODD_AND_SQUARE_NUMBERS.AWK
BEGIN {
start = 100
stop = 999
i = n = 1
while (n <= stop) {
if (n >= start) {
printf("%5d%1s",n,++count%10?"":"\n")
}
n += 8 * i++
}
printf("\nOdd and square numbers %d-%d: %d\n",start,stop,count)
exit(0)
}
- Output:
121 169 225 289 361 441 529 625 729 841 961 Odd and square numbers 100-999: 11
BASIC
10 DEFINT A-Z
20 N=10
30 S=N*N
40 IF S>=1000 THEN END
50 IF S AND 1 THEN PRINT S
60 N=N+1
70 GOTO 30
- Output:
121 169 225 289 361 441 529 625 729 841 961
BCPL
get "libhdr"
let start() be
$( let n = 10
$( let sq = n * n
if sq >= 1000 then finish
if sq rem 2 = 1 then writef("%N*N", sq)
n := n + 1
$) repeat
$)
- Output:
121 169 225 289 361 441 529 625 729 841 961
BQN
ט11+2×↕11
Generate odd numbers from 11 to 31 and square them.
An alternate version uses more code, but doesn't require any arithmetic to derive:
100 ↓⟜↕○⌈⌾((ט1+2×⊢)⁼) 1000
Here it's known that the final output should have the transformation ט1+2×⊢
applied to it to produce odd squares. The reverse of this transformation is applied to the two bounds 100 and 1000, then ↓⟜↕
produces a numeric range which is transformed back.
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;
}
- Output:
Same as Wren example.
CLU
start_up = proc ()
po: stream := stream$primary_output()
n: int := 10
while true do
sq: int := n**2
if sq>=1000 then break end
if sq//2 = 1 then stream$putl(po, int$unparse(sq)) end
n := n+1
end
end start_up
- Output:
121 169 225 289 361 441 529 625 729 841 961
COBOL
IDENTIFICATION DIVISION.
PROGRAM-ID. ODD-AND-SQUARE.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 VARIABLES.
03 N PIC 999.
03 SQR PIC 9999 VALUE 0.
03 FILLER REDEFINES SQR.
05 FILLER PIC 999.
05 FILLER PIC 9.
88 ODD VALUE 1, 3, 5, 7, 9.
03 FMT PIC ZZ9.
PROCEDURE DIVISION.
BEGIN.
PERFORM CHECK VARYING N FROM 10 BY 1
UNTIL SQR IS NOT LESS THAN 1000.
STOP RUN.
CHECK.
MULTIPLY N BY N GIVING SQR.
IF ODD, MOVE SQR TO FMT, DISPLAY FMT.
- Output:
121 169 225 289 361 441 529 625 729 841 961
Cowgol
include "cowgol.coh";
var n: uint16 := 10;
loop
var sq := n * n;
if sq >= 1000 then break; end if;
if sq % 2 == 1 then
print_i16(sq);
print_nl();
end if;
n := n+1;
end loop;
- Output:
121 169 225 289 361 441 529 625 729 841 961
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;
- Output:
121 169 225 289 361 441 529 625 729 841 961 Count=11 Elapsed Time: 1.975 ms.
Draco
proc nonrec main() void:
word i, sq;
i := 11;
while sq := i * i; sq < 1000 do
writeln(sq);
i := i + 2
od
corp
- Output:
121 169 225 289 361 441 529 625 729 841 961
Euler
Same algorithm as the Algol and other samples.
Note formatted output is not Euler's strong point...
begin new toNext; new oddSquare; label again; toNext <- 0; oddSquare <- 1; again: if oddSquare < 1000 then begin if oddSquare > 99 then out oddSquare else 0; oddSquare <- oddSquare + toNext; toNext <- toNext + 8; goto again end else 0 end $
- Output:
NUMBER 121 NUMBER 169 NUMBER 225 NUMBER 289 NUMBER 361 NUMBER 441 NUMBER 529 NUMBER 625 NUMBER 729 NUMBER 841 NUMBER 961
F#
// Odd and square numbers. Nigel Galloway: November 23rd., 2021
Seq.initInfinite((*)2>>(+)11)|>Seq.map(fun n->n*n)|>Seq.takeWhile((>)1000)|>Seq.iter(printfn "%d")
- Output:
121 169 225 289 361 441 529 625 729 841 961
Factor
USING: io math math.functions math.ranges prettyprint sequences ;
11 1000 sqrt 2 <range> [ bl ] [ sq pprint ] interleave nl
- Output:
121 169 225 289 361 441 529 625 729 841 961
Fe
(= 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))
- Output:
(121 169 225 289 361 441 529 625 729 841 961)
Fermat
Func Oddsq(j)=(2*j-1)^2.;
i:=1;
n:=1;
while n<1000 do
if n>100 then !!n fi;
i:+;
n:=Oddsq(i);
od;
FOCAL
01.10 S N=10
01.20 S S=N*N
01.30 I (1000-S)1.8
01.40 I (FITR(S/2)*2-S)1.5,1.6
01.50 T %3,S,!
01.60 S N=N+1
01.70 G 1.2
01.80 Q
- Output:
= 121 = 169 = 225 = 289 = 361 = 441 = 529 = 625 = 729 = 841 = 961
FreeBASIC
Squares without squaring.
dim as integer i=1, n=1
while n<1000
if n>100 then print n
n+=8*i
i+=1
wend
Go
package main
import (
"fmt"
"math"
)
func main() {
pow := 1
for p := 0; p < 5; p++ {
low := int(math.Ceil(math.Sqrt(float64(pow))))
if low%2 == 0 {
low++
}
pow *= 10
high := int(math.Sqrt(float64(pow)))
var oddSq []int
for i := low; i <= high; i += 2 {
oddSq = append(oddSq, i*i)
}
fmt.Println(len(oddSq), "odd squares from", pow/10, "to", pow, "\b:")
for i := 0; i < len(oddSq); i++ {
fmt.Printf("%d ", oddSq[i])
if (i+1)%10 == 0 {
fmt.Println()
}
}
fmt.Println("\n")
}
}
- Output:
Same as Wren example
Haskell
main :: IO ()
main = print $ takeWhile (<1000) $ filter odd $ map (^2) $ [10..]
- Output:
[121,169,225,289,361,441,529,625,729,841,961]
J
Example implementation:
(#~ (1=2|])*(=<.)@%:*>&99) i.1000
121 169 225 289 361 441 529 625 729 841 961
Note that we could have instead used cascading filters (which would be roughly analogous to short circuit operators) for example:
(#~ 1=2|]) (#~ (=<.)@%:) 99}. i.1000
121 169 225 289 361 441 529 625 729 841 961
Or, we could instead have opted to not use filters at all, because the values are their own indices in the initial selection we were working with:
I.((1=2|])*(=<.)@%:*>&99) i.1000
121 169 225 289 361 441 529 625 729 841 961
jq
Works with gojq, the Go implementation of jq
Basic Task
# Output: a stream up to but less than $upper
def oddSquares($upper):
label $out
| 1, foreach range(1;infinite) as $i (1;
. + 8 * $i;
if . >= $upper then break $out else . end);
oddSquares(1000) | select(. > 100)
- Output:
As for #Julia.
Extended Example
# input: an array
# output: a stream of arrays of size size except possibly for the last array
def group(size):
recurse( .[size:]; length>0) | .[0:size];
foreach range(0; 5) as $p ({pow:1};
.low = (.pow|sqrt|ceil)
| if .low % 2 == 0 then .low += 1 else . end
| .pow *= 10 ;
[range(.low; 1 + (.pow|sqrt|floor); 2) | . * . ] as $oddSq
| "\($oddSq|length) odd squares from \(.pow/10) to \(.pow):",
( $oddSq | group(10) | join(" ")), "" )
- Output:
As for #Wren.
Julia
oddsquares(lim) = [i^2 for i ∈ Int.(range((√).(lim)...)) if isodd(i)]
oddsquares((100, 999))
- Output:
11-element Vector{Int64}: 121 169 225 289 361 441 529 625 729 841 961
Lua
for i = 1, math.sqrt( 1000 ), 2 do
local i2 = i * i
if i2 > 99 then
io.write( " ", i2 )
end
end
- Output:
121 169 225 289 361 441 529 625 729 841 961
MACRO-11
.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
- Output:
121 169 225 289 361 441 529 625 729 841 961
Mathematica / Wolfram Language
Cases[Range[100, 1000], _?(IntegerQ[Sqrt@#] && OddQ[#] &)]
- Output:
{121,169,225,289,361,441,529,625,729,841,961}
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);
- Output:
[121,169,225,289,361,441,529,625,729,841,961]
Miranda
main = [Stdout (show taskresults),
Stdout "\n"]
taskresults = dropwhile (< minimum) (takewhile (< maximum) oddsquares)
oddsquares = map (^ 2) odds
odds = [1, 3..]
minimum = 101
maximum = 1000
- Output:
[121,169,225,289,361,441,529,625,729,841,961]
Modula-2
MODULE OddSquare;
FROM InOut IMPORT WriteCard, WriteLn;
VAR n, square: CARDINAL;
BEGIN
n := 10;
LOOP
square := n * n;
IF square > 1000 THEN EXIT END;
IF square MOD 2 = 1 THEN
WriteCard(square, 3);
WriteLn
END;
n := n + 1
END
END OddSquare.
- Output:
121 169 225 289 361 441 529 625 729 841 961
Modula-3
MODULE OddSquare EXPORTS Main;
IMPORT IO;
VAR N,Square:CARDINAL;
BEGIN
N := 10;
LOOP
Square := N * N;
IF Square > 1000 THEN EXIT END;
IF Square MOD 2 = 1 THEN
IO.PutInt(Square);
IO.Put("\n");
END;
N := N + 1
END
END OddSquare.
121 169 225 289 361 441 529 625 729 841 961
Nim
import std/math
for n in countup(11, sqrt(1000.0).int, 2):
echo n * n
- Output:
121 169 225 289 361 441 529 625 729 841 961
Objeck
class OddSquare {
function : Main(args : String[]) ~ Nil {
i:=n:=1;
while(n < 1000) {
if(n > 100) { "{$n} "->Print(); };
n +=8*i; i+=1;
};
""->PrintLine();
}
}
- Output:
121 169 225 289 361 441 529 625 729 841 961
OCaml
let seq_odd_squares =
let rec next n a () = Seq.Cons (n, next (n + a) (a + 8)) in
next 1 8
let () =
seq_odd_squares |> Seq.drop_while ((>) 100) |> Seq.take_while ((>) 1000)
|> Seq.iter (Printf.printf " %u") |> print_newline
- Output:
121 169 225 289 361 441 529 625 729 841 961
Perl
#!/usr/bin/perl
use strict;
use warnings;
use ntheory qw( is_square );
print join( ' ', grep $_ & 1 && is_square($_), 100 .. 999 ), "\n";
- Output:
121 169 225 289 361 441 529 625 729 841 961
Phix
with javascript_semantics pp(sq_power(tagset(floor(sqrt(1000)),11,2),2))
- Output:
{121,169,225,289,361,441,529,625,729,841,961}
PILOT
C :n=9
*loop
C :n=#n+1
C :sq=#n*#n
C :sr=(#sq/2)*2
T (sq<>sr):#sq
J (sq<1000):*loop
- Output:
121 169 225 289 361 441 529 625 729 841 961
PL/I
PL/M
Based on the Algol 68 sample.
... under CP/M (or an emulator)
100H: /* PRINT ODD SQUARES BETWEEN 100 AND 1000 */
/* CP/M BDOS SYSTEM CALL */
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5;END;
/* CONSOLE OUTPUT ROUTINES */
PR$CHAR: PROCEDURE( C ); DECLARE C BYTE; CALL BDOS( 2, C ); END;
PR$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
PR$NUMBER: PROCEDURE( N );
DECLARE N ADDRESS;
DECLARE V ADDRESS, N$STR( 6 ) BYTE, W BYTE;
V = N;
W = LAST( N$STR );
N$STR( W ) = '$';
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
DO WHILE( ( V := V / 10 ) > 0 );
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
END;
CALL PR$STRING( .N$STR( W ) );
END PR$NUMBER;
/* TASK */
DECLARE ( NEXT$GAP, ODD$SQUARE ) ADDRESS;
NEXT$GAP = 8;
ODD$SQUARE = 1;
DO WHILE( ODD$SQUARE < 100 );
ODD$SQUARE = ODD$SQUARE + NEXT$GAP;
NEXT$GAP = NEXT$GAP + 8;
END;
DO WHILE( ODD$SQUARE < 1000 );
CALL PR$CHAR( ' ' );
CALL PR$NUMBER( ODD$SQUARE );
ODD$SQUARE = ODD$SQUARE + NEXT$GAP;
NEXT$GAP = NEXT$GAP + 8;
END;
EOF
- Output:
121 169 225 289 361 441 529 625 729 841 961
See also #Polyglot:PL/I and PL/M
Polyglot:PL/I and PL/M
... under CP/M (or an emulator)
Should work with many PL/I implementations.
The PL/I include file "pg.inc" can be found on the Polyglot:PL/I and PL/M page. Note the use of text in column 81 onwards to hide the PL/I specifics from the PL/M compiler.
/* PRINT ODD SQUARES BETWEEN 100 AND 1000 */
odd_squares_100H: procedure options (main);
/* PL/I DEFINITIONS */
%include 'pg.inc';
/* PL/M DEFINITIONS: CP/M BDOS SYSTEM CALL AND CONSOLE I/O ROUTINES, ETC. */ /*
DECLARE BINARY LITERALLY 'ADDRESS', CHARACTER LITERALLY 'BYTE';
BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
PRCHAR: PROCEDURE( C ); DECLARE C CHARACTER; CALL BDOS( 2, C ); END;
PRNUMBER: PROCEDURE( N );
DECLARE N ADDRESS;
DECLARE V ADDRESS, N$STR( 6 ) BYTE, W BYTE;
N$STR( W := LAST( N$STR ) ) = '$';
N$STR( W := W - 1 ) = '0' + ( ( V := N ) MOD 10 );
DO WHILE( ( V := V / 10 ) > 0 );
N$STR( W := W - 1 ) = '0' + ( V MOD 10 );
END;
CALL BDOS( 9, .N$STR( W ) );
END PRNUMBER;
/* END LANGUAGE DEFINITIONS */
/* TASK */
DECLARE ( NEXTGAP, ODDSQUARE ) BINARY;
NEXTGAP = 8;
ODDSQUARE = 1;
DO WHILE( ODDSQUARE < 100 );
ODDSQUARE = ODDSQUARE + NEXTGAP;
NEXTGAP = NEXTGAP + 8;
END;
DO WHILE( ODDSQUARE < 1000 );
CALL PRCHAR( ' ' );
CALL PRNUMBER( ODDSQUARE );
ODDSQUARE = ODDSQUARE + NEXTGAP;
NEXTGAP = NEXTGAP + 8;
END;
EOF: end odd_squares_100H;
- Output:
121 169 225 289 361 441 529 625 729 841 961
Python
import math
szamok = []
limit = 1000
for i in range(1, math.isqrt(limit - 1) + 1, 2):
num = i*i
if (num > 99):
szamok.append(num)
print(szamok)
- Output:
[121, 169, 225, 289, 361, 441, 529, 625, 729, 841, 961]
- By using itertools
from itertools import accumulate, count, dropwhile, takewhile
print(*takewhile(lambda x: x<1000, dropwhile(lambda x: x<100, accumulate(count(8, 8), initial=1))))
- Output:
121 169 225 289 361 441 529 625 729 841 961
Quackery
[] 1 0
[ 8 + dup dip +
over 100 > until ]
[ dip
[ tuck join swap ]
8 + dup dip +
over 1000 > until ]
2drop
echo
- Output:
[ 121 169 225 289 361 441 529 625 729 841 961 ]
Raku
Vote for deletion: trivial. But if we gotta keep it, at least make it slightly interesting.
for 1..5 {
my $max = exp $_, 10;
put "\n{+$_} odd squares from {$max / 10} to $max:\n{ .batch(10).join: "\n" }"
given ({(2 × $++ + 1)²} … * > $max).grep: $max / 10 ≤ * ≤ $max
}
- Output:
2 odd squares from 1 to 10: 1 9 3 odd squares from 10 to 100: 25 49 81 11 odd squares from 100 to 1000: 121 169 225 289 361 441 529 625 729 841 961 34 odd squares from 1000 to 10000: 1089 1225 1369 1521 1681 1849 2025 2209 2401 2601 2809 3025 3249 3481 3721 3969 4225 4489 4761 5041 5329 5625 5929 6241 6561 6889 7225 7569 7921 8281 8649 9025 9409 9801 108 odd squares from 10000 to 100000: 10201 10609 11025 11449 11881 12321 12769 13225 13689 14161 14641 15129 15625 16129 16641 17161 17689 18225 18769 19321 19881 20449 21025 21609 22201 22801 23409 24025 24649 25281 25921 26569 27225 27889 28561 29241 29929 30625 31329 32041 32761 33489 34225 34969 35721 36481 37249 38025 38809 39601 40401 41209 42025 42849 43681 44521 45369 46225 47089 47961 48841 49729 50625 51529 52441 53361 54289 55225 56169 57121 58081 59049 60025 61009 62001 63001 64009 65025 66049 67081 68121 69169 70225 71289 72361 73441 74529 75625 76729 77841 78961 80089 81225 82369 83521 84681 85849 87025 88209 89401 90601 91809 93025 94249 95481 96721 97969 99225
Red
Red[]
n: 11
limit: sqrt 1000
while [n < limit][
print n * n
n: n + 2
]
- Output:
121 169 225 289 361 441 529 625 729 841 961
Ring
see "working..." + nl
limit = 1000
list = []
for i = 1 to ceil(sqrt(limit)) step 2
num = pow(i,2)
if (num < 1000 and num > 99)
add(list,num)
ok
next
showArray(list)
see nl + "done..." + nl
func showArray(array)
txt = ""
see "["
for n = 1 to len(array)
txt = txt + array[n] + ","
next
txt = left(txt,len(txt)-1)
txt = txt + "]"
see txt
- Output:
working... [121,169,225,289,361,441,529,625,729,841,961] done...
RPL
≪ { } 99 999 FOR j IF j √ FP NOT THEN j + END 2 STEP ≫ EVAL
- Output:
1: { 121 169 225 289 361 441 529 625 729 841 961 }
Ruby
lo, hi = 100, 1000
(Integer.sqrt(lo)..Integer.sqrt(hi)).each{|n| puts n*n if n.odd?}
- Output:
121 169 225 289 361 441 529 625 729 841 961
Sidef
var lo = 100
var hi = 1_000
say gather {
for k in (lo.isqrt .. hi.isqrt) {
take(k**2) if k.is_odd
}
}
- Output:
[121, 169, 225, 289, 361, 441, 529, 625, 729, 841, 961]
V (Vlang)
import math
fn main() {
mut pow := 1
for _ in 0..5 {
mut low := int(math.ceil(math.sqrt(f64(pow))))
if low%2 == 0 {
low++
}
pow *= 10
high := int(math.sqrt(f64(pow)))
mut odd_sq := []int{}
for i := low; i <= high; i += 2 {
odd_sq << i*i
}
println("$odd_sq.len odd squares from ${pow/10} to $pow, \b:")
for i in 0..odd_sq.len {
print("${odd_sq[i]} ")
if (i+1)%10 == 0 {
println('')
}
}
println("\n")
}
}
- Output:
Same as Wren example
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");
}
}
- Output:
Same as Wren example.
Wren
import "./iterate" for Stepped
import "./seq" for Lst
var pow = 1
for (p in 0..4) {
var low = pow.sqrt.ceil
if (low % 2 == 0) low = low + 1
pow = pow * 10
var high = pow.sqrt.floor
var oddSq = Stepped.new(low..high, 2).map { |i| i * i }.toList
System.print("%(oddSq.count) odd squares from %(pow/10) to %(pow):")
for (chunk in Lst.chunks(oddSq, 10)) System.print(chunk.join(" "))
System.print()
}
- Output:
2 odd squares from 1 to 10: 1 9 3 odd squares from 10 to 100: 25 49 81 11 odd squares from 100 to 1000: 121 169 225 289 361 441 529 625 729 841 961 34 odd squares from 1000 to 10000: 1089 1225 1369 1521 1681 1849 2025 2209 2401 2601 2809 3025 3249 3481 3721 3969 4225 4489 4761 5041 5329 5625 5929 6241 6561 6889 7225 7569 7921 8281 8649 9025 9409 9801 108 odd squares from 10000 to 100000: 10201 10609 11025 11449 11881 12321 12769 13225 13689 14161 14641 15129 15625 16129 16641 17161 17689 18225 18769 19321 19881 20449 21025 21609 22201 22801 23409 24025 24649 25281 25921 26569 27225 27889 28561 29241 29929 30625 31329 32041 32761 33489 34225 34969 35721 36481 37249 38025 38809 39601 40401 41209 42025 42849 43681 44521 45369 46225 47089 47961 48841 49729 50625 51529 52441 53361 54289 55225 56169 57121 58081 59049 60025 61009 62001 63001 64009 65025 66049 67081 68121 69169 70225 71289 72361 73441 74529 75625 76729 77841 78961 80089 81225 82369 83521 84681 85849 87025 88209 89401 90601 91809 93025 94249 95481 96721 97969 99225
XPL0
int N2, N;
[for N2:= 101 to 999 do
[N:= sqrt(N2);
if N*N=N2 & (N&1)=1 then
[IntOut(0, N2); ChOut(0, ^ )];
];
]
- Output:
121 169 225 289 361 441 529 625 729 841 961
- Draft Programming Tasks
- 11l
- 8080 Assembly
- ALGOL 68
- ALGOL W
- Arturo
- AWK
- BASIC
- BCPL
- BQN
- C
- CLU
- COBOL
- Cowgol
- Delphi
- SysUtils,StdCtrls
- Draco
- Euler
- F Sharp
- Factor
- Fe
- Fermat
- FOCAL
- FreeBASIC
- Go
- Haskell
- J
- Jq
- Julia
- Lua
- MACRO-11
- Mathematica
- Wolfram Language
- Maxima
- Miranda
- Modula-2
- Modula-3
- Nim
- Objeck
- OCaml
- Perl
- Ntheory
- Phix
- PILOT
- PL/I
- PL/M
- Polyglot:PL/I and PL/M
- Python
- Quackery
- Raku
- Red
- Ring
- RPL
- Ruby
- Sidef
- V (Vlang)
- Vala
- Wren
- Wren-iterate
- Wren-seq
- XPL0