An abundance of integers!

40

6

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!

James

Posted 2017-02-21T01:01:38.620

Reputation: 54 537

11"the first odd abundant is 945 = 3^357, the 232nd abundant number!" – mbomb007 – 2017-02-21T14:29:19.337

The asymptotic density of abundant numbers is somewhere within the interval [0.24761748, 0.24764422]. Calculated using the source code included in this paper.

– Deadcode – 2019-01-25T22:20:24.030

1I am trying to do this in Geometry Dash. It's a nightmare – MilkyWay90 – 2019-01-29T03:25:13.047

Answers

43

ECMAScript Regex, 1085 855 597 536 511 508 504 bytes

Matching abundant numbers in ECMAScript regex is an entirely different beast than doing so in practically any other regex flavor. The lack of forward/nested backreferences or recursion means that it is impossible to directly count or keep a running total of anything. The lack of lookbehind makes it often a challenge even to have enough space to work in.

Many problems must be approached from an entirely different perspective, and you'll be wondering if they're solvable at all. It forces you to cast a much wider net in finding which mathematical properties of the numbers you're working with might be able to be used to make a particular problem solvable.

Back in March-April 2014 I constructed a solution to this problem in ECMAScript regex. At first I had every reason to suspect the problem was completely impossible, but then the mathematician teukon sketched an idea that made an encouraging case for making it look solvable after all – but he made it clear he had no intention of constructing the regex (he had competed/cooperated with my on constructing/golfing previous regexes, but reached his limit by this point and was content to restrict his further contributions to theorizing).

As with my regex posted a couple days ago, I'll give a warning: I highly recommend learning how to solve unary mathematical problems in ECMAScript regex. It's been a fascinating journey for me, and I don't want to spoil it for anybody who might potentially want to try it themselves, especially those with an interest in number theory. See that post for a list of consecutively spoiler-tagged recommended problems to solve one by one.

So do not read any further if you don't want some deep unary regex magic spoiled for you. My previous post was just a small taste. If you do want to take a shot at figuring out this magic yourself, I highly recommend starting by solving some problems in ECMAScript regex as outlined in that post linked above.

Before posting my ECMAScript regex, I thought it would be interesting to analyze Martin Ender's .NET pure regex solution, ^(?!(1(?<=(?=(?(\3+$)((?>\2?)\3)))^(1+)))*1$). It turns out to be very straightforward to understand that regex, and it is elegant in its simplicity. To demonstrate the contrast between our solutions, here is a commented and pretty-printed (but unmodified) version of his regex:

# For the purpose of these comments, the input number will be referred to as N.

^(?!                  # Attempt to add up all the divisors. Since this is a regex and we
                      # can only work within the available space of the input, that means
                      # if the sum of the divisors is greater than N, the attempt to add
                      # all the divisors will fail at some point, causing this negative
                      # lookahead to succeed, showing that N is an abundant number.

  (1                  # Cycle through all values of tail that are less than N, testing
                      # each one to see if it is a divisor of N.

    (?<=              # Temporarily go back to the start so we can directly operate both
                      # on N and the potential divisor. This requires variable-length
                      # lookbehind, a .NET feature – even though this special case of
                      # going back to the start, if done left-to-right, would actually be
                      # very easy to implement even in a regex flavour that has no
                      # lookbehind to begin with. But .NET evaluates lookbehinds right
                      # to left, so please read these comments in the order indicated,
                      # from [Step 1] to [Step 7]. The comment applying to entering the
                      # lookahead group, [Step 2], is shown on its closing parenthesis.
      (?=             # [Step 3] Since we're now in a lookahead, evaluation is left to
                      #          right.
        (?(\3+$)      # [Step 4] If \3 is a divisor of N, then...
          (           # [Step 5] Add it to \2, the running total sum of divisors:
                      #          \2 = \2 + \3         
            (?>\2?)   # [Step 6] Since \2 is a nested backref, it will fail to match on
                      #          the first iteration. The "?" accounts for this, making
                      #          it add zero to itself on the first iteration. This must
                      #          be done before adding \3, to ensure there is enough room
                      #          for the "?" not to cause the match to become zero-length
                      #          even if \2 has a value.
            \3        # [Step 7] Iff we run out of space here, i.e. iff the sum would
                      #          exceed N at this point, the match will fail, making the
                      #          negative lookahead succeed, showing that we have an
                      #          abundant number.
          )

        )
      )               # [Step 2] Enter a lookahead that is anchored to the start due to
                      #          having a "^" immediately to its right. The regex would
                      #          still work if the "^" were moved to the left of the
                      #          lookahead, but would be slightly slower, because the
                      #          engine would do some spurious matching before hitting
                      #          the "^" and backtracking.
      ^(1+)           # [Step 1] \3 = number to test for being a potential divisor – its
                      #               right-side-end is at the point where the lookbehind
                      #               started, and thus \3 cycles through all values from
                      #               1 to N-1.
    )
  )*1$                # Exclude N itself from being considered as a potential divisor,
                      # because if we included it, the test for proper abundance would be
                      # the sum of divisors exceeding 2*N. We don't have enough space for
                      # that, so instead what would happen if we did not exclude N as a
                      # divisor would be testing for "half-abundance", i.e. the sum of
                      # all divisors other than N exceeding N/2. By excluding N as a
                      # divisor we can let our threshold for abundance be the sum of
                      # divisors exceeding N.
)

Try the .NET regex online

Now, back to my ECMAScript regex. First, here it is in raw, whitespace-and-comment-free format:

^(?=(((?=(xx+?)\3+$)(x+)\4*(?=\4$))+(?!\3+$)(?=(xx(x*?))\5*$)x)(x+))(?=\1(x(x*))(?=\8*$)\6\9+$)(?=(.*)((?=\8*$)\5\9+$))(?=(x*?)(?=(x\11)+$)(?=\12\10|(x))(x(x*))(?=\15*$)(?=\11+$)\11\16+$)(?=(x(x*))(?=\17*$)\7\18+$)((?=(x*?(?=\17+$)(?=\17+?(?=((xx(x*))(?=\18+$)\22*$))(x+).*(?=\17$)\24*(?=\24$)(?!(xx+)\25*(?!\22+$)\25$)\22+$)((?=(x\7)+$)\15{2}\14|)))(?=.*(?=\24)x(x(x*))(?=\28*$)\23\29*$)(?=.*(x((?=\28*$)\22\29+$)))(.*(?!\30)\20|(?=.*?(?!x\20)(?=\30*$)(x(x*))(?=\33*$)(?=\31+$)\31\34+$).*(?=\33\21$)))+$

