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)
 
(13 intermediate revisions by 9 users not shown)
Line 29:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V oddNumber = 1
V aCount = 0
V dSum = 0
Line 69:
print("\nFirst abundant odd number > 1 000 000 000:")
print(‘ ’oddNumber‘ proper divisor sum: ’dSum)
oddNumber += 2</langsyntaxhighlight>
 
{{out}}
Line 108:
 
=={{header|360 Assembly}}==
<langsyntaxhighlight lang="360asm">* Abundant odd numbers 18/09/2019
ABUNODDS CSECT
USING ABUNODDS,R13 base register
Line 192:
XDEC DS CL12 temp for edit
REGEQU equate registers
END ABUNODDS</langsyntaxhighlight>
{{out}}
<pre>
Line 226:
=={{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">
<lang AArch64 Assembly>
/* ARM assembly AARCH64 Raspberry PI 3B or android 64 bits */
/* program abundant64.s */
Line 774:
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</syntaxhighlight>
</lang>
{{output}}
<pre>
Line 815:
[[http://rosettacode.org/wiki/Proper_divisors#Ada]].
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO, Generic_Divisors;
 
procedure Odd_Abundant is
Line 871:
end loop;
Print_Abundant_Line(1, Current, False);
end Odd_Abundant;</langsyntaxhighlight>
 
{{out}}
Line 907:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">BEGIN
# find some abundant odd numbers - numbers where the sum of the proper #
# divisors is bigger than the number #
Line 982:
OD
END
END</langsyntaxhighlight>
{{out}}
<pre>
Line 1,020:
{{Trans|ALGOL 68}}
Using the divisor_sum procedure from the [[Sum_of_divisors#ALGOL_W]] task.
<langsyntaxhighlight lang="algolw">begin
% find some abundant odd numbers - numbers where the sum of the proper %
% divisors is bigger than the number %
Line 1,094:
end while_not_foundOddAn ;
end
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 1,131:
=={{header|AppleScript}}==
 
<langsyntaxhighlight lang="applescript">on aliquotSum(n)
if (n < 2) then return 0
set sum to 1
Line 1,183:
set output to output as text
set AppleScript's text item delimiters to astid
return output</langsyntaxhighlight>
 
{{output}}
 
<langsyntaxhighlight lang="applescript">"The first 25 abundant odd numbers:
945 (proper divisor sum: 975)
1575 (proper divisor sum: 1649)
Line 1,216:
492975 (proper divisor sum: 519361)
The first > 1,000,000,000:
1000000575 (proper divisor sum: 1083561009)"</langsyntaxhighlight>
 
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
<lang ARM Assembly>
/* ARM assembly Raspberry PI */
/* program abundant.s */
Line 1,737:
/***************************************************/
.include "../affichage.inc"
</syntaxhighlight>
</lang>
<pre>
Program start
Line 1,775:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">abundant?: function [n]-> (2*n) < sum factors n
 
print "the first 25 abundant odd numbers:"
Line 1,803:
]
'i + 2
]</langsyntaxhighlight>
 
{{out}}
Line 1,837:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">Abundant(num){
sum := 0, str := ""
for n, bool in proper_divisors(num)
Line 1,856:
Array[i] := true
return Array
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">output := "First 25 abundant odd numbers:`n"
while (count<1000)
{
Line 1,881:
}
MsgBox % output
return</langsyntaxhighlight>
{{out}}
<pre>First 25 abundant odd numbers:
Line 1,922:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f ABUNDANT_ODD_NUMBERS.AWK
# converted from C
Line 1,957:
return(sum)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,990:
</pre>
 
=={{header|BASIC256BASIC}}==
==={{header|BASIC256}}===
{{trans|Visual Basic .NET}}
<syntaxhighlight lang="basic256">
<lang BASIC256>
numimpar = 1
contar = 0
Line 2,046 ⟶ 2,047:
End While
End
</syntaxhighlight>
</lang>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <math.h>
 
Line 2,071 ⟶ 2,072:
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>1: 945
Line 2,103 ⟶ 2,104:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using static System.Console;
using System.Collections.Generic;
using System.Linq;
Line 2,129 ⟶ 2,130:
 
static string Format(this (int n, int sum) pair) => $"{pair.n:N0} with sum {pair.sum:N0}";
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,165 ⟶ 2,166:
=={{header|C++}}==
{{trans|Go}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <numeric>
Line 2,246 ⟶ 2,247:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>The first 25 abundant odd numbers are:
Line 2,282 ⟶ 2,283:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">% Integer square root
isqrt = proc (s: int) returns (int)
x0: int := s / 2
Line 2,343 ⟶ 2,344:
break
end
end start_up</langsyntaxhighlight>
{{out}}
<pre>1: 945 aliquot: 975
Line 2,381 ⟶ 2,382:
Using the ''iterate'' library instead of the standard ''loop'' or ''do''.
 
<langsyntaxhighlight lang="lisp">;; * Loading the external libraries
(eval-when (:compile-toplevel :load-toplevel)
(ql:quickload '("cl-annot" "iterate" "alexandria")))
Line 2,446 ⟶ 2,447:
@type fixnum n sum-of-divisors
(until (< n sum-of-divisors))
(finally (format t "~D ~D~%~%" n sum-of-divisors)))))</langsyntaxhighlight>
 
{{out}}
Line 2,493 ⟶ 2,494:
=={{header|D}}==
{{trans|C++}}
<langsyntaxhighlight lang="d">import std.stdio;
 
int[] divisors(int n) {
Line 2,551 ⟶ 2,552:
writeln("\nThe first abundant odd number above one billion is:");
abundantOdd(cast(int)(1e9 + 1), 0, 1, true);
}</langsyntaxhighlight>
{{out}}
<pre>The first 25 abundant odd numbers are:
Line 2,588 ⟶ 2,589:
=={{header|Delphi}}==
{{trans|C}}
<langsyntaxhighlight lang="delphi">program AbundantOddNumbers;
 
{$APPTYPE CONSOLE}
Line 2,635 ⟶ 2,636:
 
end.
</syntaxhighlight>
</lang>
{{out}}
<pre>1: 945
Line 2,665 ⟶ 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#}}==
<langsyntaxhighlight 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))
Line 2,674 ⟶ 2,716:
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>
</lang>
{{out}}
<pre>
Line 2,709 ⟶ 2,751:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: arrays formatting io kernel lists lists.lazy math
math.primes.factors sequences tools.memory.private ;
IN: rosetta-code.abundant-odd-numbers
Line 2,733 ⟶ 2,775:
[ print show nl ] 2tri@ ;
 
MAIN: abundant-odd-numbers-demo</langsyntaxhighlight>
{{out}}
<pre>
Line 2,773 ⟶ 2,815:
A basic direct solution. A more robust alternative would be to find
the prime factors and then use a formulaic approach.
<langsyntaxhighlight lang="fortran">
program main
use,intrinsic :: iso_fortran_env, only : int8, int16, int32, int64
Line 2,821 ⟶ 2,863:
 
end program main
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,857 ⟶ 2,899:
=={{header|FreeBASIC}}==
{{trans|Visual Basic .NET}}
<langsyntaxhighlight lang="freebasic">
Declare Function SumaDivisores(n As Integer) As Integer
 
Line 2,915 ⟶ 2,957:
Loop
End
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,954 ⟶ 2,996:
=={{header|Frink}}==
Frink has efficient functions for factoring numbers that use trial division, wheel factoring, and Pollard rho factoring.
<langsyntaxhighlight lang="frink">isAbundantOdd[n] := sum[allFactors[n, true, false]] > n
 
n = 3
Line 2,996 ⟶ 3,038:
println["$n: proper divisor sum " + sum[allFactors[n, 1, false]]]
</langsyntaxhighlight>
{{out}}
<pre>
Line 3,035 ⟶ 3,077:
=={{header|FutureBasic}}==
{{trans|C}}
FB's 'cln' keyword is used to enter a line of C or Objective-C code.
<lang futurebasic>
 
<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 3,054 ⟶ 3,098:
 
HandleEvents
</syntaxhighlight>
</lang>
 
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 3,089 ⟶ 3,177:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 3,161 ⟶ 3,249:
fmt.Println("\nThe first abundant odd number above one billion is:")
abundantOdd(1e9+1, 0, 1, true)
}</langsyntaxhighlight>
 
{{out}}
Line 3,201 ⟶ 3,289:
=={{header|Groovy}}==
{{trans|Java}}
<langsyntaxhighlight lang="groovy">class Abundant {
static List<Integer> divisors(int n) {
List<Integer> divs = new ArrayList<>()
Line 3,265 ⟶ 3,353:
abundantOdd((int) (1e9 + 1), 0, 1, true)
}
}</langsyntaxhighlight>
{{out}}
<pre>The first 25 abundant odd numbers are:
Line 3,301 ⟶ 3,389:
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">import Data.List (nub)
 
divisorSum :: Integral a => a -> a
Line 3,329 ⟶ 3,417:
printAbundant $ oddAbundants 1 !! 1000
putStrLn "The first odd abundant number above 1000000000 is:"
printAbundant . head . oddAbundants $ 10 ^ 9</langsyntaxhighlight>
 
{{out}}
Line 3,364 ⟶ 3,452:
 
Or, importing Data.Numbers.Primes (and significantly faster):
<langsyntaxhighlight lang="haskell">import Data.List (group, sort)
import Data.Numbers.Primes
 
Line 3,402 ⟶ 3,490:
( [1 + billion, 3 + billion ..]
>>= abundantTuple
)</langsyntaxhighlight>
{{Out}}
<pre>First 25 abundant odd numbers with their divisor sums:
Line 3,479 ⟶ 3,567:
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.util.ArrayList;
import java.util.List;
 
Line 3,524 ⟶ 3,612:
}
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,566 ⟶ 3,654:
Composing reusable functions and generators:
{{Trans|Python}}
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
const main = () => {
Line 3,776 ⟶ 3,864:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>First 25 abundant odd numbers, with their divisor sums:
Line 3,812 ⟶ 3,900:
 
=={{header|jq}}==
<syntaxhighlight lang="jq">
<lang jq>
# The factors, unsorted
def factors:
Line 3,828 ⟶ 3,916:
| (factors | add) as $sum
| select($sum > 2*.)
| [., $sum] ;</langsyntaxhighlight>
 
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)),
[],
Line 3,837 ⟶ 3,925:
+ nth(999; abundant_odd_numbers))
| @tsv
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,871 ⟶ 3,959:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Primes
 
function propfact(n)
Line 3,909 ⟶ 3,997:
println("The first abundant odd number greater than one billion:")
oddabundantsfrom(1000000000, 1)
</langsyntaxhighlight>{{out}}
<pre>
First 25 abundant odd numbers:
Line 3,944 ⟶ 4,032:
=={{header|Kotlin}}==
{{trans|D}}
<langsyntaxhighlight lang="scala">fun divisors(n: Int): List<Int> {
val divs = mutableListOf(1)
val divs2 = mutableListOf<Int>()
Line 4,001 ⟶ 4,089:
println("\nThe first abundant odd number above one billion is:")
abundantOdd((1e9 + 1).toInt(), 0, 1, true)
}</langsyntaxhighlight>
{{out}}
<pre>The first 25 abundant odd numbers are:
Line 4,038 ⟶ 4,126:
=={{header|Lobster}}==
{{trans|C}}
<syntaxhighlight lang="lobster">
<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 4,080 ⟶ 4,168:
 
abundant_odd_numbers()
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 4,114 ⟶ 4,202:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">-- Return the sum of the proper divisors of x
function sumDivs (x)
local sum, sqr = 1, math.sqrt(x)
Line 4,150 ⟶ 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))</langsyntaxhighlight>
{{out}}
<pre>1: the proper divisors of 945 sum to 975
Line 4,182 ⟶ 4,270:
=={{header|MAD}}==
 
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
INTERNAL FUNCTION(ND)
Line 4,216 ⟶ 4,304:
VECTOR VALUES HUGENO =
0 $25HFIRST ABOVE 1 BILLION IS ,I10,S1,7HDIVSUM ,I10*$
END OF PROGRAM</langsyntaxhighlight>
 
{{out}}
Line 4,252 ⟶ 4,340:
=={{header|Maple}}==
 
<syntaxhighlight lang="maple">
<lang Maple>
with(NumberTheory):
 
Line 4,293 ⟶ 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>
</lang>
{{out}}<pre>
"First 25 abundant odd numbers"
Line 4,354 ⟶ 4,442:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
 
<langsyntaxhighlight Mathematicalang="mathematica">ClearAll[AbundantQ]
AbundantQ[n_] := TrueQ[Greater[Total @ Most @ Divisors @ n, n]]
res = {};
Line 4,384 ⟶ 4,472:
i += 2;
];
res</langsyntaxhighlight>
{{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}}
Line 4,391 ⟶ 4,479:
 
=={{header|Maxima}}==
<langsyntaxhighlight lang="maxima">block([k: 0, n: 1, l: []],
while k < 25 do (
n: n+2,
Line 4,400 ⟶ 4,488:
),
return(l)
);</langsyntaxhighlight>
{{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>
 
<langsyntaxhighlight lang="maxima">block([k: 0, n: 1],
while k < 1000 do (
n: n+2,
Line 4,410 ⟶ 4,498:
),
return([n,divsum(n)])
);</langsyntaxhighlight>
{{out}}
<pre>[492975,1012336]</pre>
 
<langsyntaxhighlight lang="maxima">block([n: 5, l: [5], r: divsum(n,-1)],
while n < 10^8 do (
if not mod(n,3)=0 then (
Line 4,423 ⟶ 4,511:
),
return(l)
);</langsyntaxhighlight>
{{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">
<lang Nim>
from math import sqrt
import strformat
Line 4,483 ⟶ 4,648:
echo fmt"The sum of its proper divisors is {s}."
break
</syntaxhighlight>
</lang>
 
{{out}}
Line 4,572 ⟶ 4,737:
=={{header|Pascal}}==
{{works with|Free Pascal}} {{works with|Delphi}}
<langsyntaxhighlight lang="pascal">
program AbundantOddNumbers;
{$IFDEF FPC}
Line 4,752 ⟶ 4,917:
Inc(N, 2);
WriteLn('The first abundant odd number above one billion is: ',OutNum(N));
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 4,793 ⟶ 4,958:
{{trans|Raku}}
{{libheader|ntheory}}
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 4,816 ⟶ 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);</langsyntaxhighlight>
{{out}}
<pre style="height:20ex">First 25 abundant odd numbers:
Line 4,852 ⟶ 5,017:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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,874 ⟶ 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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 4,910 ⟶ 5,075:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de accud (Var Key)
(if (assoc Key (val Var))
(con @ (inc (cdr @)))
Line 4,955 ⟶ 5,120:
'****
1000000575
(factor-sum 1000000575) ) )</langsyntaxhighlight>
{{out}}
<pre>
Line 4,988 ⟶ 5,153:
 
=={{header|Processing}}==
<langsyntaxhighlight lang="processing">void setup() {
println("First 25 abundant odd numbers: ");
int abundant = 0;
Line 5,030 ⟶ 5,195:
}
return sum;
}</langsyntaxhighlight>
{{out}}
<pre>First 25 abundant odd numbers:
Line 5,065 ⟶ 5,230:
=={{header|PureBasic}}==
{{trans|C}}
<langsyntaxhighlight PureBasiclang="purebasic">NewList l_sum.i()
 
 
Line 5,134 ⟶ 5,299:
Input()
EndIf</langsyntaxhighlight>
{{out}}
<pre> 1: 945 -&gt; 975 = 1+3+5+7+9+15+21+27+35+45+63+105+135+189+315
Line 5,171 ⟶ 5,336:
===Procedural===
{{trans|Visual Basic .NET}}
<langsyntaxhighlight Pythonlang="python">#!/usr/bin/python
# Abundant odd numbers - Python
 
Line 5,216 ⟶ 5,381:
print ("\nFirst abundant odd number > 1 000 000 000:")
print (" ",oddNumber," proper divisor sum: ",dSum)
oddNumber += 2</langsyntaxhighlight>
{{out}}
<pre>
Line 5,254 ⟶ 5,419:
 
===Functional===
<langsyntaxhighlight lang="python">'''Odd abundant numbers'''
 
from math import sqrt
Line 5,354 ⟶ 5,519:
 
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>First 25 abundant odd numbers with their divisor sums:
Line 5,390 ⟶ 5,555:
 
=={{header|q}}==
<langsyntaxhighlight lang="q">s:{c where 0=x mod c:1+til x div 2} / proper divisors
sd:sum s@ / sum of proper divisors
abundant:{x<sd x}
Filter:{y where x each y}</langsyntaxhighlight>
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}}
<langsyntaxhighlight lang="q">q)count A:Filter[abundant] 1+2*til 260000 / a batch of abundant odd numbers; 1000+ is enough
1054
 
Line 5,408 ⟶ 5,573:
 
q)1 sd\(not abundant@)(2+)/1000000000-1 / first abundant odd number above 1,000,000,000 and its divisors
1000000575 1083561009</langsyntaxhighlight>
References:
* [https://code.kx.com/q/ref/ Language Reference]
Line 5,417 ⟶ 5,582:
<code>factors</code> is defined at [[Factors of an integer#Quackery]].
 
<langsyntaxhighlight Quackerylang="quackery"> [ 0 swap factors witheach + ] is sigmasum ( n --> n )
 
0 -1 [ 2 +
Line 5,439 ⟶ 5,604:
[ 2 + dup sigmasum
over 2 * > until ]
dup echo sp sigmasum echo cr</langsyntaxhighlight>
 
{{out}}
Line 5,475 ⟶ 5,640:
 
=={{header|R}}==
<langsyntaxhighlight Rlang="r"># Abundant Odd Numbers
 
find_div_sum <- function(x){
Line 5,515 ⟶ 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)</langsyntaxhighlight>
 
{{Out}}
Line 5,553 ⟶ 5,718:
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">#lang racket
 
(require math/number-theory
Line 5,566 ⟶ 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</langsyntaxhighlight>
 
{{out}}
Line 5,603 ⟶ 5,768:
{{works with|Rakudo|2019.03}}
 
<syntaxhighlight lang="raku" perl6line>sub odd-abundant (\x) {
my @l = x.is-prime ?? 1 !! flat
1, (3 .. x.sqrt.floor).map: -> \d {
Line 5,629 ⟶ 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;</langsyntaxhighlight>
{{out}}
<pre>First 25 abundant odd numbers:
Line 5,668 ⟶ 5,833:
 
The &nbsp; '''sigO''' &nbsp; function is a specialized version of the &nbsp; '''sigma''' &nbsp; function optimized just for &nbsp; ''odd'' &nbsp; numbers.
<langsyntaxhighlight 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. */</lang>
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,742 ⟶ 5,919:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
#Project: Anbundant odd numbers
 
Line 5,809 ⟶ 5,986:
see "" + index + ". " + string(n) + ": divisor sum: " +
see string(nArray[m]) + " = " + string(sum) + nl + nl
</syntaxhighlight>
</lang>
 
<pre>
Line 5,871 ⟶ 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
<langsyntaxhighlight lang="ruby">require "prime"
class Integer
Line 5,899 ⟶ 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>
</lang>
 
=={{header|Rust}}==
{{trans|Go}}
<langsyntaxhighlight lang="rust">fn divisors(n: u64) -> Vec<u64> {
let mut divs = vec![1];
let mut divs2 = Vec::new();
Line 5,956 ⟶ 6,168:
println!("The first abundant odd number above one billion is:");
abundant_odd(1e9 as u64 + 1, 0, 1, true);
}</langsyntaxhighlight>
{{out}}
<pre>The first 25 abundant odd numbers are:
Line 5,992 ⟶ 6,204:
=={{header|Scala}}==
{{trans|D}}
<langsyntaxhighlight lang="scala">import scala.collection.mutable.ListBuffer
 
object Abundant {
Line 6,051 ⟶ 6,263:
abundantOdd((1e9 + 1).intValue(), 0, 1, printOne = true)
}
}</langsyntaxhighlight>
{{out}}
<pre>The first 25 abundant odd numbers are:
Line 6,087 ⟶ 6,299:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func is_abundant(n) {
n.sigma > 2*n
}
Line 6,113 ⟶ 6,325:
with(odd_abundants(1e9).first) {|n|
printf(sep + fstr, '***', n, n.sigma-n)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 6,150 ⟶ 6,362:
 
=={{header|Smalltalk}}==
<langsyntaxhighlight lang="smalltalk">divisors :=
[:nr |
|divs|
Line 6,196 ⟶ 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.</langsyntaxhighlight>
{{out}}
<pre>first 25 odd abundant numbers:
Line 6,236 ⟶ 6,448:
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">extension BinaryInteger {
@inlinable
public func factors(sorted: Bool = true) -> [Self] {
Line 6,271 ⟶ 6,483:
.first(where: { $1.0 })!
 
print("first odd abundant number over 1 billion: \(bigA), sigma: \(bigFactors.reduce(0, +))")</langsyntaxhighlight>
 
{{out}}
Line 6,304 ⟶ 6,516:
=={{header|uBasic/4tH}}==
{{trans|C}}
<syntaxhighlight lang="text">c = 0
For n = 1 Step 2 While c < 25
Line 6,319 ⟶ 6,531:
Print "\nThe one thousandth abundant odd number is: "; n
For n = 1000000001 Step 2)
Until n < FUNC(_SumProperDivisors(n))
Next
Line 6,359 ⟶ 6,571:
Loop
 
Return (c@)</langsyntaxhighlight>
{{out}}
<pre> 1 945
Line 6,395 ⟶ 6,607:
=={{header|Visual Basic .NET}}==
{{Trans|ALGOL 68}}
<langsyntaxhighlight lang="vbnet">Module AbundantOddNumbers
' find some abundant odd numbers - numbers where the sum of the proper
' divisors is bigger than the number
Line 6,453 ⟶ 6,665:
Loop
End Sub
End Module</langsyntaxhighlight>
{{out}}
<pre>
Line 6,488 ⟶ 6,700:
</pre>
 
=={{header|V (Vlang)}}==
{{trans|go}}
<syntaxhighlight lang="v (vlang)">fn divisors(n i64) []i64 {
mut divs := [i64(1)]
mut divs2 := []i64{}
Line 6,557 ⟶ 6,769:
println("\nThe first abundant odd number above one billion is:")
abundant_odd(1_000_000_001, 0, 1, true)
}</langsyntaxhighlight>
 
{{out}}
Line 6,568 ⟶ 6,780:
{{libheader|Wren-fmt}}
{{libheader|Wren-math}}
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
import "./math" for Int, Nums
var sumStr = Fn.new { |divs| divs.reduce("") { |acc, div| acc + "%(div) + " }[0...-3] }
Line 6,584 ⟶ 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)
}
}
Line 6,603 ⟶ 6,815:
System.print("\nThe first abundant odd number above one billion is:")
abundantOdd.call(1e9+1, 0, 1, true)</langsyntaxhighlight>
 
{{out}}
Line 6,643 ⟶ 6,855:
=={{header|X86 Assembly}}==
Assemble with tasm and tlink /t
<langsyntaxhighlight lang="asm"> .model tiny
.code
.486
Line 6,714 ⟶ 6,926:
int 29h
no30: ret
end start</langsyntaxhighlight>
{{out}}
<pre>
Line 6,747 ⟶ 6,959:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">int Cnt, Num, Div, Sum, Quot;
[Cnt:= 0;
Num:= 3; \find odd abundant numbers
Line 6,771 ⟶ 6,983:
Num:= Num+2;
];
]</langsyntaxhighlight>
 
{{out}}
Line 6,805 ⟶ 7,017:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn oddAbundants(startAt=3){ //--> iterator
Walker.zero().tweak(fcn(rn){
n:=rn.value;
Line 6,818 ⟶ 7,030:
}
}.fp(Ref(startAt.isOdd and startAt or startAt+1)))
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">fcn oddDivisors(n){ // -->sorted List
[3.. n.toFloat().sqrt().toInt(), 2].pump(List(1),'wrap(d){
if( (y:=n/d) *d != n) return(Void.Skip);
Line 6,830 ⟶ 7,042:
println("%6,d: %6,d = %s".fmt(n, ds.sum(0), ds.sort().concat(" + ")))
}
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">oaw:=oddAbundants();
 
println("First 25 abundant odd numbers:");
Line 6,840 ⟶ 7,052:
 
println("\nThe first abundant odd number above one billion is:");
printOAs(oddAbundants(1_000_000_000).next());</langsyntaxhighlight>
{{out}}
<pre style="height:45ex; font-size:83%">
2,295

edits