Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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!

share|improve this question

22 Answers 22

Python 2, 41 40 bytes

n=k=j=input()
while~k<0:j-=1;k-=j>>n%j*n

Output is via exit code, so 0 is truthy and 1 is falsy.

Try it online!

How it works

After setting all of n, k, and j to the input from STDIN, we enter the while loop. Said loop will break as soon as -k - 1 = ~k ≥ 0, i.e., k ≤ -1 / k < 0.

In each iteration, we first decrement j to consider only proper divisors of n. If j is a divisor of n, n%j yields 0 and j >> n%j*n = j/20 = j gets subtracted from k. However, if j does not divide n, n%j is positive, so n%j*n is at least n > log2 j and j >> n%j*n = j / 2n%j*n = 0 is subtracted from k.

For abundant numbers, k will reach a negative value before or when j becomes 1, since the sum of n's proper divisors is strictly greater than n. In this case, we break out of the while loop and the program finishes normally.

However, if n is not abundant, j eventually reaches 0. In this case, n%j throws a ZeroDivisionError and the program exits with an error.

share|improve this answer

Python, 44 bytes

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

Try it online!

share|improve this answer
    
It's a shame you can't do range(n). That pesky modulus by zero – DJMcMayhem 8 hours ago

05AB1E, 5 bytes

ÑO¹·›

Try it online!

Ñ     # Push divisors
 O    # Sum
  ¹·  # Push input * 2
    › # print (Sum > 2*input)
share|improve this answer

Mathematica, 17 bytes

Tr@Divisors@#>2#&

Explanation

Tr@                 The sum of the main diagonal of
   Divisors@         the list of divisors of
            #         the first argument
             >      is greater than
              2#      twice the first argument.
                &   End of function.
share|improve this answer

Brachylog, 5 bytes

fk+>?

Try it online!

Explanation

f           Factors
 k          Knife: remove the last one (the input itself)
  +         Sum
   >?       Stricly greater than the Input
share|improve this answer

MATL, 6 bytes

Z\sGE>

Outputs 1 for abundant numbers, 0 otherwise.

How it works

Z\      % list the divisors of the implicit input
s       % add them
G       % push the input again
E       % double it
>       % compare
        % implicitly display result
share|improve this answer

RProgN, 8 Bytes

~_]k+2/<

Explained

~_]k+2/<
~           # Zero Space Segment
 _          # Convert the input to an integer
  ]         # Duplicate the input on the stack
   k+       # Get the sum of the divisors of the top of the stack
     2/     # Divded by 2
       <    # Is the Input less than the sum of its divisors/2.

Try it online!

share|improve this answer

Actually, 5 bytes

;÷Σ½>

Try it online!

;     # Duplicate input
 ÷    # Get divisors
  Σ   # Sum
   ½  # Divide by 2 (float)
    > # Test is greater than input
share|improve this answer

PARI/GP, 15 bytes

n->sigma(n)>2*n

The variant n->sigma(n,-1)>2 is, unfortunately, longer.

share|improve this answer

QBIC, 22 bytes

:[a/2|~a%b|\p=p+b}?p>a

This is an adaptation to the QBIC primality test. Instead of counting the divisors and checking if it's less than three, this sums the proper divisors. This runs only along half of 1 to n, where the primality test runs through 1 to n completely.

Explanation:

:       Get the input number, 'a'
[a/2|   FOR(b=1, b<=(a/2), b++)
~a%b    IF a MOD b != 0  --> QBasic registers a clean division  (0) as false. 
        The IF-branch ('|') therefor is empty, the code is in the ELSE branch ('\')
|\p=p+b THEN add b to runnning total p
}       Close all language constructs: IF/END IF, FOR/NEXT
?p>a    Print '-1' for abundant numbers, 0 otherwise.
share|improve this answer

Retina, 34 bytes

Byte count assumes ISO 8859-1 encoding.

M!&`(1+)$(?<=^\1+)
1>`¶

^(1+)¶1\1

Input in unary, output 1 for abundant numbers, 0 otherwise.

Try it online!

share|improve this answer

Retina, 50 45 bytes

^(?!(1(?<=(?=(?(\3+$)((?>\2?)\3)))^(1+)))*1$)

Input in unary, output 1 for abundant numbers, 0 otherwise.

There is nothing Retina-specific about this solution. The above is a pure .NET regex which matches only abundant numbers.

Try it online! (Test suite that filters decimal input with the above regex.)

share|improve this answer

Jelly, 3 bytes

Æṣ>

Try it online!

How it works

Æṣ>  Main link. Argument: n

Æs   Get the proper divisor sum of n.
  >  Test if the result is greater than n.
share|improve this answer

Java 8, 53 bytes ( a lot more if you include the ceremonial code )

return IntStream.range(1,n).filter(e->n%e<1).sum()>n;

Try it online

Explanation :

IntStream.range(1,n) \\ numbers from 1 to n-1
filter(e->n%e<1)     \\ filter in numbers that perfectly divide the number n
sum()>n              \\ sum and compare to original number
share|improve this answer
    
Great answer, but with Java 8 you must include the function in your byte-count. Then again, you can drop the return if I'm not mistaken, so it will be even shorter: n->IntStream.range(1,n).filter(e->n%e<1).sum()>n (not 100% if this is correct, I almost never program in Java 8). Welcome to PPCG! – Kevin Cruijssen 1 hour ago

Pyth, 11 bytes

L!%Qb>sPy#S

I can't use !% as a pfn for #, which 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
share|improve this answer

Perl 6, 72 28 bytes

{^$^a .grep($^a%%*).sum>$^a}
  • Program argument: a.
  • Generate a list from 1..a.
  • Take all the numbers that are divisors of a.
  • Sum them.
  • Check if that sum is greater than a.
share|improve this answer

Pure Bash, 43 bytes

for((;k<$1;k++)){((s+=$1%k?0:k));};((s>$1))

The input is passed as an argument.

The output is returned in the exit code: 0 for abundant, 1 for not abundant.

Output to stderr should be ignored.

Test runs:

for n in {1..100}; do if ./abundant "$n"; then echo $n; fi; done 2>/dev/null
12
18
20
24
30
36
40
42
48
54
56
60
66
70
72
78
80
84
88
90
96
100
share|improve this answer

Batch, 84 bytes

@set/ak=%1*2
@for /l %%j in (1,1,%1)do @set/ak-=%%j*!(%1%%%%j)
@cmd/cset/a"%k%>>31

Outputs -1 for an abundant number, 0 otherwise. Works by subtracting all the factors from 2n and then shifting the result 31 places to extract the sign bit. Alternative formulation, also 84 bytes:

@set k=%1
@for /l %%j in (1,1,%1)do @set/ak-=%%j*!(%1%%%%j)
@if %k% lss -%1 echo 1

Outputs 1 for an abundant number. Works by subtracting all the factors from n and then comparing the result to to -n. (set/a is Batch's only way of doing arithmetic so I can't easily adjust the loop.)

share|improve this answer

Java 7, 69 bytes

boolean c(int n){int s=0,i=1;for(;++i<=n/2;s+=n%i<1?i:0);return s>n;}

Explanation:

boolean c(int n){                      // boolean method with integer parameter
   int s = 0,                          // sum initialization
       i = 1;                          // counter initialization (starts at 2: i=1 and ++i becomes i=2)
   for(; ++i <= n/2; s += n % i < 1    // loop while (i <= n/2)
                           ? i         //   if (n % i == 0)
                           : 0);       //     r = r + i
   return s > n;                       // return sum > input
 }

Test code:

Try it here.

class M{
  static boolean c(int n){int s=0,i=1;for(;++i<=n/2;s+=n%i<1?i:0);return s>n;}

  public static void main(String[] a){
    for(int i = 0; i <= 100; i++){
      System.out.print(i+": "+c(i) + ";  ");
    }
  }
}

Output:

0: false;  1: false;  2: false;  3: false;  4: false;  5: false;  6: false;  7: false;  8: false;  9: false;  10: false;  
11: false;  12: true;  13: false;  14: false;  15: false;  16: false;  17: false;  18: true;  19: false;  
20: true;  21: false;  22: false;  23: false;  24: true;  25: false;  26: false;  27: false;  28: false;  29: false;  
30: true;  31: false;  32: false;  33: false;  34: false;  35: false;  36: true;  37: false;  38: false;  39: false;  
40: true;  41: false;  42: true;  43: false;  44: false;  45: false;  46: false;  47: false;  48: true;  49: false;  
50: false;  51: false;  52: false;  53: false;  54: true;  55: false;  56: true;  57: false;  58: false;  59: false;  
60: true;  61: false;  62: false;  63: false;  64: false;  65: false;  66: true;  67: false;  68: false;  69: false;  
70: true;  71: false;  72: true;  73: false;  74: false;  75: false;  76: false;  77: false;  78: true;  79: false;  
80: true;  81: false;  82: false;  83: false;  84: true;  85: false;  86: false;  87: false;  88: true;  89: false;  
90: true;  91: false;  92: false;  93: false;  94: false;  95: false;  96: true;  97: false;  98: false;  99: false;  100: true;  
share|improve this answer

JavaScript ES6, 61 bytes

a=>[...Array(a).keys()].filter(b=>a%b<1).reduce((c,d)=>c+d)>a

f=a=>[...Array(a).keys()].filter(b=>a%b<1).reduce((c,d)=>c+d)>a;

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

share|improve this answer

Japt, 9 bytes

â kU x >U

Try it online!

share|improve this answer
    
9 characters, 10 bytes. – Metoniem 19 mins ago
    
@Metoniem I'm sure â is 1 byte, in unicode at least (0xE2). – TomDevs 10 mins ago

><>, 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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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