49
\$\begingroup\$

An Abundant number is any number where the sum of its proper divisors is greater than the original number. For example, the proper divisors of 12 are:

1, 2, 3, 4, 6

And summing these results in 16. Since 16 is larger than 12, 12 is abundant. Note that this does not include "Perfect numbers", e.g. numbers that are equal to the sum of its proper divisors, such as 6 and 28.

Your task for today is to write a program or function that determines if a number is abundant or not. Your program should take a single integer as input, and output a truthy/falsy value depending on whether it is abundant or not. You can assume that the input will always be valid and greater than 0, so for bad inputs, undefined behavior is fine.

You may take your input and output in any reasonable format, for example STDIN/STDOUT, files, or arguments/return values would all be acceptable.

For reference, here are the abundant numbers up to 100:

12,
18,
20,
24,
30,
36,
40,
42,
48,
54,
56,
60,
66,
70,
72,
78,
80,
84,
88,
90,
96,
100

And more can be found on A005101

Since this is , standard loopholes are denied, and try to write the shortest possible code you can in whichever language you happen to choose!

\$\endgroup\$
3
  • 14
    \$\begingroup\$ "the first odd abundant is 945 = 3^3*5*7, the 232nd abundant number!" \$\endgroup\$
    – mbomb007
    Commented Feb 21, 2017 at 14:29
  • 1
    \$\begingroup\$ The asymptotic density of abundant numbers is somewhere within the interval [0.24761748, 0.24764422]. Calculated using the source code included in this paper. \$\endgroup\$
    – Deadcode
    Commented Jan 25, 2019 at 22:20
  • 2
    \$\begingroup\$ I am trying to do this in Geometry Dash. It's a nightmare \$\endgroup\$
    – MilkyWay90
    Commented Jan 29, 2019 at 3:25

71 Answers 71

2
\$\begingroup\$

Pyth, 11 bytes

>sPf!%QTS

Old:

L!%Qb>sPy#S

I can't use !% as a pfn for #, because it's two functions. Makes me sad :(.


L!%Qb>sPy#SQ    Program's argument: Q
L!%Qb           Define a lambda y, that takes b as an argument
 !%Qb           Return true if Q is divisible by b
          S     Make a range 1..Q
        y#      Filter that range with the lambda (y)
       P        Remove the last element (Q itself)
      s         Sum them
     >     Q    Check if that sum is greater than the program's argument
\$\endgroup\$
1
  • \$\begingroup\$ Not defining a function seems to be shorter: >sPf!%QTS \$\endgroup\$ Commented Feb 21, 2017 at 15:42
2
\$\begingroup\$

PowerShell, 43 bytes

param($n)1..$n|%{$s+=$_*!($n%$_)}
$s-gt2*$n

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Common Lisp, 63 bytes

(lambda(n)(>(loop for i from 1 below n if(=(mod n i)0)sum i)n))

Try it online!

\$\endgroup\$
2
\$\begingroup\$

F#, 51 bytes

let f n=Seq.filter(fun x->n%x=0){1..n-1}|>Seq.sum>n

Try it online!

Filters out all the numbers that don't divide evenly into n, then sums them and compares them against n.

\$\endgroup\$
2
\$\begingroup\$

Japt, 4 bytes

<âÔx

Try it online!

\$\endgroup\$
0
2
\$\begingroup\$

C# (Visual C# Interactive Compiler), 48 bytes

x=>Enumerable.Range(1,x).Sum(y=>y<x&x%y<1?y:0)>x

Try it online!

Above is a LINQ based solution that creates a range of numbers slightly larger than the input number. Each element is tested for being a) being a proper divisor of the input and b) being less than the input. If both checks pass, it is added to a sum which is compared to the input.

Below is a a recursive solution that uses an inner function to calculate the summation. While this one seems the most interesting to me, I think the LINQ based approach will not be beat.

C# (Visual C# Interactive Compiler), 60 bytes

x=>{int g(int y)=>(x%y<1?y:0)+(++y<x?g(y):0);return g(1)>x;}

Try it online!

Finally, we have an iterative solution which currently is between the LINQ based approach and the recursive one.

C# (Visual C# Interactive Compiler), 53 bytes

x=>{int y=0,z=0;for(;++y<x;z+=x%y<1?y:0);return z>x;}

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Ruby, 39 32 bytes

->n{n<(1...n).sum{|x|n%x>0?0:x}}

Try it online!

Updated to Ruby 2.6

\$\endgroup\$
2
\$\begingroup\$

R, 32 28 bytes

2*(n=scan())<(x=1:n)%*%!n%%x

Try it online!

Reads n from stdin, returns TRUE for abundant numbers and FALSE otherwise.

\$\endgroup\$
2
\$\begingroup\$