(change \14 to \14? for compatibility with PCRE, .NET, and practically every other regex flavour that's not ECMAScript)

Try it online!
Try it online! (faster, 537 byte version of the regex)

And now a brief summary of the story behind it.

At first it was very non-obvious, to me at least, that it was even possible to match primes in the general case. And after solving that, the same applied to powers of 2. And then powers of composite numbers. And then perfect squares. And even after solving that, doing generalized multiplication seemed impossible at first.

In an ECMAScript loop, you can only keep track of one changing number; that number cannot exceed the input, and has to decrease at every step. My first working regex for matching correct multiplication statements A*B=C was 913 bytes, and worked by factoring A, B, and C into their prime powers – for each prime factor, repeatedly divide the pair of prime power factors of A and C by their prime base until the one corresponding to A reaches 1; the one corresponding to C is then compared to the prime power factor of B. These two powers of the same prime were "multiplexed" into a single number by adding them together; this would always be unambiguously separable on each subsequent iteration of the loop, for the same reason that positional numeral systems work.

We got multiplication down to 50 bytes using a completely different algorithm (which teukon and I were able to arrive at independently, though it took him only a few hours and he went straight to it, whereas it took me a couple days even after it was brought to my attention that a short method existed): for A≥B, A*B=C if any only if C is the smallest number which satisfies C≡0 mod A and C≡B mod A-1. (Conveniently, the exception of A=1 needs no special handling in regex, where 0%0=0 yields a match.) I just can't get over how neat it is that such an elegant way of doing multiplication exists in such a minimal regex flavour. (And the requirement of A≥B can be replaced with a requirement that A and B are prime powers of the same power. For the case of A≥B, this can be proven using the Chinese remainder theorem.)

If it had turned out that there was no simpler algorithm for multiplication, the abundant number regex would probably be on the order of ten thousand bytes or so (even taking into account that I golfed the 913 byte algorithm down to 651 bytes). It does lots of multiplication and division, and ECMAScript regex has no subroutines.

I started working on the abundant number problem tangentially in 23 March 2014, by constructing a solution for what seemed at the time to be a sub-problem of this: Identifying the prime factor of highest multiplicity, so that it could be divided out of N at the start, leaving room to do some necessary calculations. At the time this seemed to be a promising route to take. (My initial solution ended up being quite large at 326 bytes, later golfed down to 185 bytes.) But the rest of the method teukon sketched would have been extremely complicated, so as it turned out, I took a rather different route. It proved to be sufficient to divide out the largest prime power factor of N corresponding to the largest prime factor on N; doing this for the prime of highest multiplicity would have added needless complexity and length to the regex.

What remained was treating the sum of divisors as a product of sums instead of a straight sum. As explained by teukon on 14 March 2014:

We're given a number n = p0a0p1a1...pk-1ak-1. We want to handle the sum of the factors of n, which is (1 + p0 + p02 + ... + p0a0)(1 + p1 + p12 + ... + p1a1)...(1 + pk-1 + pk-12 + ... + pk-1ak-1).

It blew my mind to see this. I had never thought of factoring the aliquot sum in that way, and it was this formula more than anything else that made the solvability of abundant number matching in ECMAScript regex look plausible.

In the end, instead of testing for a result of addition or multiplication exceeding N, or testing that such a result pre-divided by M exceeds N/M, I went with testing if a result of division is less than 1. I arrived at the first working version on 7 April 2014.

The full history of my golf optimizations of this regex is on github. At a certain point one optimization ended up making the regex much slower, so from that point on I maintained two versions. They are:

regex for matching abundant numbers.txt
regex for matching abundant numbers - shortest.txt

These regexes are fully compatible with both ECMAScript and PCRE, but a recent optimization involved using a potentially non-participating capture group \14, so by dropping PCRE compatibility and changing \14? to \14 they can both be reduced by 1 byte.

Here is the smallest version, with that optimization applied (making it ECMAScript-only), reformatted to fit in a StackExchange code block with (mostly) no horizontal scrolling needed:

# Match abundant numbers in the domain ^x*$ using only the ECMAScript subset of regex
# functionality. For the purposes of these comments, the input number = N.
^
# Capture the largest prime factor of N, and the largest power of that factor that is
# also a factor of N. Note that the algorithm used will fail if N itself is a prime
# power, but that's fine, because prime powers are never abundant.
(?=
  (                      # \1 = tool to make tail = Z-1
    (                    # Repeatedly divide current number by its smallest factor
      (?=(xx+?)\3+$)
      (x+)\4*(?=\4$)
    )+                   # A "+" is intentionally used instead of a "*", to fail if N
                         #  is prime. This saves the rest of the regex from having to
                         #  do needless work, because prime numbers are never abundant.
    (?!\3+$)             # Require that the last factor divided out is a different prime.
    (?=(xx(x*?))\5*$)    # \5 = the largest prime factor of N; \6 = \5-2
    x                    # An extra 1 so that the tool \1 can make tail = Z-1 instead of just Z
  )
  (x+)                   # Z = the largest power of \5 that is a factor of N; \7 = Z-1
)
# We want to capture Z + Z/\5 + Z/\5^2 + ... + \5^2 + \5 + 1 = (Z * \5 - 1) / (\5 - 1),
# but in case Z * \5 > N we need to calculate it as (Z - 1) / (\5 - 1) * \5 + 1.
# The following division will fail if Z == N, but that's fine, because no prime power is
# abundant.
(?=
  \1                     # tail = (Z - 1)
  (x(x*))                # \8   = (Z - 1) / (\5 - 1); \9 = \8-1
  # It is guaranteed that either \8 > \5-1 or \8 == 1, which allows the following
  # division-by-multiplication to work.
  (?=\8*$)
  \6\9+$
)
(?=
  (.*)                   # \10 = tool to compare against \11
  (                      # \11 = \8 * \5  =  (Z - 1) / (\5 - 1) * \5; later, \13 = \11+1
    (?=\8*$)
    \5\9+$
  )
)
# Calculate Q = \15{2} + Q_R = floor(2 * N / \13). Since we don't have space for 2*N, we
# need to calculate N / \13 first, including the fractional part (i.e. the remainder),
# and then multiply the result, including the fractional part, by 2.
(?=
  (x*?)(?=(x\11)+$)      # \12 = N % \13; \13 = \11 + 1
  (?=\12\10|(x))         # \14 = Q_R = floor(\12 * 2 / \13)
                         #     = +1 carry if \12 * 2 > \11, or NPCG otherwise
  (x(x*))                # \15 = N / \13; \16 = \15-1
  (?=\15*$)
  (?=\11+$)              # must match if \15 <  \13; otherwise doesn't matter
  \11\16+$               # must match if \15 >= \13; otherwise doesn't matter
)
# Calculate \17 = N / Z. The division by Z can be done quite simply, because the divisor
# is a prime power.
(?=
  (x(x*))                # \17 = N / Z; \18 = \17-1
  (?=\17*$)
  \7\18+$
)
# Seed a loop which will start with Q and divide it by (P^(K+1)-1)/(P-1) for every P^K
# that is a factor of \17. The state is encoded as \17 * P + R, where the initial value
# of R is Q, and P is the last prime factor of N to have been already processed.
#
# However, since the initial R would be larger than \17 (and for that matter there would
# be no room for any nonzero R since with the initial value of P, it is possible for
# \17 * P to equal N), treat it as a special case, and let the initial value of R be 0,
# signalling the first iteration to pretend R=Q. This way we can avoid having to divide Q
# and \17 again outside the loop.
#
# While we're at it, there's really no reason to do anything to seed this loop. To seed
# it with an initial value of P=\5, we'd have to do some multiplication. If we don't do
# anything to seed it, it will decode P=Z. That is wrong, but harmless, since the next
# lower prime that \17 is divisible by will still be the same, as \5 cannot be a factor
# of \17.

# Start the loop.
(
  (?=
    (                    # \20 = actual value of R
      x*?(?=\17+$)       # move forward by directly decoded value of R, which can be zero
      # The division by \17 can be done quite simply, because it is known that
      # the quotient is prime.
      (?=
        \17+?            # tail = \17 * (a prime which divides into \17)
        (?=
          (              # \21 = encoded value for next loop iteration
            (xx(x*))     # \22 = decoded value of next smaller P; \23 = (\22-1)-1
            (?=\18+$)    # iff \22 > \17, this can have a false positive, but never a false negative
            \22*$        # iff \22 < \17, this can have a false positive, but never a false negative
          )
        )
        # Find the largest power of \22 that is a factor of \17, while also asserting
        # that \22 is prime.
        (x+)             # \24 = the largest power of \22 that is a factor of \17
        .*(?=\17$)
        \24*(?=\24$)
        (?!
          (xx+)\25*
          (?!\22+$)
          \25$
        )
        \22+$
      )
      (
        (?=(x\7)+$)      # True iff this is the first iteration of the loop.
        \15{2}\14        # Potentially unset capture, and thus dependent on ECMAScript
                         # behavior. Change "\14" to "\14?" for compatibility with non-
                         # ECMAScript engines, so that it will act as an empty capture
                         # with engines in which unset backrefs always fail to match.
      |
      )
    )
  )
  # Calculate \30 = (\24 - 1) / (\22 - 1) * \22 + 1
  (?=
    .*(?=\24)x           # tail = \24 - 1
    (x(x*))              # \28 = (\24 - 1) / (\22 - 1); \29 = \28-1
    (?=\28*$)
    \23\29*$
  )
  (?=
    .*(x(                # \30 = 1 + \28 * \22 = (\28 - 1) / (\22 - 1) * \22 + 1; \31 = \30-1
      (?=\28*$)
      \22\29+$
    ))
  )
  # Calculate \33 = floor(\20 / \30)
  (
    .*(?!\30)\20         # if dividing \20 / \30 would result in a number less than 1,
                         # then N is abundant and we can exit the loop successfully
  |
    (?=
      .*?(?!x\20)(?=\30*$)
      (x(x*))            # \33 = \20 / \30; \34 = \33-1
      (?=\33*$)
      (?=\31+$)          # must match if \33 <  \30; otherwise doesn't matter
      \31\34+$           # must match if \33 >= \30; otherwise doesn't matter
    )
    # Encode the state for the next iteration of the loop, as \17 * \22 + \33
    .*(?=\33\21$)
  )
)+$

Deadcode

Posted 2017-02-21T01:01:38.620

Reputation: 3 022

Comments are not for extended discussion; this conversation has been moved to chat.

– James – 2019-01-23T20:35:34.383

2-98 bytes – Grimmy – 2019-03-05T17:19:14.573

27

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.

Dennis

Posted 2017-02-21T01:01:38.620

Reputation: 196 637

4~k<0 is fancy, but I think -1<k also does the trick ;) – Martin Ender – 2017-02-21T13:55:21.743

