Abundant odd numbers: Difference between revisions

→‎{{header|REXX}}: ooRexx conformance and only one loop instead of three
m (BASIC256 moved to the BASIC section.)
(→‎{{header|REXX}}: ooRexx conformance and only one loop instead of three)
 
(5 intermediate revisions by 4 users not shown)
Line 2,666:
The first abundant odd number above one billion is: 1000000575
</pre>
 
=={{header|EasyLang}}==
{{trans|AWK}}
 
<syntaxhighlight lang=easylang>
fastfunc sumdivs n .
sum = 1
i = 3
while i <= sqrt n
if n mod i = 0
sum += i
j = n / i
if i <> j
sum += j
.
.
i += 2
.
return sum
.
n = 1
numfmt 0 6
while cnt < 1000
sum = sumdivs n
if sum > n
cnt += 1
if cnt <= 25 or cnt = 1000
print cnt & " n: " & n & " sum: " & sum
.
.
n += 2
.
print ""
n = 1000000001
repeat
sum = sumdivs n
until sum > n
n += 2
.
print "1st > 1B: " & n & " sum: " & sum
</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
Line 4,473 ⟶ 4,514:
{{out}}
<pre>[5,25,35,175,385,1925,5005,25025,85085,425425,1616615,8083075,37182145,56581525]</pre>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">
divisorSum = function(n)
ans = 0
i = 1
while i * i <= n
if n % i == 0 then
ans += i
j = floor(n / i)
if j != i then ans += j
end if
i += 1
end while
return ans
end function
 
cnt = 0
n = 1
while cnt < 25
sum = divisorSum(n) - n
if sum > n then
print n + ": " + sum
cnt += 1
end if
n += 2
end while
 
while true
sum = divisorSum(n) - n
if sum > n then
cnt += 1
if cnt == 1000 then break
end if
n += 2
end while
 
print "The 1000th abundant number is " + n + " with a proper divisor sum of " + sum
 
n = 1000000001
while true
sum = divisorSum(n) - n
if sum > n and n > 1000000000 then break
n += 2
end while
 
print "The first abundant number > 1b is " + n + " with a proper divisor sum of " + sum
</syntaxhighlight>
{{out}}
<pre>945: 975
1575: 1649
2205: 2241
2835: 2973
3465: 4023
4095: 4641
4725: 5195
5355: 5877
5775: 6129
5985: 6495
6435: 6669
6615: 7065
6825: 7063
7245: 7731
7425: 7455
7875: 8349
8085: 8331
8415: 8433
8505: 8967
8925: 8931
9135: 9585
9555: 9597
9765: 10203
10395: 12645
11025: 11946
The 1000th abundant number is 492975 with a proper divisor sum of 519361
The first abundant number > 1b is 1000000575 with a proper divisor sum of 1083561009
</pre>
 
=={{header|Nim}}==
Line 5,715 ⟶ 5,833:
 
