Abundant odd numbers: Difference between revisions

→‎{{header|REXX}}: ooRexx conformance and only one loop instead of three
(→‎{{header|REXX}}: ooRexx conformance and only one loop instead of three)
 
(2 intermediate revisions by 2 users not shown)
Line 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,756 ⟶ 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 6,691 ⟶ 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,707 ⟶ 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