Whispers v2, 49 bytes

> Input
>> ∤1
>> ∑2
>> 3-1
>> 4>1
>> Output 5

Try it online!

How it works

Whispers is a non-linear language, where numbers don't refer to their numerical value; rather they reference the result of that line in the program. Execution always starts at the last line, and branches through these references.

Program structure tree (green are nilad lines):

enter image description here

Therefore, we start on line 6 by outputting the result of line 5, which is

>> 4>1

which calls lines 4 and 1 and compares the results.

Line 1 is the odd line out in the program: as it starts with a single >, rather than a double >>, it is a nilad line. This means it returns its value interpreted literally. Numbers return their numeric value, and Input is preprocessed at the start of execution, being replaced with the first input on STDIN. Therefore, line 5 checks if the result for line 4 is greater than the input.

Line 4, >> 3-1, subtracts the input from line 3's result, while line 3, >>∑2, sums the values on line 2. Line 2, >> ∤1, generates the list of the input's divisors. Therefore, together, the program generates a list of the input's divisors (including the input), sums them, then subtracts the input (in order to account for the input being included in the list of divisors). Finally, we compare the sum to the input, and output the result.

\$\endgroup\$
2
\$\begingroup\$

Zsh, 39 bytes

for ((;++b<$1;t+=$1%b?0:b)):
<<<$[t>$1]

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Vyxal, 3 bytes

∆K<

Try it Online!

\$\endgroup\$
2
\$\begingroup\$

Lua (71 68 bytes)

Thank to @Deadcode for finding a typo in the byte length and for suggesting shorter code.

function A(n)z=0 for i=1,n-1 do z=z+i*#{[n%i+1]=0}end return z>n end
\$\endgroup\$
2
2
\$\begingroup\$

x86 opcode edited from 179016, 20 bytes

f:      mov ecx, eax       ; copy input number N into ECX
        lea ebx, [eax+eax] ; copy input number 2N into EBX
.a:     cdq                ; clear DX (high word for dividend)
        push eax           ; save original dividend
        div ecx            ; divide DX:AX / CX
        pop eax            ; restore dividend
        test edx, edx      ; if remainder = 0, it divides evenly and is a divisor of N
        jnz .b             ; if not, continue loop
        sub ebx, ecx       ; Subtract divisor from EBX; this simultaneously changes the
                           ; running total, and sets the flags for the return value.
        jc .c              ; If CF=1, the sum of divisors subtracted from N has exceeded
                           ; N, thus N is abundant, and we need to exit the loop now to
                           ; return this result in CF.
.b:     loop .a
.c:                        ; return value: CF=0 (non-abundant), CF=1 (abundant)

Try it online!

Saved from the reverse working

\$\endgroup\$
3
  • \$\begingroup\$ Try it online! (please change to a direct link if you include it) Note that this TIO is not executing exactly the same machine code, as it needs to use 0x66 and 0x67 prefixes to do 16-bit operations, but I still think it's much better than nothing. :-) \$\endgroup\$
    – Deadcode
    Commented Mar 8, 2021 at 21:20
  • \$\begingroup\$ @Deadcode Totally changed to 32-bit code and fixed N=1 case with the price that >=0x80000000 fail \$\endgroup\$
    – l4m2
    Commented Mar 9, 2021 at 21:42
  • \$\begingroup\$ 32-bit means you can use the hex-dump code I added: Try it online!. Cool that you chose a different way of dealing with input=1. In yours, values of 2^31 or larger can't be handled. In mine, all 32-bit inputs can be handled, making it 3 bytes longer than yours. (And of course, neither of them can handle input=0.) \$\endgroup\$
    – Deadcode
    Commented Mar 10, 2021 at 18:26
1
\$\begingroup\$

><>, 47+3 46+3 39+3 = 42 bytes

:l)?!v}l:{:}$%0=*{
 v-$0<
l<+v?=1
)n;>0

Expects the input number to be present on the stack at program start, so +3 bytes for the -v flag. Try it online!

Edit: Golfed 1 byte from 3rd line: -\~0$ => -\:+

Edit 2: Near complete rewrite, previous version:

1v
}/!?)@:{:}+1r~?$r}:}%@:{:
-\:+
?\+l1=
;>0)n
\$\endgroup\$
1
\$\begingroup\$

Haskell, 34 bytes

a n=n<sum[a|a<-[1..n-1],mod n a<1]

Try it online! Usage example: a 12 returns True.

\$\endgroup\$
1
\$\begingroup\$

JavaScript ES6, 61 50 bytes

a=>[...Array(a).keys()].reduce((b,c)=>a%c?b:b+c)>a

f=a=>[...Array(a).keys()].reduce((b,c)=>a%c?b:b+c)>a;

