Odd and square numbers: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
m (→{{header|J}}) |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(24 intermediate revisions by 14 users not shown) | |||
Line 8:
{{trans|Python}}
<
L(i) (1 .< Int(ceil(sqrt(limit)))).step(2)
V num = i * i
I num < limit & num > 99
print(num, end' ‘ ’)</
{{out}}
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}}==
<
# if 2m + 1 and 2m - 1 are consecutive odd numbers, the difference between their squares is 8m #
INT to next := 8;
Line 32 ⟶ 92:
to next +:= 8
OD
END</
{{out}}
<pre>
Line 40 ⟶ 100:
=={{header|ALGOL W}}==
{{Trans|PL/M}} which is based on the Algol 68 sample.
<
integer oddSquare, nextGap;
oddSquare := 1;
Line 53 ⟶ 113:
nextGap := nextGap + 8
end while_oddSquare_lt_1000
end.</
{{out}}
<pre>
121 169 225 289 361 441 529 625 729 841 961
</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}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f ODD_AND_SQUARE_NUMBERS.AWK
BEGIN {
Line 74 ⟶ 144:
exit(0)
}
</syntaxhighlight>
{{out}}
<pre>
Line 83 ⟶ 153:
=={{header|BASIC}}==
<
20 N=10
30 S=N*N
Line 89 ⟶ 159:
50 IF S AND 1 THEN PRINT S
60 N=N+1
70 GOTO 30</
{{out}}
<pre> 121
Line 104 ⟶ 174:
=={{header|BCPL}}==
<
let start() be
Line 113 ⟶ 183:
n := n + 1
$) repeat
$)</
{{out}}
<pre>121
Line 129 ⟶ 199:
=={{header|BQN}}==
<syntaxhighlight lang
Generate odd numbers from 11 to 31 and square them.
Line 135 ⟶ 205:
An alternate version uses more code, but doesn't require any arithmetic to derive:
<
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}}==
<
po: stream := stream$primary_output()
n: int := 10
Line 149 ⟶ 250:
n := n+1
end
end start_up</
{{out}}
<pre>121
Line 164 ⟶ 265:
=={{header|COBOL}}==
<
PROGRAM-ID. ODD-AND-SQUARE.
Line 186 ⟶ 287:
CHECK.
MULTIPLY N BY N GIVING SQR.
IF ODD, MOVE SQR TO FMT, DISPLAY FMT.</
{{out}}
<pre>121
Line 201 ⟶ 302:
=={{header|Cowgol}}==
<
var n: uint16 := 10;
Line 212 ⟶ 313:
end if;
n := n+1;
end loop;</
{{out}}
<pre>121
Line 225 ⟶ 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}}==
<
word i, sq;
i := 11;
Line 234 ⟶ 375:
i := i + 2
od
corp</
{{out}}
<pre>121
Line 247 ⟶ 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 <- 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#}}==
<
// 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")
</syntaxhighlight>
{{out}}
<pre>
Line 270 ⟶ 443:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-06-02}}
<
11 1000 sqrt 2 <range> [ bl ] [ sq pprint ] interleave nl</
{{out}}
<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>
=={{header|Fermat}}==
<
i:=1;
n:=1;
Line 286 ⟶ 486:
i:+;
n:=Oddsq(i);
od;</
=={{header|FOCAL}}==
<
01.20 S S=N*N
01.30 I (1000-S)1.8
Line 296 ⟶ 496:
01.60 S N=N+1
01.70 G 1.2
01.80 Q</
{{out}}
<pre>= 121
Line 312 ⟶ 512:
=={{header|FreeBASIC}}==
Squares without squaring.
<
while n<1000
if n>100 then print n
n+=8*i
i+=1
wend</
=={{header|Go}}==
{{trans|Wren}}
<
import (
Line 350 ⟶ 550:
fmt.Println("\n")
}
}</
{{out}}
Line 358 ⟶ 558:
=={{header|Haskell}}==
<
main = print $ takeWhile (<1000) $ filter odd $ map (^2) $ [10..]</
{{out}}
<pre>[121,169,225,289,361,441,529,625,729,841,961]</pre>
Line 367 ⟶ 567:
Example implementation:
<
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:
<
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:
<
121 169 225 289 361 441 529 625 729 841 961</
=={{header|jq}}==
Line 384 ⟶ 584:
'''Works with gojq, the Go implementation of jq'''
====Basic Task====
<
def oddSquares($upper):
label $out
Line 392 ⟶ 592:
oddSquares(1000) | select(. > 100)
</syntaxhighlight>
{{out}}
As for [[#Julia]].
Line 398 ⟶ 598:
====Extended Example====
{{trans|Wren}}
<
# output: a stream of arrays of size size except possibly for the last array
def group(size):
Line 410 ⟶ 610:
[range(.low; 1 + (.pow|sqrt|floor); 2) | . * . ] as $oddSq
| "\($oddSq|length) odd squares from \(.pow/10) to \(.pow):",
( $oddSq | group(10) | join(" ")), "" )</
{{out}}
As for [[#Wren]].
=={{header|Julia}}==
<syntaxhighlight lang="julia">oddsquares(lim) = [i^2 for i ∈ Int.(range((√).(lim)...)) if isodd(i)]
oddsquares((100, 999))
</syntaxhighlight>
{{Out}}
<pre>
11-element Vector{Int64}:
121
169
225
289
361
441
529
625
729
841
961
</pre>
=={{header|Lua}}==
<syntaxhighlight lang="lua">
for i = 1, math.sqrt( 1000 ),
local i2 = i
if i2 >
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
Line 434 ⟶ 692:
729
841
961</pre>
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<
{{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>
=={{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}}==
<
FROM InOut IMPORT WriteCard, WriteLn;
VAR n, square: CARDINAL;
Line 458 ⟶ 742:
n := n + 1
END
END OddSquare.</
{{out}}
<pre>121
Line 474 ⟶ 758:
=={{header|Modula-3}}==
{{trans|Modula-2}}
<
IMPORT IO;
Line 491 ⟶ 775:
N := N + 1
END
END OddSquare.</
<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 507 ⟶ 812:
=={{header|Objeck}}==
<
function : Main(args : String[]) ~ Nil {
i:=n:=1;
Line 516 ⟶ 821:
""->PrintLine();
}
}</
{{output}}
Line 522 ⟶ 827:
121 169 225 289 361 441 529 625 729 841 961
</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}}==
{{libheader|ntheory}}
<
use strict;
Line 531 ⟶ 846:
use ntheory qw( is_square );
print join( ' ', grep $_ & 1 && is_square($_), 100 .. 999 ), "\n";</
{{out}}
<pre>
Line 538 ⟶ 853:
=={{header|Phix}}==
<!--<
<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>
<!--</
{{out}}
<pre>
Line 548 ⟶ 863:
=={{header|PILOT}}==
<
*loop
C :n=#n+1
Line 554 ⟶ 869:
C :sr=(#sq/2)*2
T (sq<>sr):#sq
J (sq<1000):*loop</
{{out}}
<pre>121
Line 575 ⟶ 890:
Based on the Algol 68 sample.
{{works with|8080 PL/M Compiler}} ... under CP/M (or an emulator)
<
/* CP/M BDOS SYSTEM CALL */
Line 610 ⟶ 925:
END;
EOF</
{{out}}
<pre>
Line 623 ⟶ 938:
<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.
<
odd_squares_100H: procedure options (main);
Line 659 ⟶ 974:
END;
EOF: end odd_squares_100H;</
{{out}}
<pre>
Line 666 ⟶ 981:
=={{header|Python}}==
<
import math
szamok = []
limit = 1000
for i in range(1,
num = i*i
if (
szamok.append(num)
print(szamok)
</syntaxhighlight>
{{out}}
<pre>
[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}}==
<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}}==
Vote for deletion: trivial. But if we gotta keep it, at least make it ''slightly'' interesting.
<syntaxhighlight lang="raku"
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
}</
{{out}}
<pre>2 odd squares from 1 to 10:
Line 721 ⟶ 1,059:
=={{header|Red}}==
<
n: 11
Line 728 ⟶ 1,066:
print n * n
n: n + 2
]</
{{out}}
<pre>
Line 745 ⟶ 1,083:
=={{header|Ring}}==
<
see "working..." + nl
limit = 1000
Line 770 ⟶ 1,108:
txt = txt + "]"
see txt
</syntaxhighlight>
{{out}}
<pre>
Line 776 ⟶ 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>
=={{header|Sidef}}==
<
var hi = 1_000
Line 786 ⟶ 1,149:
take(k**2) if k.is_odd
}
}</
{{out}}
Line 793 ⟶ 1,156:
</pre>
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">import math
fn main() {
Line 819 ⟶ 1,182:
println("\n")
}
}</
{{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>
=={{header|Wren}}==
{{libheader|Wren-
{{libheader|Wren-seq}}
<
import "./seq" for Lst
Line 842 ⟶ 1,230:
for (chunk in Lst.chunks(oddSq, 10)) System.print(chunk.join(" "))
System.print()
}</
{{out}}
Line 877 ⟶ 1,265:
=={{header|XPL0}}==
<
[for N2:= 101 to 999 do
[N:= sqrt(N2);
Line 883 ⟶ 1,271:
[IntOut(0, N2); ChOut(0, ^ )];
];
]</
{{out}}
|