Are they abundant, deficient or perfect?
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]]
[Husk], 10 bytes kSo±-ȯ …
3y ago
Vyxal `o`, 16 bytes …
3y ago
Vyxal, 30 28 25 bytes Saved …
3y ago
[APL (Dyalog Unicode)], 21 byt …
3y ago
BQN, 21 bytesSBCS ``` (¬·× …
3y ago
[Jelly], 6 bytes ÆṣṠ)Ġ …
3y ago
Japt, 11 bytes Could be 2 b …
3y ago
[Python 3], 235 185 141 118 by …
3y ago
8 answers
Husk, 10 bytes
kSo±-ȯΣhḊḣ
keyon
is very nice here, but it's still a bit too long, sadly.
0 comment threads
Vyxal o
, 16 bytes
'∆K=;,'∆K<;,'∆K>
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
0 comment threads
Vyxal, 30 28 25 bytes
Saved 1 4 bytes thanks to Aaron Miller
ƛ∆K-±";£kɽƛ¥D„£_'t¥=;vṪ,£
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
0 comment threads
APL (Dyalog Unicode), 21 bytes
{×⍵-+/∪⍵∨¯1↓⍳⍵}¨∘⍳⊢⌸⍳
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
0 comment threads
BQN, 21 bytesSBCS
(¬·×-+´·/0=↕⊸|)¨⊸⊔1+↕
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.
0 comment threads
Jelly, 6 bytes
_ÆṣṠ)Ġ
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
0 comment threads
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
õ üÈ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
0 comment threads
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)
Golfed 50 bytes by @caird coinheringaahing. Golfed 44 bytes by @bastolki. Golfed another 23 bytes by @bastolki.
1 comment thread