The &nbsp; '''sigO''' &nbsp; function is a specialized version of the &nbsp; '''sigma''' &nbsp; function optimized just for &nbsp; ''odd'' &nbsp; numbers.
<syntaxhighlight lang="rexx">/*REXX pgm displays abundant odd numbers: 1st 25, one─thousandthone-thousandth, first > 1 billion. */
parse arg Nlow Nuno Novr . /*obtain optional arguments from the CL*/
if Nlow=='' | Nlow=="',"' then Nlow= 25 /*Not specified? Then use the default.*/
if Nuno=='' | Nuno=="',"' then Nuno= 1000 /* "' "' "' "' "' "' */
if Novr=='' | Novr=="',"' then Novr= 1000000000 /* "' "' "' "' "' "' */
numeric digits max(9, length(Novr) ) /*ensure enough decimal digits for // */
@a= 'odd abundant number' /*variable for annotating the output. */
#n= 0 /*count of odd abundant numbers so far.*/
do j=3 by 2 until #n>=Nlow; $= sigO(j) /*get the sigma for an odd integer. */
d=sigO(j)
if $<=j then iterate /*sigma ≤ J ? Then ignore it. */
if d>j then Do #= # + 1 /*sigma = J ? Then ignore it. /*bump the counter for abundant odd #'s*/
n= n say+ rt(th(#))1 @ 'is:'rt(commas(j), 8) rt("sigma=") rt(commas($), 9) /*bump the counter for abundant odd n's*/
say rt(th(n)) a 'is:'rt(commas(j),8) rt('sigma=') rt(commas(d),9)
end /*j*/
End
end /*j*/
say
#n= 0 /*count of odd abundant numbers so far.*/
do j=3 by 2; $= sigO(j) /*get the sigma for an odd integer. */
d= sigO(j)
if $<=j then iterate /*sigma ≤ J ? Then ignore it. */
if d>j #= #then +do 1 /*sigma = J ? Then ignore it. /*bump the counter for abundant odd #'s*/
 
if #<Nuno then iterate /*Odd abundant# count<Nuno? Then skip.*/
n= n say+ rt(th(#))1 @ 'is:'rt(commas(j), 8) rt("sigma=") rt(commas($), 9) /*bump the counter for abundant odd n's*/
if n>=Nuno then do /*Odd abundantn count<Nuno? Then skip.*/
say rt(th(n)) a 'is:'rt(commas(j),8) rt('sigma=') rt(commas(d),9)
leave /*we're finished displaying NUNOth num.*/
end /*j*/End
End
end /*j*/
say
do j=1+Novr%2*2 by 2; $= sigO(j) /*get sigma for an odd integer > Novr. */
d= sigO(j)
if $<=j then iterate /*sigma ≤ J ? Then ignore it. */
if d>j say rt(th(1))then Do @ 'over' commas(Novr) "is: " commas(j) rt(' /*sigma =') commas($)J ? Then ignore it. */
say rt(th(1)) a 'over' commas(Novr) 'is: ' commas(j) rt('sigma=') commas(d)
leave /*we're finished displaying NOVRth num.*/
Leave /*we're finished displaying NOVRth num.*/
end /*j*/
End
exit /*stick a fork in it, we're all done. */
end /*j*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
exit
commas:parse arg _; do c_=length(_)-3 to 1 by -3; _=insert(',', _, c_); end; return _
/*--------------------------------------------------------------------------------------*/
rt: procedure; parse arg #,len; if len=='' then len= 20; return right(#, len)
commas:parse arg _; do c_=length(_)-3 to 1 by -3; _=insert(',',_,c_); end; return _
th: parse arg th; return th||word('th st nd rd',1+(th//10)*(th//100%10\==1)*(th//10<4))
rt: procedure; parse arg n,len; if len=='' then len=20; return right(n,len)
/*──────────────────────────────────────────────────────────────────────────────────────*/
th: parse arg th; return th||word('th st nd rd',1+(th//10)*(th//100%10\==1)*(th//10<4))
sigO: parse arg x; s= 1 /*sigma for odd integers. ___*/
/*--------------------------------------------------------------------------------------*/
do k=3 by 2 while k*k<x /*divide by all odd integers up to √ x */
sigO: parse arg x; if x//k==0 then s= s + k + x%k /*addsigma for odd integers. the two divisors to (sigma) sum. ___*/
s=1
end /*k*/ /* ___*/
do k=3 by 2 ifwhile k*k==<x then return s + k /*Was X a square? divide by all Ifodd so,integers addup to v x */
if x//k==0 then
return s /*return (sigma) sum of the divisors. */</syntaxhighlight>
s= s + k + x%k /*add the two divisors to (sigma) sum. */
end /*k*/ /* ___*/
if k*k==x then
return s + k /*Was X a square? If so,add v x */
return s /*return (sigma) sum of the divisors. */
</syntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 5,918 ⟶ 6,048:
done...
</pre>
 
=={{header|RPL}}==
{{works with|Hewlett-Packard|50g}}
≪ DUP DIVIS ∑LIST SWAP DUP + ≥
≫ '<span style="color:blue">ABUND?</span>' STO
≪ { } 1
'''DO'''
2 +
'''IF''' DUP <span style="color:blue">ABUND?</span> '''THEN'''
DUP "*2 <" + OVER DIVIS ∑LIST + ROT SWAP + SWAP '''END'''
'''UNTIL''' OVER SIZE 25 ≥ '''END''' DROP
≫ '<span style="color:blue">TASK1</span>' STO
≪ 0 1
'''DO'''
2 +
'''IF''' DUP <span style="color:blue">ABUND?</span> '''THEN''' SWAP 1 + SWAP '''END'''
'''UNTIL''' OVER 1000 ≥ '''END''' NIP
DUP "*2 <" + SWAP DIVIS ∑LIST +
≫ '<span style="color:blue">TASK2</span>' STO
≪ 1E9 1 -
'''DO'''
2 +
'''UNTIL''' DUP <span style="color:blue">ABUND?</span> '''END'''
DUP "*2 <" + SWAP DIVIS ∑LIST +
≫ '<span style="color:blue">TASK3</span>' STO
{{out}}
<pre>
3: { "945*2 < 1920" "1575*2 < 3224" "2205*2 < 4446" "2835*2 < 5808" "3465*2 < 7488" "4095*2 < 8736" "4725*2 < 9920" "5355*2 < 11232" "5775*2 < 11904" "5985*2 < 12480" "6435*2 < 13104" "6615*2 < 13680" "6825*2 < 13888" "7245*2 < 14976" "7425*2 < 14880" "7875*2 < 16224" "8085*2 < 16416" "8415*2 < 16848" "8505*2 < 17472" "8925*2 < 17856" "9135*2 < 18720" "9555*2 < 19152" "9765*2 < 19968" "10395*2 < 23040" "11025*2 < 22971" }
2: "492975*2 < 1012336"
1: "1000000575*2 < 2083561584"
</pre>
Abundant odd numbers are far from being abundant. It tooks 16 minutes to run TASK1 on an HP-50g calculator and 28 minutes to run TASK2 on an iOS emulator.
 
=={{header|Ruby}}==
Line 6,615 ⟶ 6,780:
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
import "./math" for Int, Nums
var sumStr = Fn.new { |divs| divs.reduce("") { |acc, div| acc + "%(div) + " }[0...-3] }
Line 6,631 ⟶ 6,796:
var s = sumStr.call(divs)
if (!printOne) {
SystemFmt.print("%(Fmt$2d. $5d < $s = $d(2", count)). %(Fmt.d(5, n)), < %(s), = %(tot)")
} else {
SystemFmt.print("%(n)$d < %($s) = %(tot)$d", n, s, tot)
}
}
2,289

edits