12

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

Fatalize

Posted 2017-02-21T01:01:38.620

Reputation: 32 976

10

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.

Dennis

Posted 2017-02-21T01:01:38.620

Reputation: 196 637

10

Python, 44 bytes

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

Try it online!

Jonathan Allan

Posted 2017-02-21T01:01:38.620

Reputation: 67 804

It's a shame you can't do range(n). That pesky modulus by zero – James – 2017-02-21T01:44:24.003

10

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.

ngenisis

Posted 2017-02-21T01:01:38.620

Reputation: 4 600

1I'm surprised Mathematica has no built in for this.. – MrPaulch – 2017-02-21T17:08:31.387

1@MrPaulch Considering the length of the program though, the builtin may very well be longer in name >.> – Conor O'Brien – 2017-02-22T01:51:58.497

1@ConorO'Brien If it existed, it would probably be AbundantNumberQ, so it would save a couple bytes :) – ngenisis – 2017-02-22T01:54:23.943

8

05AB1E, 5 4 bytes

-1 bytes thanks to scottinet

ѨO‹

Try it online! or Try 0 to 100

Riley

Posted 2017-02-21T01:01:38.620

Reputation: 11 345

@scottinet Nice! – Riley – 2017-09-12T15:50:47.570

7

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.)

Martin Ender

