I'm working on modernizing Rosetta Code's infrastructure. Starting with communications. Please accept this time-limited open invite to RC's Slack.. --Michael Mol (talk) 20:59, 30 May 2020 (UTC)

# Odd and square numbers

Odd and square numbers is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Find odd and square numbers (>99) under 1.000

## 11l

Translation of: Python
`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
```

## 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    ODEND`
Output:
``` 121 169 225 289 361 441 529 625 729 841 961
```

## ALGOL W

Translation of: PL/M
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_1000end.`
Output:
```121 169 225 289 361 441 529 625 729 841 961
```

## AWK

` # syntax: GAWK -f ODD_AND_SQUARE_NUMBERS.AWKBEGIN {    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-Z20 N=1030 S=N*N40 IF S>=1000 THEN END50 IF S AND 1 THEN PRINT S60 N=N+170 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.

## 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    endend 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```

## Draco

`proc nonrec main() void:    word i, sq;    i := 11;    while sq := i * i; sq < 1000 do        writeln(sq);        i := i + 2    odcorp`
Output:
```121
169
225
289
361
441
529
625
729
841
961```

## F#

` // Odd and square numbers. Nigel Galloway: November 23rd., 2021Seq.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

Works with: Factor version 0.99 2021-06-02
`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
```

## 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=1001.20 S S=N*N01.30 I (1000-S)1.801.40 I (FITR(S/2)*2-S)1.5,1.601.50 T %3,S,!01.60 S N=N+101.70 G 1.201.80 Q`
Output:
```= 121
= 169
= 225
= 289
= 361
= 441
= 529
= 625
= 729
= 841
= 961```

## FreeBASIC

Squares without squaring.

`dim as integer i=1, n=1while n<1000    if n>100 then print n    n+=8*i    i+=1wend`

## Go

Translation of: Wren
`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
```

`main :: IO ()main = print \$ takeWhile (<1000) \$ filter odd \$ map (^2) \$ [10..]`
Output:
`[121,169,225,289,361,441,529,625,729,841,961]`

## jq

Works with: jq

Works with gojq, the Go implementation of jq

`# Output: a stream up to but less than \$upperdef 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

Translation of: Wren
`# input: an array# output: a stream of arrays of size size except possibly for the last arraydef 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

Translation of: FreeBASIC
`julia> i = n = 11 julia> while n < 1000       n > 100 && println(n)       n += 8i       i += 1       end121169225289361441529625729841961 `

## Mathematica / Wolfram Language

`Cases[Range[100, 1000], _?(IntegerQ[[email protected]#] && OddQ[#] &)]`
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    ENDEND OddSquare.`
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
```

## Perl

Library: ntheory
`#!/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*loopC :n=#n+1C :sq=#n*#nC :sr=(#sq/2)*2T (sq<>sr):#sqJ (sq<1000):*loop`
Output:
```121
169
225
289
361
441
529
625
729
841
961```

## PL/M

Based on the Algol 68 sample.

Works with: 8080 PL/M Compiler
... 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
```

## Polyglot:PL/I and PL/M

Works with: 8080 PL/M Compiler
... 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 mathszamok=[]limit = 1000 for i in range(1,int(math.ceil(math.sqrt(limit))),2):    num = i*i    if (num < 1000 and num > 99):        szamok.append(num) print(szamok) `
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: 11limit: sqrt 1000while [n < limit][    print n * n    n: n + 2]`
Output:
```121
169
225
289
361
441
529
625
729
841
961
```

## Ring

` see "working..." + nllimit = 1000list = [] for i = 1 to ceil(sqrt(limit)) step 2    num = pow(i,2)    if (num < 1000 and num > 99)	add(list,num)    oknext 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...
```

## Sidef

`var lo = 100var 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]
```

## Vlang

Translation of: Go
`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
```

## Wren

Library: Wren-trait
Library: Wren-seq
`import "./trait" for Steppedimport "./seq" for Lst var pow = 1for (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
```