console.log(f(11));
console.log(f(12));
console.log(f(17));
console.log(f(18));

\$\endgroup\$
1
  • \$\begingroup\$ 45 bytes \$\endgroup\$
    – Shaggy
    Commented Sep 12, 2017 at 15:51
1
\$\begingroup\$

Pyth, 8 bytes

>s{*MPyP

Try it online here.

>s{*MPyPQQ   Implicit: Q=eval(input())
             Trailing QQ inferred
       PQ    Prime factors of Q
      y      Take the powerset of the above
     P       Discard the last one (i.e. the original set itself)
   *M        Take the product of each list
  {          Deduplicate
 s           Take the sum
>        Q   Is the above greater than Q? Implicit print
\$\endgroup\$
1
\$\begingroup\$

PHP, 63 bytes

function a($x){for($i=1;$i<$x;$i++){$s+=$x%$i?0:$i;}echo$s>$x;}

Usage: a(12);

\$\endgroup\$
1
\$\begingroup\$

APL(NARS) 11 chars, 22 bytes

{⍵<2÷⍨11π⍵}

11π is the function sum divisor, so sum proper divisor would be: {(-⍵)+11π⍵}; test:

  f←{⍵<2÷⍨11π⍵}
  {1=f ⍵:⍵⋄⍬}¨1..100
12  18  20  24  30  36  40  42  48  54  56  60  66  70  72  78  80  84  88  90  96  100 
\$\endgroup\$
1
\$\begingroup\$

Forth (gforth), 45 bytes

: f 0 over 1 ?do over i mod 0= i * - loop < ;

Try it online!

Explanation

Loops over every number from 1 to n-1, summing all values that divide n perfectly. Returns true if sum is greater than n

Code Explanation

: f                \ start word definition
  0 over 1         \ create a value to hold the sum and setup up the bounds of the loop
  ?do              \ start a counted loop from 1 to n. (?do skips if start = end)
    over           \ copy n to the top of the stack
    i mod 0=       \ check if i divides n perfectly
    i * -          \ if so, use the fact that -1 = true in forth to add i to the sum
  loop             \ end the counted loop
  <                \ check if n is less than the sum
;                  \ end the word definition
   
\$\endgroup\$
1
\$\begingroup\$

C# (.NET Core), 59, 53 bytes

Without LINQ.

EDIT: Thanks to ASCII-only for pointing out for(;++k<p;) instead of for(;k<p;k++).

C# (.NET Core), 53 bytes

p=>{int k=0,n=0;for(;++k<p;)n+=p%k<1?k:0;return n>p;}

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 53 \$\endgroup\$
    – ASCII-only
    Commented Mar 13, 2019 at 5:56
1
\$\begingroup\$

Perl 5 -MList::Util=sum -pa, 32 bytes

$_=$_<sum grep!("@F"%$_),1..$_-1

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Python 3, 45 bytes

lambda n:sum(i for i in range(1,n)if n%i<1)>n

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Factor + project-euler.common, 9 bytes

abundant?

Try it online!

Builtin, lol

\$\endgroup\$
1
\$\begingroup\$

C (clang), 47 bytes

g,o;l(f){for(g=o=f;--o;)g-=f%o?0:o;return g<0;}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

cQuents, 6 bytes

$<U\z$

Try it online!

Outputs 1 for true and 0 for false. If no input given, outputs the 1-indexed sequences of 1/0 for whether each index is an abundant number.

Explanation

        each term in the sequence equals
$       index
 <            <
  U             sum (                           )
   \z                 proper divisors (       )
     $                                  index
\$\endgroup\$
1
\$\begingroup\$

Pyt, 7 bytes

ĐĐð⇹\Ʃ<

Try it online!

ĐĐ          implicit input (n); duplicate on stack twice
  ð         get all factors of n
   ⇹\       remove n from list of factors (i.e. reduce list to proper divisors)
     Ʃ      sum proper divisors
      <     is the sum greater than n?; implicit print
\$\endgroup\$
1
\$\begingroup\$

Arturo, 26 bytes

$[n]->n<sum chop factors n

Try it

\$\endgroup\$
0
\$\begingroup\$

Scala, 39 bytes

(n:Int)=>(1 until n filter{n%_<1}sum)>n
\$\endgroup\$
0
\$\begingroup\$

Javascript, 47 Bytes

a=>{s=d=0;while(a>d){s+=a%d?0:d;d++}return s>d}

f=a=>{s=d=0;while(a>d){s+=a%d?0:d;d++}return s>d};
console.log(f(11));
console.log(f(12));
console.log(f(15));
console.log(f(18));

Quite straight forward: check all numbers smaller than the input, if the input is divisible by them add them to the sum, and return whether the sum is bigger than the input

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.