Posted 2017-02-21T01:01:38.620

Reputation: 184 808

6

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!

Explanation

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

We start by getting all divisors of the input. To do this, we return (!) all overlapping (&) matches (M) of the regex (1+)$(?<=^\1+). The regex matches some suffix of the input, provided that the entire input is a multiple of that suffix (which we ensure by trying to reach the beginning fo the string using only copies of the suffix). Due to the way the regex engine looks for matches, this will result a list of divisors in descending order (separated by linefeeds).

1>`¶

The stage itself simply matches linefeeds () and removes them. However, the 1> is a limit, which skips the first match. So this effectively adds together all divisors except the input itself. We end up with the input on the first line and the sum of all proper divisors on the second line.

^(1+)¶1\1

Finally, we try to match the at least one more 1 on the second line than we have on the first line. If that's the case, the sum of proper divisors exceeds the input. Retina counts the number of matches of this regex, which will be 1 for abundant numbers and 0 otherwise.

Martin Ender

Posted 2017-02-21T01:01:38.620

Reputation: 184 808

1It's always amazed me how you can do math in retina. I'd love to see an explanation! :) – James – 2017-02-21T14:49:11.910

1@DJMcMayhem Sorry forgot to add that earlier. Done. – Martin Ender – 2017-02-21T14:55:06.573

6

8086 Assembly, 23 28 25 24 bytes

8bc8 d1e9 33db 33d2 50f7 f158 85d2 7502 03d9 7204 e2f0 3bc3

Unassembled:

; calculate if N (1 < N <= 65535) is abundant
; input: N (mem16/r16)
; output: CF=1 -> abundant, CF=0 -> not abundant
ABUND   MACRO   N 
        LOCAL DIV_LOOP, CONT_LOOP, END_ABUND
        IFDIFI <N>,<AX> ; skip if N is already AX
    MOV  AX, N          ; AX is dividend
        ENDIF
    MOV  CX, AX         ; counter starts at N / 2
    SHR  CX, 1          ; divide by 2
    XOR  BX, BX         ; clear BX (running sum of factors)
DIV_LOOP:
    XOR  DX, DX         ; clear DX (high word for dividend)
    PUSH AX             ; save original dividend
    DIV  CX             ; DX = DX:AX MOD CX, AX = DX:AX / CX
    POP  AX             ; restore dividend (AX was changed by DIV)
    TEST DX, DX         ; if remainder (DX) = 0, it divides evenly so CX is a divisor
    JNZ  CONT_LOOP      ; if not, continue loop to next
    ADD  BX, CX         ; add number to sum
    JC   END_ABUND      ; if CF=1, BX has unsigned overflow it is abundant (when CX < 65536)
CONT_LOOP:
    LOOP DIV_LOOP
    CMP  AX, BX         ; BX<=AX -> CF=0 (non-abund), BX>AX -> CF=1 (abund)
END_ABUND:
        ENDM

Example test program, testing N = [12..1000]:

    MOV  AX, 12         ; start tests at 12
LOOP_START:
    ABUND AX            ; call ABUND MACRO for N (in AX)
    JNC  LOOP_END       ; if not abundant, display nothing
    CALL OUTDECCSV      ; display AX as decimal (generic decimal output routine)
LOOP_END:
    INC  AX             ; increment loop counter
    CMP  AX, 1000       ; if less than 1000...
    JBE  LOOP_START     ; continue loop
    RET                 ; return to DOS

Output [2..1000]

12, 18, 20, 24, 30, 36, 40, 42, 48, 54, 56, 60, 66, 70, 72, 78, 80, 84, 88, 90, 96, 100, 102, 104, 108, 112, 114, 120, 126, 132, 138, 140, 144, 150, 156, 160, 162, 168, 174, 176, 180, 186, 192, 196, 198, 200, 204, 208, 210, 216, 220, 222, 224, 228, 234, 240, 246, 252, 258, 260, 264, 270, 272, 276, 280, 282, 288, 294, 300, 304, 306, 308, 312, 318, 320, 324, 330, 336, 340, 342, 348, 350, 352, 354, 360, 364, 366, 368, 372, 378, 380, 384, 390, 392, 396, 400, 402, 408, 414, 416, 420, 426, 432, 438, 440, 444, 448, 450, 456, 460, 462, 464, 468, 474, 476, 480, 486, 490, 492, 498, 500, 504, 510, 516, 520, 522, 528, 532, 534, 540, 544, 546, 550, 552, 558, 560, 564, 570, 572, 576, 580, 582, 588, 594, 600, 606, 608, 612, 616, 618, 620, 624, 630, 636, 640, 642, 644, 648, 650, 654, 660, 666, 672, 678, 680, 684, 690, 696, 700, 702, 704, 708, 714, 720, 726, 728, 732, 736, 738, 740, 744, 748, 750, 756, 760, 762, 768, 770, 774, 780, 784, 786, 792, 798, 800, 804, 810, 812, 816, 820, 822, 828, 832, 834, 836, 840, 846, 852, 858, 860, 864, 868, 870, 876, 880, 882, 888, 894, 896, 900, 906, 910, 912, 918, 920, 924, 928, 930, 936, 940, 942, 945, 948, 952, 954, 960, 966, 968, 972, 978, 980, 984, 990, 992, 996, 1000

Output [12500..12700]

12500, 12504, 12510, 12512, 12516, 12520, 12522, 12528, 12530, 12534, 12540, 12544, 12546, 12552, 12558, 12560, 12564, 12570, 12572, 12576, 12580, 12582, 12584, 12588, 12594, 12600, 12606, 12612, 12618, 12620, 12624, 12628, 12630, 12636, 12640, 12642, 12648, 12650, 12654, 12656, 12660, 12666, 12670, 12672, 12678, 12680, 12684, 12688, 12690, 12696, 12700

Output [25100..25300]

25100, 25104, 25110, 25116, 25120, 25122, 25128, 25130, 25134, 25140, 25144, 25146, 25152, 25158, 25160, 25164, 25168, 25170, 25172, 25176, 25180, 25182, 25188, 25194, 25200, 25206, 25212, 25216, 25218, 25220, 25224, 25228, 25230, 25232, 25236, 25240, 25242, 25245, 25248, 25254, 25256, 25260, 25266, 25270, 25272, 25278, 25280, 25284, 25290, 25296, 25300

Updates:

  • Fixed for 16-bit overflow (+5 bytes). Thanks @deadcode for the suggestions!
  • Simplified return logic (-3 bytes). Thx to help from @deadcode once again.
  • Use TEST instead of CMP (-1 byte). Thx to @l4m2!

640KB

Posted 2017-02-21T01:01:38.620

Reputation: 7 149

1I would suggest replacing JLE with JBE to double the range of numbers this can test before overflows start causing it to give false negatives. Then instead of starting to fail at 12600 (aliquot sum 35760), it'll start to fail at 25200 (aliquot sum 74744). Even better would be to also detect the carry flag and treat that as an abundant number without needing to calculate the actual >16 bit sum. – Deadcode – 2019-01-23T09:11:37.233

1Good catch @Deadcode. I've updated the code for jump below instead of jump less. I see what you mean, doing a JC after the ADD BX,CX will catch the unsigned overflow there and make it correct up until N=65535. Complicates flag testing and return state a bit, since previously CF implied false. Updated with fix also. – 640KB – 2019-01-23T17:04:37.283

Nice fixes. But you didn't correctly update your comments; the reverse of JBE is JA (not JG), and these both look at only CF and ZF, ignoring SF and OF. – Deadcode – 2019-01-24T23:30:57.963

1You can save 3 bytes by inverting the specification of your return value, with CF being set if abundant and clear if not. But I'd recommend doing the edit to correct the output documentation first, so it looks nice in the edit history. – Deadcode – 2019-01-24T23:31:00.697

1Also, to keep things simple, the specification should be that the return value is in the carry flag, and none of that fuss with the other flags. The caller should use JC or JNC to act on whether the number is abundant or not. – Deadcode – 2019-01-24T23:37:58.513

Also, it is misleading to call the unassembled version "Ungolfed". If assembling it with an assembler results in the machine code you have shown, then it is golfed. The fact that it is pretty-printed and has comments has no effect on the output when the program is assembled. Calling it "Unassembled" would be correct. The only reason you should call such a listing "Ungolfed" is if you did manual optimizations directly to the machine language bytes that cannot be, or have not been, done in an assembly language. – Deadcode – 2019-01-24T23:46:43.700

1Thanks so much for all of your help and your comments. I've updated the inline documentation and removed the term ungolfed. Honestly never gave it much thought but I see your point about it, as it isn't any different with the exception of inline comments. Also agree about makint the return flags clearer as well. Will work on that in a bit. Thanks for the attention and the assistance! – 640KB – 2019-01-25T00:30:38.430

1@Deadcode, once again very clever observation and suggestion about simplifying return flags. I've implemented your change and it is now 3 bytes smaller. Bravo! – 640KB – 2019-01-25T17:01:35.480

1Beautiful, nicely done! – Deadcode – 2019-01-25T19:05:37.013

SHR CX, 1 => DEC CX ? CMP DX, 0 => TEST DX, DX? – l4m2 – 2019-01-29T09:43:13.210

Thanks @l4m2! Updated to use TEST for -1 byte. – 640KB – 2019-01-30T16:39:35.390

Oops. You should use the SHR CX, 1DEC CX suggestion too. Although it'll make the function approximately twice as slow, it's good golf as it saves 1 byte. The only important thing is that all proper divisors still get tested and N itself is skipped. – Deadcode – 2019-01-31T03:14:24.887

5

Actually, 5 bytes

;÷Σ½>

Try it online!

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

Riley

Posted 2017-02-21T01:01:38.620

Reputation: 11 345

5

05AB1E, 4 bytes

ѨO‹

Try it online!

How it works

Ñ        #list of divisors
 ¨       #remove last element (i.e the input from the list of factors)
  O      #sum the list 
   ‹     #is this sum less than the input? 

Sorry to post in old question, I was just going through old posts for practise and noticed my solution was shorter than the next best 05AB1E solution.

space junk

Posted 2017-02-21T01:01:38.620

Reputation: 305

4Sorry to post in old question Don't worry about it! I'm always happy to see answers on my old challenges, and it's actually encouraged around here. :) – James – 2017-07-18T15:44:31.820

4

PARI/GP, 15 bytes

n->sigma(n)>2*n

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

Charles

Posted 2017-02-21T01:01:38.620

Reputation: 2 435

4

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

Gautam D

Posted 2017-02-21T01:01:38.620

Reputation: 41

4Great 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 – 2017-02-21T09:30:21.777

1The correct count via standard count would be n->java.util.stream.IntStream.range(1,n).filter(e->n%e<1).sum()>n for 65 bytes (assuming I got the package right off the top of my head) – CAD97 – 2017-02-21T14:30:47.543

4

Powershell, 51 49 Bytes

param($i)((1..$i|?{!($i%$_)})-join"+"|iex)-gt2*$i

I wish I could remove some brackets.

-2 thanks to AdmBorkBork, instead of not counting the input in the initial range, we just take it into account in the final check.

Loop through range of 1.. to the $input, minus 1, find where (?) the inverse modulo of input by the current number is $true (aka only 0) - then -join all of those numbers together with + and iex the resulting string to calculate it, then see if the sum of these parts is greater than the input.

PS C:\++> 1..100 | ? {.\abundance.ps1 $_}
12
18
20
24
30
36
40
42
48
54
56
60
66
70
72
78
80
84
88
90
96
100

colsw

Posted 2017-02-21T01:01:38.620

Reputation: 3 195

You can save two bytes by counting the top value and checking that it's bigger than 2x the input -- param($i)((1..$i|?{!($i%$_)})-join"+"|iex)-gt2*$i – AdmBorkBork – 2017-02-21T15:27:44.747

3

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

B. Mehta

Posted 2017-02-21T01:01:38.620

Reputation: 763

3

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.

steenbergh

Posted 2017-02-21T01:01:38.620

Reputation: 7 772

3

Pure Bash, 37 bytes

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

Thanks to @Dennis for rearranging the code -- saving 6 bytes and eliminating the incidental output to stderr.

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

Mitchell Spector

Posted 2017-02-21T01:01:38.620

Reputation: 3 392

3

Japt, 9 7 6 bytes

<Uâ1 x

Saved 2 bytes thanks to ETHproductions. Saved 1 byte thanks to obarakon.

Try it online!

Tom

Posted 2017-02-21T01:01:38.620

Reputation: 3 078

9 characters, 10 bytes. – Metoniem – 2017-02-21T10:20:17.683

@Metoniem I'm sure â is 1 byte, in unicode at least (0xE2). – Tom – 2017-02-21T10:29:42.053

1

@Metoniem Japt uses the ISO-8859-1 encoding, in which â is a single byte.

– ETHproductions – 2017-02-21T14:29:28.830

If â is given a truthy argument, it will remove the actual number from the remaining list, so you can do â1 x >U to save a couple bytes :-) – ETHproductions – 2017-02-21T14:30:59.420

@TomDevs Nice! You can do <Uâ1 x to save a byte. Japt adds the U in front of the program. – Oliver – 2017-02-21T16:12:58.293

@obarakon Thanks! – Tom – 2017-02-21T16:16:09.560

@ETHproductions Yeah, I only just now figured out they're not all counted in the same encoding. Oops :( – Metoniem – 2017-02-22T08:08:49.053

You can ditch the U to save another byte. – Shaggy – 2017-09-12T15:27:44.537

3

JavaScript (ES6), 33 bytes

let g =
x=>(f=n=>--n&&n*!(x%n)+f(n))(x)>x
<input type=number min=1 value=1 step=1 oninput="O.innerHTML=g(+value)"><br>
<pre id=O>false</pre>

ETHproductions

Posted 2017-02-21T01:01:38.620

Reputation: 47 880

I was sure a recursive answer would be best but I didn't think it would be this good. – Neil – 2017-02-22T00:18:30.460

3

Cubix, 38 bytes

/..?%?(O;0I:^.<.>;rrw+s;rUO?-<...O0L.@

Try it here

      / . .
      ? % ?
      ( O ;
0 I : ^ . < . > ; r r w
+ s ; r U O ? - < . . .
O 0 L . @ . . . . . . .
      . . .
      . . .
      . . .

0I: - sets up the stack with 0, n, n (s, n, d)
^ - start of the loop )? - decrement d and test for 0. 0 exits loop
%? - mod against n and test. 0 causes ;rrw+s;rU which rotates s to top and adds d, rotates s to bottom and rejoins loop
;< - Cleanup and rejoin the loop.
On exiting loop
;< - Remove d from stack and redirect
-? - Remove n from s and test, 0 LOU@ turns left, outputs and exits, negatives 0O@ push zero, output and exits. positives ;O remove difference and outputs n. The path then goes through to the left turn which redirects to the @ exit

MickyT

Posted 2017-02-21T01:01:38.620

Reputation: 11 735

2

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!

ATaco

Posted 2017-02-21T01:01:38.620

Reputation: 7 898

2

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.)

Neil

Posted 2017-02-21T01:01:38.620

Reputation: 95 035

1"(%1%%%%j)" oh, batch :) – Bryan Boettcher – 2017-02-21T23:44:52.793

2

Perl 6, 72 24 bytes

{$_ <sum grep $_%%*,^$_}
  • 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.

Thanks to @b2gills.

Ven

Posted 2017-02-21T01:01:38.620

Reputation: 3 382

Every occurrence of $^a after the first one can be shortened to just $a. but it is even shorter if you write it as {$_ <sum grep $_%%*,^$_} Also looking at an earlier version, [+](LIST) works (no spaces) – Brad Gilbert b2gills – 2017-02-21T20:35:15.407

@BradGilbertb2gills Thanks! :) – Ven – 2017-02-21T22:22:19.810

2

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

Ven

Posted 2017-02-21T01:01:38.620

Reputation: 3 382

Not defining a function seems to be shorter: >sPf!%QTS – FryAmTheEggman – 2017-02-21T15:42:54.683

2

J, 19 bytes

Thanks to Conor O'Brien for cutting it to 19 bytes!

<[:+/i.#~i.e.]%2+i.

Previous: (34 bytes)

f=:3 :'(+/((i.y)e.y%2+i.y)#i.y)>y'

Returns 1 if it's abundant and 0 if it's not.

Output:

   f 3
0
   f 12
1
   f 11
0
   f 20
1

Blocks

Posted 2017-02-21T01:01:38.620

Reputation: 141

Welcome to PPCG! We allow anonymous functions, so you can remove the leading f=: as part of your byte count. Also, you can get down to 19 by converting to a tacit verb: <[:+/i.#~i.e.]%2+i. – Conor O'Brien – 2017-02-22T01:55:43.953

Thanks for the advice! However, could you please explain the cap verb ([:) and the switch verb (~). I don't really get what they're supposed to do in this tacit verb. – Blocks – 2017-02-22T11:06:37.303

~ switches so it's i.e.#i. but what is the purpose of [: ? – Blocks – 2017-02-22T11:09:03.713

so you know about forks, right? (f g h) y' is the same as(f y) g (h y). Whenfis a cap,([: g h) yis roughly the same asg h y. As for~, this switches the left and right arguments. It is important to know that~is not a verb but is actually an adverb. It modifies a verb. For example, we could have something like2 %~ 8. Here,~modifies%to switch its arguments, so the expression is equivalent to8 % 2`. – Conor O'Brien – 2017-02-22T12:27:29.963

