Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Challenges

Are they abundant, deficient or perfect?

+5
−0

Abundant numbers are numbers which are less than their proper divisor sum. For example $18$ is abundant as $1 + 2 + 3 + 6 + 9 = 21 > 18$

Deficient numbers are numbers which are greater than their proper divisor sum. For example, $15$ is deficient as $1 + 3 + 5 = 9 < 15$

Perfect numbers are numbers wich are equal to their proper divisor sum. For example, $6$ is perfect as $1 + 2 + 3 = 6$.

You should take a positive integer $x \ge 12$ and output three lists. The lists should contain, in any order, the abundant numbers less than or equal to $x$, the deficient numbers less than or equal to $x$ and the perfect numbers less than or equal to $x$.

For example, if $x = 15$, the output could look like

[[12], [6], [1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15]]

This is code golf, so the shortest code in bytes wins

Test cases

49 -> [[12, 18, 20, 24, 30, 36, 40, 42, 48], [6, 28], [1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 19, 21, 22, 23, 25, 26, 27, 29, 31, 32, 33, 34, 35, 37, 38, 39, 41, 43, 44, 45, 46, 47, 49]]
32 -> [[12, 18, 20, 24, 30], [6, 28], [1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 19, 21, 22, 23, 25, 26, 27, 29, 31, 32]]
16 -> [[12], [6], [1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16]]
29 -> [[12, 18, 20, 24], [6, 28], [1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 19, 21, 22, 23, 25, 26, 27, 29]]
23 -> [[12, 18, 20], [6], [1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16, 17, 19, 21, 22, 23]]
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

General comments (6 comments)

8 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+4
−0

APL (Dyalog Unicode), 21 bytes

{×⍵-+/∪⍵∨¯1↓⍳⍵}¨∘⍳⊢⌸⍳

Try it online!

Returns a matrix (padded with zeroes) where the first row is deficient numbers, the second is perfect numbers, and the third is abundant numbers.

{×⍵-+/∪⍵∨¯1↓⍳⍵}¨∘⍳⊢⌸⍳
                  ⍳    ⍝ Make a range [1, input]
                ¨     ⍝ For every ⍵ in that range
             ⍳⍵       ⍝ Make a range [1, ⍵]
          ¯1↓         ⍝ Drop the last number (⍵)
       ⍵∨            ⍝ GCD all numbers in that range with ⍵
      ∪               ⍝ The previous step gave proper divisors, this removes duplicates
    +/                ⍝ Sum proper divisors
  ⍵-                  ⍝ Subtract from ⍵
 ×                    ⍝ Get the sign
                     ⍳ ⍝ Make another range [1, input]
                   ⌸  ⍝ Group by the signs of the differences we got earlier
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+5
−0

Husk, 10 bytes

kSo±-ȯΣhḊḣ

Try it online!

keyon is very nice here, but it's still a bit too long, sadly.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+4
−0

Vyxal, 30 28 25 bytes

Saved 1 4 bytes thanks to Aaron Miller

ƛ∆K-±";£kɽƛ¥D„£_'t¥=;vṪ,£

Try it Online!

A rather pitiful answer, but hey, it works. Gives deficient numbers, then perfect numbers, then abundant numbers.

ƛD∆K-±$";£
ƛ       ;  #For every number in the range [1,n]
 D         #Triplicate it
  ∆K       #Sum its proper divisors
    -      #Subtract the original number from that
     ±     #Get the sign of that (-1, 0, or 1)
      $"   #Make a 2-element list [sign, orig_num]
           #Sort it
         £ #Store it in the register

For an input of 12, this gives ⟨⟨1|1⟩|⟨1|2⟩|⟨1|3⟩|⟨1|4⟩|⟨1|5⟩|⟨0|6⟩|⟨1|7⟩|⟨1|8⟩|⟨1|9⟩|⟨1|10⟩|⟨1|11⟩|⟨-1|12⟩⟩ (0 means perfect, -1 means deficient, 1 means abundant). The explanation's old.

