Abundant odd numbers: Difference between revisions
→{{header|REXX}}: ooRexx conformance and only one loop instead of three
(Added Processing code) |
Walterpachl (talk | contribs) (→{{header|REXX}}: ooRexx conformance and only one loop instead of three) |
||
(42 intermediate revisions by 24 users not shown) | |||
Line 25:
:* American Journal of Mathematics, Vol. 35, No. 4 (Oct., 1913), pp. 413-422 - Finiteness of the Odd Perfect and Primitive Abundant Numbers with n Distinct Prime Factors (LE Dickson)
<br><br>
=={{header|11l}}==
{{trans|Python}}
<syntaxhighlight lang="11l">V oddNumber = 1
V aCount = 0
V dSum = 0
F divisorSum(n)
V sum = 1
V i = Int(sqrt(n) + 1)
L(d) 2 .< i
I n % d == 0
sum += d
V otherD = n I/ d
I otherD != d
sum += otherD
R sum
print(‘The first 25 abundant odd numbers:’)
L aCount < 25
dSum = divisorSum(oddNumber)
I dSum > oddNumber
aCount++
print(‘#5 proper divisor sum: #.’.format(oddNumber, dSum))
oddNumber += 2
L aCount < 1000
dSum = divisorSum(oddNumber)
I dSum > oddNumber
aCount++
oddNumber += 2
print("\n1000th abundant odd number:")
print(‘ ’(oddNumber - 2)‘ proper divisor sum: ’dSum)
oddNumber = 1000000001
V found = 0B
L !found
dSum = divisorSum(oddNumber)
I dSum > oddNumber
found = 1B
print("\nFirst abundant odd number > 1 000 000 000:")
print(‘ ’oddNumber‘ proper divisor sum: ’dSum)
oddNumber += 2</syntaxhighlight>
{{out}}
<pre>
The first 25 abundant odd numbers:
945 proper divisor sum: 975
1575 proper divisor sum: 1649
2205 proper divisor sum: 2241
2835 proper divisor sum: 2973
3465 proper divisor sum: 4023
4095 proper divisor sum: 4641
4725 proper divisor sum: 5195
5355 proper divisor sum: 5877
5775 proper divisor sum: 6129
5985 proper divisor sum: 6495
6435 proper divisor sum: 6669
6615 proper divisor sum: 7065
6825 proper divisor sum: 7063
7245 proper divisor sum: 7731
7425 proper divisor sum: 7455
7875 proper divisor sum: 8349
8085 proper divisor sum: 8331
8415 proper divisor sum: 8433
8505 proper divisor sum: 8967
8925 proper divisor sum: 8931
9135 proper divisor sum: 9585
9555 proper divisor sum: 9597
9765 proper divisor sum: 10203
10395 proper divisor sum: 12645
11025 proper divisor sum: 11946
1000th abundant odd number:
492975 proper divisor sum: 519361
First abundant odd number > 1 000 000 000:
1000000575 proper divisor sum: 1083561009
</pre>
=={{header|360 Assembly}}==
<
ABUNODDS CSECT
USING ABUNODDS,R13 base register
Line 111 ⟶ 192:
XDEC DS CL12 temp for edit
REGEQU equate registers
END ABUNODDS</
{{out}}
<pre>
Line 142 ⟶ 223:
0 - number= 1000000575 sigma= 1083561009
</pre>
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits <br> or android 64 bits with application Termux }}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B or android 64 bits */
/* program abundant64.s */
Line 692 ⟶ 774:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
{{output}}
<pre>
Line 733 ⟶ 815:
[[http://rosettacode.org/wiki/Proper_divisors#Ada]].
<
procedure Odd_Abundant is
Line 789 ⟶ 871:
end loop;
Print_Abundant_Line(1, Current, False);
end Odd_Abundant;</
{{out}}
Line 825 ⟶ 907:
=={{header|ALGOL 68}}==
<
# find some abundant odd numbers - numbers where the sum of the proper #
# divisors is bigger than the number #
Line 900 ⟶ 982:
OD
END
END</
{{out}}
<pre>
Line 938 ⟶ 1,020:
{{Trans|ALGOL 68}}
Using the divisor_sum procedure from the [[Sum_of_divisors#ALGOL_W]] task.
<
% find some abundant odd numbers - numbers where the sum of the proper %
% divisors is bigger than the number %
Line 974 ⟶ 1,056:
% returns the sum of the proper divisors of v %
integer procedure divisorSum( integer value v ) ;
if v < 2 then
% find numbers required by the task %
begin
Line 1,012 ⟶ 1,094:
end while_not_foundOddAn ;
end
end.</
{{out}}
<pre>
Line 1,049 ⟶ 1,131:
=={{header|AppleScript}}==
<
if (n < 2) then return 0
set sum to 1
Line 1,101 ⟶ 1,183:
set output to output as text
set AppleScript's text item delimiters to astid
return output</
{{output}}
<
945 (proper divisor sum: 975)
1575 (proper divisor sum: 1649)
Line 1,134 ⟶ 1,216:
492975 (proper divisor sum: 519361)
The first > 1,000,000,000:
1000000575 (proper divisor sum: 1083561009)"</
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
/* program abundant.s */
Line 1,655 ⟶ 1,737:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
<pre>
Program start
Line 1,693 ⟶ 1,775:
=={{header|Arturo}}==
<
print "the first 25 abundant odd numbers:"
Line 1,721 ⟶ 1,803:
]
'i + 2
]</
{{out}}
Line 1,755 ⟶ 1,837:
=={{header|AutoHotkey}}==
<
sum := 0, str := ""
for n, bool in proper_divisors(num)
Line 1,774 ⟶ 1,856:
Array[i] := true
return Array
}</
Examples:<
while (count<1000)
{
Line 1,799 ⟶ 1,881:
}
MsgBox % output
return</
{{out}}
<pre>First 25 abundant odd numbers:
Line 1,840 ⟶ 1,922:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f ABUNDANT_ODD_NUMBERS.AWK
# converted from C
Line 1,875 ⟶ 1,957:
return(sum)
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,908 ⟶ 1,990:
</pre>
=={{header|
==={{header|BASIC256}}===
{{trans|Visual Basic .NET}}
<syntaxhighlight lang="basic256">
numimpar = 1
contar = 0
Line 1,931 ⟶ 2,014:
# Encontrar los números requeridos por la tarea:
# primeros 25 números abundantes impares
Print "Los primeros 25 números impares abundantes:"
Line 1,946 ⟶ 2,028:
While contar < 1000
sumaDiv = SumaDivisores(numimpar)
If sumaDiv > numimpar Then contar += 1
numimpar += 2
End While
Print Chr(10) & "1000º número impar abundante:"
Print "
# primer número impar abundante > mil millones (millardo)
Line 1,966 ⟶ 2,047:
End While
End
</syntaxhighlight>
=={{header|C}}==
<
#include <math.h>
Line 1,991 ⟶ 2,072:
return 0;
}</
{{out}}
<pre>1: 945
Line 2,022 ⟶ 2,103:
The first abundant odd number above one billion is: 1000000575</pre>
=={{header|C sharp|C#}}==
<
using System.Collections.Generic;
using System.Linq;
Line 2,049 ⟶ 2,130:
static string Format(this (int n, int sum) pair) => $"{pair.n:N0} with sum {pair.sum:N0}";
}</
{{out}}
<pre>
Line 2,085 ⟶ 2,166:
=={{header|C++}}==
{{trans|Go}}
<
#include <iostream>
#include <numeric>
Line 2,166 ⟶ 2,247:
return 0;
}</
{{out}}
<pre>The first 25 abundant odd numbers are:
Line 2,200 ⟶ 2,281:
The first abundant odd number above one billion is:
1000000575 < 1 + 3 + 5 + 7 + 9 + 15 + 21 + 25 + 35 + 45 + 49 + 63 + 75 + 105 + 147 + 175 + 225 + 245 + 315 + 441 + 525 + 735 + 1225 + 1575 + 2205 + 3675 + 11025 + 90703 + 272109 + 453515 + 634921 + 816327 + 1360545 + 1904763 + 2267575 + 3174605 + 4081635 + 4444447 + 5714289 + 6802725 + 9523815 + 13333341 + 15873025 + 20408175 + 22222235 + 28571445 + 40000023 + 47619075 + 66666705 + 111111175 + 142857225 + 200000115 + 333333525 = 1083561009</pre>
=={{header|CLU}}==
<syntaxhighlight lang="clu">% Integer square root
isqrt = proc (s: int) returns (int)
x0: int := s / 2
if x0 = 0 then
return(s)
else
x1: int := (x0 + s/x0) / 2
while x1 < x0 do
x0 := x1
x1 := (x0 + s/x0) / 2
end
return(x0)
end
end isqrt
% Calculate aliquot sum (for odd numbers only)
aliquot = proc (n: int) returns (int)
sum: int := 1
for i: int in int$from_to_by(3, isqrt(n)+1, 2) do
if n//i = 0 then
j: int := n / i
sum := sum + i
if i ~= j then
sum := sum + j
end
end
end
return(sum)
end aliquot
% Generate abundant odd numbers
abundant_odd = iter (n: int) yields (int)
while true do
if n < aliquot(n) then yield(n) end
n := n + 2
end
end abundant_odd
start_up = proc ()
po: stream := stream$primary_output()
count: int := 0
for n: int in abundant_odd(1) do
count := count + 1
if count <= 25 cor count = 1000 then
stream$putl(po, int$unparse(count)
|| ":\t"
|| int$unparse(n)
|| "\taliquot: "
|| int$unparse(aliquot(n)))
if count = 1000 then break end
end
end
for n: int in abundant_odd(1000000001) do
stream$putl(po, "First above 1 billion: "
|| int$unparse(n)
|| " aliquot: "
|| int$unparse(aliquot(n)))
break
end
end start_up</syntaxhighlight>
{{out}}
<pre>1: 945 aliquot: 975
2: 1575 aliquot: 1649
3: 2205 aliquot: 2241
4: 2835 aliquot: 2973
5: 3465 aliquot: 4023
6: 4095 aliquot: 4641
7: 4725 aliquot: 5195
8: 5355 aliquot: 5877
9: 5775 aliquot: 6129
10: 5985 aliquot: 6495
11: 6435 aliquot: 6669
12: 6615 aliquot: 7065
13: 6825 aliquot: 7063
14: 7245 aliquot: 7731
15: 7425 aliquot: 7455
16: 7875 aliquot: 8349
17: 8085 aliquot: 8331
18: 8415 aliquot: 8433
19: 8505 aliquot: 8967
20: 8925 aliquot: 8931
21: 9135 aliquot: 9585
22: 9555 aliquot: 9597
23: 9765 aliquot: 10203
24: 10395 aliquot: 12645
25: 11025 aliquot: 11946
1000: 492975 aliquot: 519361
First above 1 billion: 1000000575 aliquot: 1083561009</pre>
=={{header|Common Lisp}}==
Line 2,209 ⟶ 2,382:
Using the ''iterate'' library instead of the standard ''loop'' or ''do''.
<
(eval-when (:compile-toplevel :load-toplevel)
(ql:quickload '("cl-annot" "iterate" "alexandria")))
Line 2,274 ⟶ 2,447:
@type fixnum n sum-of-divisors
(until (< n sum-of-divisors))
(finally (format t "~D ~D~%~%" n sum-of-divisors)))))</
{{out}}
Line 2,318 ⟶ 2,491:
54,820,848 bytes consed
</pre>
=={{header|D}}==
{{trans|C++}}
<
int[] divisors(int n) {
Line 2,378 ⟶ 2,552:
writeln("\nThe first abundant odd number above one billion is:");
abundantOdd(cast(int)(1e9 + 1), 0, 1, true);
}</
{{out}}
<pre>The first 25 abundant odd numbers are:
Line 2,415 ⟶ 2,589:
=={{header|Delphi}}==
{{trans|C}}
<
{$APPTYPE CONSOLE}
Line 2,462 ⟶ 2,636:
end.
</syntaxhighlight>
{{out}}
<pre>1: 945
Line 2,491 ⟶ 2,665:
The one thousandth abundant odd number is: 492975
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#}}==
<syntaxhighlight lang="fsharp">
// Abundant odd numbers. Nigel Galloway: August 1st., 2021
let fN g=Seq.initInfinite(int64>>(+)1L)|>Seq.takeWhile(fun n->n*n<=g)|>Seq.filter(fun n->g%n=0L)|>Seq.sumBy(fun n->let i=g/n in n+(if i=n then 0L else i))
let aon n=Seq.initInfinite(int64>>(*)2L>>(+)n)|>Seq.map(fun g->(g,fN g))|>Seq.filter(fun(n,g)->2L*n<g)
aon 1L|>Seq.take 25|>Seq.iter(fun(n,g)->printfn "The sum of the divisors of %d is %d" n g)
let n,g=aon 1L|>Seq.item 999 in printfn "\nThe 1000th abundant odd number is %d. The sum of it's divisors is %d" n g
let n,g=aon 1000000001L|>Seq.head in printfn "\nThe first abundant odd number greater than 1000000000 is %d. The sum of it's divisors is %d" n g
</syntaxhighlight>
{{out}}
<pre>
The sum of the divisors of 945 is 1920
The sum of the divisors of 1575 is 3224
The sum of the divisors of 2205 is 4446
The sum of the divisors of 2835 is 5808
The sum of the divisors of 3465 is 7488
The sum of the divisors of 4095 is 8736
The sum of the divisors of 4725 is 9920
The sum of the divisors of 5355 is 11232
The sum of the divisors of 5775 is 11904
The sum of the divisors of 5985 is 12480
The sum of the divisors of 6435 is 13104
The sum of the divisors of 6615 is 13680
The sum of the divisors of 6825 is 13888
The sum of the divisors of 7245 is 14976
The sum of the divisors of 7425 is 14880
The sum of the divisors of 7875 is 16224
The sum of the divisors of 8085 is 16416
The sum of the divisors of 8415 is 16848
The sum of the divisors of 8505 is 17472
The sum of the divisors of 8925 is 17856
The sum of the divisors of 9135 is 18720
The sum of the divisors of 9555 is 19152
The sum of the divisors of 9765 is 19968
The sum of the divisors of 10395 is 23040
The sum of the divisors of 11025 is 22971
The 1000th abundant odd number is 492975. The sum of it's divisors is 1012336
The first abundant odd number greater than 1000000000 is 1000000575. The sum of it's divisors is 2083561584
</pre>
=={{header|Factor}}==
<
math.primes.factors sequences tools.memory.private ;
IN: rosetta-code.abundant-odd-numbers
Line 2,505 ⟶ 2,762:
: first25 ( -- seq ) 25 1 abundant-odds-from ltake list>array ;
: 1,000th ( -- n ) 999 1 abundant-odds-from
: first>10^9 ( -- n ) 1,000,000,001 abundant-odds-from car ;
Line 2,518 ⟶ 2,775:
[ print show nl ] 2tri@ ;
MAIN: abundant-odd-numbers-demo</
{{out}}
<pre>
Line 2,553 ⟶ 2,810:
First abundant odd number > one billion:
1,000,000,575 σ = 2,083,561,584
</pre>
=={{header|Fortran}}==
A basic direct solution. A more robust alternative would be to find
the prime factors and then use a formulaic approach.
<syntaxhighlight lang="fortran">
program main
use,intrinsic :: iso_fortran_env, only : int8, int16, int32, int64
implicit none
integer,parameter :: dp=kind(0.0d0)
character(len=*),parameter :: g='(*(g0,1x))'
integer :: j, icount
integer,allocatable :: list(:)
real(kind=dp) :: tally
write(*,*)'N sum'
icount=0 ! number of abundant odd numbers found
do j=1,huge(0)-1,2 ! loop through odd numbers for candidates
list=divisors(j) ! git list of divisors for current value
tally= sum([real(list,kind=dp)]) ! sum divisors
if(tally>2*j .and. iand(j,1) /= 0) then ! count an abundant odd number
icount=icount+1
select case(icount) ! if one of the values targeted print it
case(1:25,1000);write(*,g)icount,':',j!, list
end select
endif
if(icount.gt.1000)exit ! quit after last targeted value is found
enddo
do j=1000000001,huge(0),2
list=divisors(j)
tally= sum([real(list,kind=dp)])
if(tally>2*j .and. iand(j,1) /= 0) then
write(*,g)'First abundant odd number greater than one billion:',j
exit
endif
enddo
contains
function divisors(num) result (numbers)
!> brute force divisors
integer,intent(in) :: num
integer :: i
integer,allocatable :: numbers(:)
numbers=[integer :: ]
do i=1 , int(sqrt(real(num)))
if (mod(num , i) .eq. 0) numbers=[numbers, i,num/i]
enddo
end function divisors
end program main
</syntaxhighlight>
{{out}}
<pre>
N sum
1 : 945
2 : 1575
3 : 2205
4 : 2835
5 : 3465
6 : 4095
7 : 4725
8 : 5355
9 : 5775
10 : 5985
11 : 6435
12 : 6615
13 : 6825
14 : 7245
15 : 7425
16 : 7875
17 : 8085
18 : 8415
19 : 8505
20 : 8925
21 : 9135
22 : 9555
23 : 9765
24 : 10395
25 : 11025
1000 : 492975
First abundant odd number greater than one billion: 1000000575
</pre>
=={{header|FreeBASIC}}==
{{trans|Visual Basic .NET}}
<
Declare Function SumaDivisores(n As Integer) As Integer
Line 2,615 ⟶ 2,957:
Loop
End
</syntaxhighlight>
{{out}}
<pre>
Line 2,650 ⟶ 2,992:
Primer número impar abundante > 1 000 000 000:
1000000575 suma divisoria adecuada: 1083561009
</pre>
=={{header|Frink}}==
Frink has efficient functions for factoring numbers that use trial division, wheel factoring, and Pollard rho factoring.
<syntaxhighlight lang="frink">isAbundantOdd[n] := sum[allFactors[n, true, false]] > n
n = 3
count = 0
println["The first 25 abundant odd numbers:"]
do
{
if isAbundantOdd[n]
{
println["$n: proper divisor sum " + sum[allFactors[n, 1, false]]]
count = count + 1
}
n = n + 2
} while count < 25
println["\nThe thousandth abundant odd number:"]
n = 1
count = 0
do
{
n = n + 2
if isAbundantOdd[n]
count = count + 1
} until count == 1000
println["$n: proper divisor sum " + sum[allFactors[n, 1, false]]]
println["\nThe first abundant odd number over 1 billion:"]
n = 10^9 + 1
count = 0
do
n = n + 2
until isAbundantOdd[n]
println["$n: proper divisor sum " + sum[allFactors[n, 1, false]]]
</syntaxhighlight>
{{out}}
<pre>
The first 25 abundant odd numbers:
945: proper divisor sum 975
1575: proper divisor sum 1649
2205: proper divisor sum 2241
2835: proper divisor sum 2973
3465: proper divisor sum 4023
4095: proper divisor sum 4641
4725: proper divisor sum 5195
5355: proper divisor sum 5877
5775: proper divisor sum 6129
5985: proper divisor sum 6495
6435: proper divisor sum 6669
6615: proper divisor sum 7065
6825: proper divisor sum 7063
7245: proper divisor sum 7731
7425: proper divisor sum 7455
7875: proper divisor sum 8349
8085: proper divisor sum 8331
8415: proper divisor sum 8433
8505: proper divisor sum 8967
8925: proper divisor sum 8931
9135: proper divisor sum 9585
9555: proper divisor sum 9597
9765: proper divisor sum 10203
10395: proper divisor sum 12645
11025: proper divisor sum 11946
The thousandth abundant odd number:
492975: proper divisor sum 519361
The first abundant odd number over 1 billion:
1000000575: proper divisor sum 1083561009
</pre>
=={{header|FutureBasic}}==
{{trans|C}}
FB's 'cln' keyword is used to enter a line of C or Objective-C code.
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
local fn SumOfProperDivisors( n as NSUInteger ) as NSUinteger
NSUinteger sum = 1
cln for (unsigned i = 3, j; i < sqrt(n)+1; i += 2) if (n % i == 0) sum += i + (i == (j = n / i) ? 0 : j);
end fn = sum
Line 2,673 ⟶ 3,098:
HandleEvents
</syntaxhighlight>
The following is a 'pure' FB code version.
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
local fn SumOfProperDivisors( n as NSUInteger ) as NSUinteger
NSUinteger i, j, sum = 1
for i = 3 to sqr(n) step 2
if ( n mod i == 0 )
sum += i
j = n/i
if ( i != j )
sum += j
end if
end if
next
end fn = sum
NSUinteger n = 1, c
while ( c < 25 )
if ( n < fn SumOfProperDivisors( n ) )
NSLog( @"%2lu: %lu", c, n )
c++
end if
n += 2
wend
while ( c < 1000 )
if ( n < fn SumOfProperDivisors( n ) ) then c++
n += 2
wend
NSLog( @"\nThe one thousandth abundant odd number is: %lu\n", n )
n = 1000000001
while ( n >= fn SumOfProperDivisors( n ) )
n += 2
wend
NSLog( @"The first abundant odd number above one billion is: %lu\n", n )
HandleEvents
</syntaxhighlight>
{{out}}
<pre>
Line 2,708 ⟶ 3,177:
=={{header|Go}}==
<
import (
Line 2,780 ⟶ 3,249:
fmt.Println("\nThe first abundant odd number above one billion is:")
abundantOdd(1e9+1, 0, 1, true)
}</
{{out}}
Line 2,820 ⟶ 3,289:
=={{header|Groovy}}==
{{trans|Java}}
<
static List<Integer> divisors(int n) {
List<Integer> divs = new ArrayList<>()
Line 2,884 ⟶ 3,353:
abundantOdd((int) (1e9 + 1), 0, 1, true)
}
}</
{{out}}
<pre>The first 25 abundant odd numbers are:
Line 2,920 ⟶ 3,389:
=={{header|Haskell}}==
<
divisorSum :: Integral a => a -> a
Line 2,948 ⟶ 3,417:
printAbundant $ oddAbundants 1 !! 1000
putStrLn "The first odd abundant number above 1000000000 is:"
printAbundant . head . oddAbundants $ 10 ^ 9</
{{out}}
Line 2,983 ⟶ 3,452:
Or, importing Data.Numbers.Primes (and significantly faster):
<
import Data.Numbers.Primes
Line 3,021 ⟶ 3,490:
( [1 + billion, 3 + billion ..]
>>= abundantTuple
)</
{{Out}}
<pre>First 25 abundant odd numbers with their divisor sums:
Line 3,098 ⟶ 3,567:
=={{header|Java}}==
<
import java.util.List;
Line 3,143 ⟶ 3,612:
}
</syntaxhighlight>
{{out}}
<pre>
Line 3,185 ⟶ 3,654:
Composing reusable functions and generators:
{{Trans|Python}}
<
'use strict';
const main = () => {
Line 3,395 ⟶ 3,864:
// MAIN ---
return main();
})();</
{{Out}}
<pre>First 25 abundant odd numbers, with their divisor sums:
Line 3,429 ⟶ 3,898:
First abundant odd number above 10^9, with divisor sum:
(1000000575,1083561009)</pre>
=={{header|jq}}==
<syntaxhighlight lang="jq">
# The factors, unsorted
def factors:
. as $num
| reduce range(1; 1 + sqrt|floor) as $i
([];
if ($num % $i) == 0 then
($num / $i) as $r
| if $i == $r then . + [$i] else . + [$i, $r] end
else .
end) ;
def abundant_odd_numbers:
range(1; infinite; 2)
| (factors | add) as $sum
| select($sum > 2*.)
| [., $sum] ;</syntaxhighlight>
Computing the first abundant number greater than 10^9 is presently impractical using jq, but for the other tasks:
<syntaxhighlight lang="text">( ["n", "sum of divisors"],
limit(25; abundant_odd_numbers)),
[],
(["The 1000th abundant odd number and corresponding sum of divisors:"]
+ nth(999; abundant_odd_numbers))
| @tsv
</syntaxhighlight>
{{out}}
<pre>
n sum of divisors
945 1920
1575 3224
2205 4446
2835 5808
3465 7488
4095 8736
4725 9920
5355 11232
5775 11904
5985 12480
6435 13104
6615 13680
6825 13888
7245 14976
7425 14880
7875 16224
8085 16416
8415 16848
8505 17472
8925 17856
9135 18720
9555 19152
9765 19968
10395 23040
11025 22971
The 1000th abundant odd number and corresponding sum of divisors: 492975 1012336
</pre>
=={{header|Julia}}==
<
function propfact(n)
Line 3,469 ⟶ 3,997:
println("The first abundant odd number greater than one billion:")
oddabundantsfrom(1000000000, 1)
</
<pre>
First 25 abundant odd numbers:
Line 3,504 ⟶ 4,032:
=={{header|Kotlin}}==
{{trans|D}}
<
val divs = mutableListOf(1)
val divs2 = mutableListOf<Int>()
Line 3,561 ⟶ 4,089:
println("\nThe first abundant odd number above one billion is:")
abundantOdd((1e9 + 1).toInt(), 0, 1, true)
}</
{{out}}
<pre>The first 25 abundant odd numbers are:
Line 3,598 ⟶ 4,126:
=={{header|Lobster}}==
{{trans|C}}
<syntaxhighlight lang="lobster">
// Note that the following function is for odd numbers only
// Use "for (unsigned i = 2; i*i <= n; i++)" for even and odd numbers
Line 3,640 ⟶ 4,168:
abundant_odd_numbers()
</syntaxhighlight>
{{out}}
<pre>
Line 3,674 ⟶ 4,202:
=={{header|Lua}}==
<
function sumDivs (x)
local sum, sqr = 1, math.sqrt(x)
Line 3,710 ⟶ 4,238:
for k, v in pairs(oddAbundants("First", 25)) do showResult(k, v) end
showResult("1000", oddAbundants("Nth", 1000))
showResult("Above 1e6", oddAbundants("Above", 1e6))</
{{out}}
<pre>1: the proper divisors of 945 sum to 975
Line 3,742 ⟶ 4,270:
=={{header|MAD}}==
<
INTERNAL FUNCTION(ND)
Line 3,776 ⟶ 4,304:
VECTOR VALUES HUGENO =
0 $25HFIRST ABOVE 1 BILLION IS ,I10,S1,7HDIVSUM ,I10*$
END OF PROGRAM</
{{out}}
Line 3,812 ⟶ 4,340:
=={{header|Maple}}==
<syntaxhighlight lang="maple">
with(NumberTheory):
Line 3,853 ⟶ 4,381:
cat("First abundant odd number > 10^9 is ", number, ", its sum of divisors is ", SumOfDivisors(number), ", and its sum of proper divisors is ",divisorSum(number));
</syntaxhighlight>
{{out}}<pre>
"First 25 abundant odd numbers"
Line 3,911 ⟶ 4,439:
"First abundant odd number > 10^9 is 1000000575, its sum of divisors is 2083561584, and its sum of proper divisors is 1083561009"
</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">ClearAll[AbundantQ]
AbundantQ[n_] := TrueQ[Greater[Total @ Most @ Divisors @ n, n]]
res = {};
i = 1;
While[Length[res] < 25,
If[AbundantQ[i],
AppendTo[res, {i, Total @ Most @ Divisors @ i}];
];
i += 2;
];
res
res = {};
i = 1;
While[Length[res] < 1000,
If[AbundantQ[i],
AppendTo[res, {i, Total @ Most @ Divisors @ i}];
];
i += 2;
];
res[[-1]]
res = {};
i = 1000000001;
While[Length[res] < 1,
If[AbundantQ[i],
AppendTo[res, {i, Total @ Most @ Divisors @ i}];
];
i += 2;
];
res</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}}
{492975, 519361}
{{1000000575,1083561009}}</pre>
=={{header|Maxima}}==
<
while k < 25 do (
n: n+2,
Line 3,922 ⟶ 4,488:
),
return(l)
);</
{{out}}
<pre>[[945,1920],[1575,3224],[2205,4446],[2835,5808],[3465,7488],[4095,8736],[4725,9920],[5355,11232],[5775,11904],[5985,12480],[6435,13104],[6615,13680],[6825,13888],[7245,14976],[7425,14880],[7875,16224],[8085,16416],[8415,16848],[8505,17472],[8925,17856],[9135,18720],[9555,19152],[9765,19968],[10395,23040],[11025,22971]]</pre>
<
while k < 1000 do (
n: n+2,
Line 3,932 ⟶ 4,498:
),
return([n,divsum(n)])
);</
{{out}}
<pre>[492975,1012336]</pre>
<
while n < 10^8 do (
if not mod(n,3)=0 then (
Line 3,945 ⟶ 4,511:
),
return(l)
);</
{{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}}==
<syntaxhighlight lang="nim">
from math import sqrt
import strformat
Line 4,005 ⟶ 4,648:
echo fmt"The sum of its proper divisors is {s}."
break
</syntaxhighlight>
{{out}}
Line 4,045 ⟶ 4,688:
</pre>
=={{header|
<pre>
genit(brk1,brk2,brk3)={tcnt=0;
Line 4,094 ⟶ 4,737:
=={{header|Pascal}}==
{{works with|Free Pascal}} {{works with|Delphi}}
<
program AbundantOddNumbers;
{$IFDEF FPC}
Line 4,274 ⟶ 4,917:
Inc(N, 2);
WriteLn('The first abundant odd number above one billion is: ',OutNum(N));
end.</
{{out}}
<pre>
Line 4,315 ⟶ 4,958:
{{trans|Raku}}
{{libheader|ntheory}}
<
use warnings;
use feature 'say';
Line 4,338 ⟶ 4,981:
say for odd_abundants(1, 25);
say "\nOne thousandth abundant odd number:\n", (odd_abundants(1, 1000))[999];
say "\nFirst abundant odd number above one billion:\n", odd_abundants(999_999_999, 1);</
{{out}}
<pre style="height:20ex">First 25 abundant odd numbers:
Line 4,374 ⟶ 5,017:
=={{header|Phix}}==
<!--<
<span style="color: #008080;">function</span> <span style="color: #000000;">abundantOdd</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">done</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">lim</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">bool</span> <span style="color: #000000;">printAll</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">done</span><span style="color: #0000FF;"><</span><span style="color: #000000;">lim</span> <span style="color: #008080;">do</span>
Line 4,396 ⟶ 5,039:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"The first abundant odd number above one billion is:"</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">abundantOdd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1e9</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">0</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #004600;">false</span><span style="color: #0000FF;">)</span>
<!--</
{{out}}
<pre>
Line 4,432 ⟶ 5,075:
=={{header|PicoLisp}}==
<
(if (assoc Key (val Var))
(con @ (inc (cdr @)))
Line 4,477 ⟶ 5,120:
'****
1000000575
(factor-sum 1000000575) ) )</
{{out}}
<pre>
Line 4,510 ⟶ 5,153:
=={{header|Processing}}==
<
println("First 25 abundant odd numbers: ");
int abundant = 0;
Line 4,552 ⟶ 5,195:
}
return sum;
}</
{{out}}
<pre>First 25 abundant odd numbers:
Line 4,587 ⟶ 5,230:
=={{header|PureBasic}}==
{{trans|C}}
<
Line 4,656 ⟶ 5,299:
Input()
EndIf</
{{out}}
<pre> 1: 945 -> 975 = 1+3+5+7+9+15+21+27+35+45+63+105+135+189+315
Line 4,693 ⟶ 5,336:
===Procedural===
{{trans|Visual Basic .NET}}
<
# Abundant odd numbers - Python
Line 4,738 ⟶ 5,381:
print ("\nFirst abundant odd number > 1 000 000 000:")
print (" ",oddNumber," proper divisor sum: ",dSum)
oddNumber += 2</
{{out}}
<pre>
Line 4,776 ⟶ 5,419:
===Functional===
<
from math import sqrt
Line 4,876 ⟶ 5,519:
if __name__ == '__main__':
main()</
{{Out}}
<pre>First 25 abundant odd numbers with their divisor sums:
Line 4,912 ⟶ 5,555:
=={{header|q}}==
<
sd:sum s@ / sum of proper divisors
abundant:{x<sd x}
Filter:{y where x each y}</
Solution largely follows that for [[#J]], except the crucial definition of <code>s</code>.
The definition here is naïve. It suffices for the first two items in this task, but takes minutes to execute the third item on a 2018 Mac with 64GB memory.
{{out}}
<
1054
Line 4,930 ⟶ 5,573:
q)1 sd\(not abundant@)(2+)/1000000000-1 / first abundant odd number above 1,000,000,000 and its divisors
1000000575 1083561009</
References:
* [https://code.kx.com/q/ref/ Language Reference]
* [https://code.kx.com/q/learn/pb/abundant-odds/ The Q Playbook: Abundant Odds – analysis]
=={{header|Quackery}}==
<code>factors</code> is defined at [[Factors of an integer#Quackery]].
<syntaxhighlight lang="quackery"> [ 0 swap factors witheach + ] is sigmasum ( n --> n )
0 -1 [ 2 +
dup sigmasum
over 2 * over < iff
[ over echo sp
echo cr
dip 1+ ]
else drop
over 25 = until ]
2drop
cr
0 -1
[ 2 + dup sigmasum
over 2 * > if [ dip 1+ ]
over 1000 = until ]
dup echo sp sigmasum echo cr
drop
cr
999999999
[ 2 + dup sigmasum
over 2 * > until ]
dup echo sp sigmasum echo cr</syntaxhighlight>
{{out}}
<pre>945 1920
1575 3224
2205 4446
2835 5808
3465 7488
4095 8736
4725 9920
5355 11232
5775 11904
5985 12480
6435 13104
6615 13680
6825 13888
7245 14976
7425 14880
7875 16224
8085 16416
8415 16848
8505 17472
8925 17856
9135 18720
9555 19152
9765 19968
10395 23040
11025 22971
492975 1012336
1000000575 2083561584
</pre>
=={{header|R}}==
<
find_div_sum <- function(x){
Line 4,976 ⟶ 5,680:
# Get the first after 1e9
cat("First odd abundant after 1e9 is")
get_n_abun(index = 1e9 + 1, total = 1, print_all = F)</
{{Out}}
Line 5,014 ⟶ 5,718:
=={{header|Racket}}==
<
(require math/number-theory
Line 5,027 ⟶ 5,731:
(for/list ([i (in-range 25)] [x (make-generator 0)]) x) ; Task 1
(for/last ([i (in-range 1000)] [x (make-generator 0)]) x) ; Task 2
(for/first ([x (make-generator (add1 (inexact->exact 1e9)))]) x) ; Task 3</
{{out}}
Line 5,064 ⟶ 5,768:
{{works with|Rakudo|2019.03}}
<syntaxhighlight lang="raku"
my @l = x.is-prime ?? 1 !! flat
1, (3 .. x.sqrt.floor).map: -> \d {
Line 5,090 ⟶ 5,794:
put "\nOne thousandth abundant odd number:\n" ~ odd-abundants( :start-at(1) )[999] ~
"\n\nFirst abundant odd number above one billion:\n" ~ odd-abundants( :start-at(1_000_000_000) ).head;</
{{out}}
<pre>First 25 abundant odd numbers:
Line 5,128 ⟶ 5,832:
A wee bit of coding was added to add commas to numbers (because of the larger numbers) as well as alignment of the output.
The '''sigO''' function is a specialized version of the '''sigma''' function optimized just for ''odd'' numbers.
<
parse arg Nlow Nuno Novr . /*obtain optional arguments from the CL*/
if Nlow=='' | Nlow==
if Nuno=='' | Nuno==
if Novr=='' | Novr==
numeric digits max(9,
d=sigO(j)
if d>j then Do
n= n
say rt(th(n)) a 'is:'rt(commas(j),8) rt('sigma=') rt(commas(d),9)
End
end /*j*/
say
d= sigO(j)
if d>j
n= n
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
end /*j*/
say
d= sigO(j)
if d>j
say rt(th(1)) a 'over' commas(Novr) 'is: ' commas(j) rt('sigma=') commas(d)
Leave /*we're finished displaying NOVRth num.*/
End
end /*j*/
exit
/*--------------------------------------------------------------------------------------*/
commas:parse arg _; do c_=length(_)-3 to 1 by -3; _=insert(',',_,c_); end; return _
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
do k=3 by 2
if x//k==0 then
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= when using the default input:}}
<pre>
Line 5,203 ⟶ 5,919:
=={{header|Ring}}==
<
#Project: Anbundant odd numbers
Line 5,270 ⟶ 5,986:
see "" + index + ". " + string(n) + ": divisor sum: " +
see string(nArray[m]) + " = " + string(sum) + nl + nl
</syntaxhighlight>
<pre>
Line 5,332 ⟶ 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}}==
proper_divisors method taken from http://rosettacode.org/wiki/Proper_divisors#Ruby
<
class Integer
Line 5,360 ⟶ 6,111:
puts "\n%d with sum %#d" % generator_odd_abundants.take(1000).last
puts "\n%d with sum %#d" % generator_odd_abundants(1_000_000_000).next
</syntaxhighlight>
=={{header|Rust}}==
{{trans|Go}}
<
let mut divs = vec![1];
let mut divs2 = Vec::new();
Line 5,417 ⟶ 6,168:
println!("The first abundant odd number above one billion is:");
abundant_odd(1e9 as u64 + 1, 0, 1, true);
}</
{{out}}
<pre>The first 25 abundant odd numbers are:
Line 5,453 ⟶ 6,204:
=={{header|Scala}}==
{{trans|D}}
<
object Abundant {
Line 5,512 ⟶ 6,263:
abundantOdd((1e9 + 1).intValue(), 0, 1, printOne = true)
}
}</
{{out}}
<pre>The first 25 abundant odd numbers are:
Line 5,548 ⟶ 6,299:
=={{header|Sidef}}==
<
n.sigma > 2*n
}
Line 5,574 ⟶ 6,325:
with(odd_abundants(1e9).first) {|n|
printf(sep + fstr, '***', n, n.sigma-n)
}</
{{out}}
<pre>
Line 5,611 ⟶ 6,362:
=={{header|Smalltalk}}==
<
[:nr |
|divs|
Line 5,657 ⟶ 6,408:
Transcript cr; showCR:'the 1000th odd abundant number is:'.
"from set of abdundant numbers>= 3, print 1000th to 1000th"
printNAbundant value:3 value:1000 value:1000.</
{{out}}
<pre>first 25 odd abundant numbers:
Line 5,697 ⟶ 6,448:
=={{header|Swift}}==
<
@inlinable
public func factors(sorted: Bool = true) -> [Self] {
Line 5,732 ⟶ 6,483:
.first(where: { $1.0 })!
print("first odd abundant number over 1 billion: \(bigA), sigma: \(bigFactors.reduce(0, +))")</
{{out}}
Line 5,762 ⟶ 6,513:
n: 11025; sigma: 11946
first odd abundant number over 1 billion: 1000000575, sigma: 1083561009</pre>
=={{header|uBasic/4tH}}==
{{trans|C}}
<syntaxhighlight lang="text">c = 0
For n = 1 Step 2 While c < 25
If n < FUNC(_SumProperDivisors(n)) Then
c = c + 1
Print Using "_#"; c; Using " ____#"; n
EndIf
Next
For n = n Step 2 While c < 1000
If n < FUNC(_SumProperDivisors(n)) Then c = c + 1
Next
Print "\nThe one thousandth abundant odd number is: "; n
For n = 1000000001 Step 2
Until n < FUNC(_SumProperDivisors(n))
Next
Print "The first abundant odd number above one billion is: "; n
End
' The following function is for odd numbers ONLY
_SumProperDivisors
Param (1)
Local (3)
b@ = 1
For c@ = 3 To FUNC(_Sqrt(a@)) Step 2
If (a@ % c@) = 0 Then b@ = b@ + c@ + Iif (c@ = Set(d@, a@/c@), 0, d@)
Next
Return (b@)
_Sqrt
Param (1)
Local (3)
Let b@ = 1
Let c@ = 0
Do Until b@ > a@
Let b@ = b@ * 4
Loop
Do While b@ > 1
Let b@ = b@ / 4
Let d@ = a@ - c@ - b@
Let c@ = c@ / 2
If d@ > -1 Then
Let a@ = d@
Let c@ = c@ + b@
Endif
Loop
Return (c@)</syntaxhighlight>
{{out}}
<pre> 1 945
2 1575
3 2205
4 2835
5 3465
6 4095
7 4725
8 5355
9 5775
10 5985
11 6435
12 6615
13 6825
14 7245
15 7425
16 7875
17 8085
18 8415
19 8505
20 8925
21 9135
22 9555
23 9765
24 10395
25 11025
The one thousandth abundant odd number is: 492977
The first abundant odd number above one billion is: 1000000575
0 OK, 0:479
</pre>
=={{header|Visual Basic .NET}}==
{{Trans|ALGOL 68}}
<
' find some abundant odd numbers - numbers where the sum of the proper
' divisors is bigger than the number
Line 5,823 ⟶ 6,665:
Loop
End Sub
End Module</
{{out}}
<pre>
Line 5,856 ⟶ 6,698:
First abundant odd number > 1 000 000 000:
1000000575 proper divisor sum: 1083561009
</pre>
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">fn divisors(n i64) []i64 {
mut divs := [i64(1)]
mut divs2 := []i64{}
for i := 2; i*i <= n; i++ {
if n%i == 0 {
j := n / i
divs << i
if i != j {
divs2 << j
}
}
}
for i := divs2.len - 1; i >= 0; i-- {
divs << divs2[i]
}
return divs
}
fn sum(divs []i64) i64 {
mut tot := i64(0)
for div in divs {
tot += div
}
return tot
}
fn sum_str(divs []i64) string {
mut s := ""
for div in divs {
s += "${u8(div)} + "
}
return s[0..s.len-3]
}
fn abundant_odd(search_from i64, count_from int, count_to int, print_one bool) i64 {
mut count := count_from
mut n := search_from
for ; count < count_to; n += 2 {
divs := divisors(n)
tot := sum(divs)
if tot > n {
count++
if print_one && count < count_to {
continue
}
s := sum_str(divs)
if !print_one {
println("${count:2}. ${n:5} < $s = $tot")
} else {
println("$n < $s = $tot")
}
}
}
return n
}
const max = 25
fn main() {
println("The first $max abundant odd numbers are:")
n := abundant_odd(1, 0, 25, false)
println("\nThe one thousandth abundant odd number is:")
abundant_odd(n, 25, 1000, true)
println("\nThe first abundant odd number above one billion is:")
abundant_odd(1_000_000_001, 0, 1, true)
}</syntaxhighlight>
{{out}}
<pre>
Same as Go entry
</pre>
Line 5,862 ⟶ 6,780:
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<
import "./math" for Int, Nums
var sumStr = Fn.new { |divs| divs.reduce("") { |acc, div| acc + "%(div) + " }[0...-3] }
Line 5,878 ⟶ 6,796:
var s = sumStr.call(divs)
if (!printOne) {
} else {
}
}
Line 5,897 ⟶ 6,815:
System.print("\nThe first abundant odd number above one billion is:")
abundantOdd.call(1e9+1, 0, 1, true)</
{{out}}
Line 5,933 ⟶ 6,851:
The first abundant odd number above one billion is:
1000000575 < 1 + 3 + 5 + 7 + 9 + 15 + 21 + 25 + 35 + 45 + 49 + 63 + 75 + 105 + 147 + 175 + 225 + 245 + 315 + 441 + 525 + 735 + 1225 + 1575 + 2205 + 3675 + 11025 + 90703 + 272109 + 453515 + 634921 + 816327 + 1360545 + 1904763 + 2267575 + 3174605 + 4081635 + 4444447 + 5714289 + 6802725 + 9523815 + 13333341 + 15873025 + 20408175 + 22222235 + 28571445 + 40000023 + 47619075 + 66666705 + 111111175 + 142857225 + 200000115 + 333333525 = 1083561009
</pre>
=={{header|X86 Assembly}}==
Assemble with tasm and tlink /t
<syntaxhighlight lang="asm"> .model tiny
.code
.486
org 100h
;ebp=counter, edi=Num, ebx=Div, esi=Sum
start: xor ebp, ebp ;odd abundant number counter:= 0
mov edi, 3 ;Num:= 3
ab10: mov ebx, 3 ;Div:= 3
mov esi, 1 ;Sum:= 1
ab20: mov eax, edi ;Quot:= Num/Div
cdq ;edx:= 0
div ebx ;eax(q):edx(r):= edx:eax/ebx
cmp ebx, eax ;if Div > Quot then quit loop
jge ab50
test edx, edx ;if remainder = 0 then
jne ab30
add esi, ebx ; Sum:= Sum + Div
cmp ebx, eax ; if Div # Quot then
je ab30
add esi, eax ; Sum:= Sum + Quot
ab30: add ebx, 2 ;Div:= Div+2 (only check odd Nums)
jmp ab20 ;loop
ab50:
cmp esi, edi ;if Sum > Num then
jle ab80
inc ebp ; counter:= counter+1
cmp ebp, 25 ; if counter<=25 or counter>=1000 then
jle ab60
cmp ebp, 1000
jl ab80
ab60: mov eax, edi ; print Num
call numout
mov al, ' ' ; print spaces
int 29h
int 29h
mov eax, esi ; print Sum
call numout
mov al, 0Dh ; carriage return
int 29h
mov al, 0Ah ; line feed
int 29h
cmp ebp, 1000 ; if counter = 1000 then
jne ab65
mov edi, 1000000001-2 ; Num:= 1,000,000,001 - 2
ab65: cmp edi, 1000000000 ; if Num > 1,000,000,000 then exit
jg ab90
ab80: add edi, 2 ;Num:= Num+2 (only check odd Nums)
jmp ab10 ;loop
ab90: ret
;Print signed integer in eax with commas, e.g: 12,345,010
numout: xor ecx, ecx ;digit counter:= 0
no00: cdq ;edx:= 0
mov ebx, 10 ;Num:= Num/10
div ebx ;eax(q):edx(r):= edx:eax/ebx
push edx ;remainder = least significant digit
inc ecx ;count digit
test eax, eax ;if Num # 0 then NumOut(Num)
je no20
call no00
no20: pop eax ;print digit + '0'
add al, '0'
int 29h
dec ecx ;un-count digit
je no30 ;if counter # 0 and
mov al, cl ; if remainder(counter/3) = 0 then
aam 3
jne no30
mov al, ',' ; print ','
int 29h
no30: ret
end start</syntaxhighlight>
{{out}}
<pre>
945 975
1,575 1,649
2,205 2,241
2,835 2,973
3,465 4,023
4,095 4,641
4,725 5,195
5,355 5,877
5,775 6,129
5,985 6,495
6,435 6,669
6,615 7,065
6,825 7,063
7,245 7,731
7,425 7,455
7,875 8,349
8,085 8,331
8,415 8,433
8,505 8,967
8,925 8,931
9,135 9,585
9,555 9,597
9,765 10,203
10,395 12,645
11,025 11,946
492,975 519,361
1,000,000,575 1,083,561,009
</pre>
=={{header|XPL0}}==
<syntaxhighlight lang="xpl0">int Cnt, Num, Div, Sum, Quot;
[Cnt:= 0;
Num:= 3; \find odd abundant numbers
loop [Div:= 1;
Sum:= 0;
loop [Quot:= Num/Div;
if Div > Quot then quit;
if rem(0) = 0 then
[Sum:= Sum + Div;
if Div # Quot then Sum:= Sum + Quot;
];
Div:= Div+2;
];
if Sum > 2*Num then
[Cnt:= Cnt+1;
if Cnt<=25 or Cnt>=1000 then
[IntOut(0, Num); ChOut(0, 9);
IntOut(0, Sum); CrLf(0);
if Cnt = 1000 then Num:= 1_000_000_001 - 2;
if Num > 1_000_000_000 then quit;
];
];
Num:= Num+2;
];
]</syntaxhighlight>
{{out}}
<pre>
945 1920
1575 3224
2205 4446
2835 5808
3465 7488
4095 8736
4725 9920
5355 11232
5775 11904
5985 12480
6435 13104
6615 13680
6825 13888
7245 14976
7425 14880
7875 16224
8085 16416
8415 16848
8505 17472
8925 17856
9135 18720
9555 19152
9765 19968
10395 23040
11025 22971
492975 1012336
1000000575 2083561584
</pre>
=={{header|zkl}}==
<
Walker.zero().tweak(fcn(rn){
n:=rn.value;
Line 5,949 ⟶ 7,030:
}
}.fp(Ref(startAt.isOdd and startAt or startAt+1)))
}</
<
[3.. n.toFloat().sqrt().toInt(), 2].pump(List(1),'wrap(d){
if( (y:=n/d) *d != n) return(Void.Skip);
Line 5,961 ⟶ 7,042:
println("%6,d: %6,d = %s".fmt(n, ds.sum(0), ds.sort().concat(" + ")))
}
}</
<
println("First 25 abundant odd numbers:");
Line 5,971 ⟶ 7,052:
println("\nThe first abundant odd number above one billion is:");
printOAs(oddAbundants(1_000_000_000).next());</
{{out}}
<pre style="height:45ex; font-size:83%">
|