In the fork chain, #~ is evaluated after the verbs to its right are executed, so it's left argument becomes the result on the right – Conor O'Brien – 2017-02-22T12:28:49.480

Thanks for so much for the explanation. It makes more sense now. – Blocks – 2017-02-22T13:49:21.457

My pleasure! If you have any more questions, ping me in the nineteenth byte with @ConorO'Brien

– Conor O'Brien – 2017-02-22T13:56:58.640

2

Japt, 4 bytes

<âÔx

Try it online!

Oliver

Posted 2017-02-21T01:01:38.620

Reputation: 7 160

2

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.

Giuseppe

Posted 2017-02-21T01:01:38.620

Reputation: 21 077

2

k, 19 16 15 bytes

{x<+/&~(!x)!'x}

Returns 1 for true, and 0 for false.

Try it online!

{             } /function(x)
       (!x)     /{0, 1, ..., x-1}
            '   /for each n in {0, 1, ..., x-1}:
           ! x  /    do (x mod n)
      ~         /for each, turn 0 -> 1, * -> 0 (map not)
     &          /get indices of 1's
   +/           /sum (fold add)
 x<             /check if x < the sum

zgrep

Posted 2017-02-21T01:01:38.620

Reputation: 1 291

2

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!

dana

Posted 2017-02-21T01:01:38.620

Reputation: 2 541

2

PowerShell, 43 bytes

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

Try it online!

mazzy

Posted 2017-02-21T01:01:38.620

Reputation: 4 832

2

Common Lisp, 63 bytes

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

Try it online!

Renzo

Posted 2017-02-21T01:01:38.620

Reputation: 2 260

2

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.

Ciaran_McCarthy

Posted 2017-02-21T01:01:38.620

Reputation: 689

2

APL (Dyalog Unicode), 11 bytesSBCS

-2 bytes thanks to @Adám

⊢<1⊥∘⍸0=⍳|⊢

Try it online!

Explanation:

⊢<1⊥∘⍸0=⍳|⊢ ⍝ Monadic function train
        ⍳   ⍝ Generate integers from 1 to input
        |⊢ ⍝ For each of the above, modulo the input    
      0=   ⍝ Return a boolean vector where ones correspond
           ⍝ to zeroes
     ⍸      ⍝ Return the indices of the ones in the above vector
           ⍝ This results in a vector of the proper divisors of our input
  1⊥∘      ⍝ Composed with the above, decode into unary base.
           ⍝ This has the same effect as summing the vector, but is used
           ⍝ here to comply with the function train format.
⊢<         ⍝ Is the above sum greater than our input?

voidhawk

Posted 2017-02-21T01:01:38.620

Reputation: 1 796

-2 with ⊢<1⊥∘⍸0=⍳|⊢ as per this tip.

– Adám – 2019-02-06T21:20:32.040

1

><>, 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

Sok

Posted 2017-02-21T01:01:38.620

Reputation: 5 592

1

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));

