Odd and square numbers: Difference between revisions
(add fermat) |
|||
Line 24: | Line 24: | ||
961 |
961 |
||
</pre> |
</pre> |
||
=={{header|Fermat}}== |
|||
<lang 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;</lang> |
|||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
Squares without squaring. |
Squares without squaring. |
Revision as of 18:02, 23 November 2021
- Task
Find odd and square numbers (>99) under 1.000
F#
<lang fsharp> // 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") </lang>
- Output:
121 169 225 289 361 441 529 625 729 841 961
Fermat
<lang 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;</lang>
FreeBASIC
Squares without squaring. <lang freebasic>dim as integer i=1, n=1 while n<1000
if n>100 then print n n+=8*i i+=1
wend</lang>
Julia
<lang julia>julia> i = n = 1 1
julia> while n < 1000
n > 100 && println(n) n += 8i i += 1 end
121 169 225 289 361 441 529 625 729 841 961 </lang>
Python
<lang python> iimport math szamok=[] 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) </lang>
- Output:
[121, 169, 225, 289, 361, 441, 529, 625, 729, 841, 961]
Ring
<lang 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
</lang>
- Output:
working... [121,169,225,289,361,441,529,625,729,841,961] done...
Raku
Vote for deletion: trivial. But if we gotta keep it, at least make it slightly interesting. <lang perl6>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
}</lang>
- 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
Wren
<lang ecmascript>import "./trait" 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()
}</lang>
- 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