kɽƛ¥D„£_'h¥=;ƛḢ;,£
kɽ                 #Push [-1, 0, 1]
  ƛ                #For every number in that range
   ¥D              #Make three copies of the register (the list above)
     „             #Rotate stack so -1, 0, or 1 is on top
      £_           #Store to register temporarily
        '   ;      #Filter the list from above
         h         #Take the head of each
          ¥=       #Keep if it equals the current element from [-1,0,1]
             ƛḢ;   #Drop the sign from each
                ,  #Print this list of numbers
                 £ #Store original list to register again
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+4
−0

Vyxal o, 16 bytes

'∆K=;,'∆K<;,'∆K>

Try it Online!

Checks all numbers to see if they are perfect, then prints the ones that are, then does the same for abundant and deficient numbers.

Explanation:

                  # Implicit input 'n'
'   ;,            # Print any numbers in [1..n] for which the following is true:
 ∆K=              #   The number = the sum of its proper divisors
      '   ;,      # Print any numbers in [1..n] for which the following is true:
       ∆K<        #   The number < the sum of its proper divisors
            '     # Push a list of numbers in [1..n] for which the following is true:
             ∆K>  #   The number > the sum of its proper divisors
                  # 'o' flag - Implicit output regardless of having printed
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+3
−0

Jelly, 6 bytes

_ÆṣṠ)Ġ

Try it online!

In order of abundant, perfect, deficient.

_ÆṣṠ)Ġ  Main Link
    )   For each from 1 to N
_       Subtract
 Æṣ     The proper divisor sum
   Ṡ    Sign of the difference
     Ġ  Group equal elements' indices
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+3
−0

BQN, 21 bytesSBCS

(¬·×-+´·/0=↕⊸|)¨⊸⊔1+↕

Run online!

Mostly one big train used to find the appropriate group for each number, to be used with Group (). The combining functions in a train normally have two arguments, but Nothing (·) can be used on the left to not pass a left argument. See also Indices (/), and note that Modulus (|) is backwards relative to C-like %.

(¬·×-+´·/0=↕⊸|)¨⊸⊔1+↕
                  1+↕  # One plus range: 1…𝕩
(             )¨       # On each of these (n):
           ↕           #   Range 0…n-1
            ⊸|         #   Remainder dividing into n
         0=            #   Equals zero
       ·/              #   Indices where true: proper divisors
    -+´                #   Sum with initial value -n
  ·×                   #   Sign
 ¬                     #   One minus that
                ⊸⊔     # Use to group 1+↕

There might be a better solution based on taking the modulus on all pairs instead of one at a time. (¬·×-+˝(⊣×≠>|)⌜˜)⊸⊔1+↕ is close at 22 bytes.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+2
−0

Japt, 11 bytes

Could be 2 bytes shorter if not for a bug in Japt when trying to get the proper divisors of a number.

õ üÈgXnXâ x

Try it

õ üÈgXnXâ x     :Implicit input of integer U
õ               :Range [0,U]
  ü             :Group & sort
   È            :By passing each X through the following function
    g           :  Sign of difference of X and
     Xn         :    Subtract X from
       Xâ       :      All divisors of X
          x     :      Reduced by addition
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+1
−0

Python 3, 235 185 141 118 bytes

a=[[],[],[]]
n=1;exec("d=sum(i for i in range(1,n)if n%i<1)-n;a[[[1,2][d<0],0][d>0]]+=n,;n+=1;"*int(input()))
print(a)

Try it online!

Golfed 50 bytes by @caird coinheringaahing. Golfed 44 bytes by @bastolki. Golfed another 23 bytes by @bastolki.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

2 comment threads

[141 bytes](https://tio.run/##TY2xCsMgFEVn/QqXgI@4SLvUxvxIyaDGtA/aFzFm6NfbWCh0OdwDB256l8dKp1q9vU3X0OA... (2 comments)
General comments (1 comment)

Sign up to answer this question »