Tom

Posted 2017-02-21T01:01:38.620

Reputation: 3 078

45 bytes – Shaggy – 2017-09-12T15:51:15.413

1

Ruby, 39 32 bytes

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

Try it online!

Updated to Ruby 2.6

G B

Posted 2017-02-21T01:01:38.620

Reputation: 11 099

1

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.

Laikoni

Posted 2017-02-21T01:01:38.620

Reputation: 23 676

1

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

Sok

Posted 2017-02-21T01:01:38.620

Reputation: 5 592

1

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!

Destroigo

Posted 2017-02-21T01:01:38.620

Reputation: 401

153 – ASCII-only – 2019-03-13T05:56:07.473

1

PHP, 63 bytes

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

Usage: a(12);

Samuel Cook

Posted 2017-02-21T01:01:38.620

Reputation: 191

1

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 

RosLuP

Posted 2017-02-21T01:01:38.620

Reputation: 3 036

1

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

reffu

Posted 2017-02-21T01:01:38.620

Reputation: 1 361

0

Java 7, 69 68 bytes

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

Explanation:

Object 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 Object 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;  

Kevin Cruijssen

Posted 2017-02-21T01:01:38.620

Reputation: 67 575

Sorry, I was looking at the main code in that part but the golfed code above. Reading failure. – None – 2017-02-22T02:45:28.577

I am most likely overlooking something obvious, though I am curious: Why did you choose to use Object as opposed to bool? – Jonathan Frech – 2019-01-22T23:14:54.543

@JonathanFrech In Java it's boolean, so it's 1 byte longer than Object :) Although nowadays I would just use a Java 8+ lambda, so Object c(int n) would be n->. – Kevin Cruijssen – 2019-01-23T07:46:40.083

0

Scala, 39 bytes

(n:Int)=>(1 until n filter{n%_<1}sum)>n

jaxad0127

Posted 2017-02-21T01:01:38.620

Reputation: 281

0

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

Michael Kunst

Posted 2017-02-21T01:01:38.620

Reputation: 513

0

C, 68 bytes

int s,x;main(i){scanf("%d",&x);while(++i<x)s+=x%i^0?0:i;return s>x;}

shadowplay86

Posted 2017-02-21T01:01:38.620

Reputation: 1

3Welcome to the site! I think you could save some bytes by taking input from function arguments, rather than implementing main with scanf. For example int f(int s,int x){while(++i<x)s+=x%i^0?0:i;return s>x;} Also, I'm not an expert at C golfing, but I think some compilers default to int functions with int arguments, so you could do f(s,x){while(++i<x)s+=x%i^0?0:i;return s>x;}, but you'd have to find the right compiler and test it first. – James – 2017-02-21T17:59:35.360

0

Racket, 46 bytes

That's 46 bytes including the (require math).

(λ(n)(>(-(sum(divisors n))n)n))

The function produces booleans.

Try it online!

It's pretty simple, uses the divisors function to produce a list of all divisors of n. Since the list includes n itself, we need to remove it after summing.

waf9000

Posted 2017-02-21T01:01:38.620

Reputation: 21

Hello and welcome to PPCG. Nice first post; however, I would think that you ought to include the byte count for your (require math) import. – Jonathan Frech – 2019-01-23T21:45:25.773

Sorry! I was unsure about what exactly to add to the byte count. – waf9000 – 2019-01-23T22:20:08.010

No problem. Usually, one counts everything the function or program truly needs -- excluding boiler code only written for testing. However, on edge cases one sometimes has to resort to the current consensus -- written and talked about on meta. In practice, you will often be notified by comment if someone disagrees with your byte count.

– Jonathan Frech – 2019-01-23T22:25:48.060

0

8086 Assembly edited from 179016, 21 bytes

f:      mov cx, ax
        mov bx, ax
        dec cx
.a:     xor dx, dx
        push ax
        div cx
        pop ax
        test dx, dx
        jnz .b
        sub bx, cx
        jc .c
.b:     loop .a
.c:    

Saved from the reverse working

l4m2

Posted 2017-02-21T01:01:38.620

Reputation: 5 985

0

Java 8, 66 bytes

n->java.util.stream.IntStream.range(1,n).filter(i->n%i==0).sum()>n

Try it online!

Explanation

n->                                       // define a lambda function
    java.util.stream.IntStream.range(1,n) // create a stream of integers from 1 to n
    .filter(i->n%i==0)                    // filter out non-factors
    .sum()                                // sum the factors
>n                                        // compare the sum to the original number n
                                          // implicit return

Edit: Looks like someone already did this 2 years ago and got 1 byte shorter

Benjamin Urquhart

Posted 2017-02-21T01:01:38.620

Reputation: 1 262

0

Javascript,72bytes

var n=prompt(),div=0,i=1;while(++i<=n/2){div+=n%i?0:i}console.log(div>n)

Jeyanth

Posted 2017-02-21T01:01:38.620

Reputation: 133

As @SriotchilismO'Zaic points out, this appears to be a snippet which is not compliant with our current I/O consensus. Please fix your post accordingly. – Jonathan Frech – 2019-03-13T08:32:27.060

As @JonathanFrech specified, modified the changes and further refined the code. – Jeyanth – 2019-03-13T09:58:03.990

0

TI-BASIC (TI-84), 23 bytes

:2Ans<sum(seq(Ans/Xnot(remainder(Ans,X)),X,1,Ans,1

This answer is very similar to my answer here.
Input is in Ans.
Output is in Ans and is automatically printed out when the program completes.

Explanation:
(TI-BASIC doesn't have comments, so just assume that ; makes a comment)

:2Ans=sum(seq(Ans/Xnot(remainder(Ans,X)),X,1,Ans    ;Full program

 2Ans                                               ;double the input
          seq(                                      ;generate a list
                                         X,          ;using the variable X,
                                           1,        ;starting at 1,
                                             Ans     ;and ending at the input
                                                     ;with an implied increment of 1
              Ans/X                                 ;from the input divided by X
                   not(                ),           ;multiplied by the negated result of
                       remainder(Ans,X)              ;the input modulo X
                                                     ;(result: 0 or 1)
      sum(                                          ;sum up the elements in the list
     <                                              ;is the sum greater than double the input?

Example:

12
           12
prgmCDGF4
            1
6
            6
prgmCDGF4
            0

Note: The byte count of a program is evaluated using the value in [MEM]>[2]>[7] (36 bytes) then subtracting the length of the program's name, CDGF2, (5 bytes) and an extra 8 bytes used for storing the program:

36 - 5 - 8 = 23 bytes

Tau

Posted 2017-02-21T01:01:38.620

Reputation: 1 935

I counted 50 characters, so presumably 50 bytes... what am I missing? – Adam – 2019-03-13T14:33:28.673

@Adam all of the functions in TI-BASIC are 1-byte tokens. Therefore, sum(, seq(, remainder(, and not( would all be considered as 1 byte each. Ans is also a 1-byte token. – Tau – 2019-03-13T14:40:11.613

Ah, okay. I see now – Adam – 2019-03-13T14:42:57.347

Actually, I should clarify that the remainder( function is a 2-byte token, not 1-byte. – Tau – 2019-03-13T16:23:41.097

0

Kotlin, 37 bytes

{i->(2..i-1).filter{i%it==0}.sum()>i}

Expanded:

{ i -> (2..i - 1).filter { i % it == 0 }.sum() > i }

Explanation:

  {i->                      start lambda with parameter i
    (2..i-1)                create a range from 2 until i - 1 (potential divisors)
      .filter{i%it==0}      it's a divisor if remainder = 0; filter divisors
        .sum()              add all divisors
          >i                compare to the original number

Try it online!

Adam

Posted 2017-02-21T01:01:38.620

Reputation: 101

0

TI-BASIC, 21 bytes

seq(I,I,1,A-1
A<sum(Ansnot(fpart(A/Ans

TI-BASIC usually has a builtin for this kind of thing, but nothing fits that well here. Takes input from the A variable and outputs 1 or 0 if the number is or isn't abundant.

seq( generates a list of numbers from a function. Here it executes the function I on the I variable from one to one less than the input. Then working backward from the next line, we divide our input by the list we generated (stored in Ans) and take the fractional part so we have a new list of remainders for each potential divisor. We then build a list of booleans by taking not( on the remainder list and multiply that by the original list in Ans implicitly to get a list of proper divisors. Then we just sum the list of proper divisors and compare that to the input.

This was 24 bytes, golfed down to 21 using not( instead of *(0=

TiKevin83

Posted 2017-02-21T01:01:38.620

Reputation: 121

0

tinylisp, 78 bytes

(load library
(q((N)(l N(sum(filter(partial divides?(q D)(list(q _)N))(range N

Try it online!

Start with a list of each D from 0 through N - 1; filter it for the values of D that exactly divide N. Sum the results. Iff N is less than that sum, N is abundant.

DLosc

Posted 2017-02-21T01:01:38.620

Reputation: 21 213