r/dailyprogrammer • u/Cosmologicon 2 3 • May 19 '23
[2023-05-19] Challenge #400 [Intermediate] Practical Numbers
Background
A practical number is a positive integer N such that all smaller positive integers can be represented as sums of distinct divisors of N. For example, 12 is a practical number because all the numbers from 1 to 11 can be expressed as sums of the divisors of 12, which are 1, 2, 3, 4, and 6. (Wikipedia.) However, 10 is not a practical number, because 4 and 9 cannot be expressed as a sum of 1, 2, and 5. For more detailed explanation and examples, see this recent Numberphile video.
Challenge
Write a function that returns whether a given positive integer is a practical number.
practical(1) => true
practical(2) => true
practical(3) => false
practical(10) => false
practical(12) => trueYou should be able to handle numbers up to 10,000 efficiently. The sum of all practical numbers up to 10,000 inclusive is 6,804,107. Test your code by verifying this value.
Optional bonus challenge
Consider the numbers X in the range 1 to 10,000 inclusive. The sum of all X such that 1019 + X is a practical number is 1,451,958. Find the sum of all X such that 1020 + X is a practical number. I found the section Characterization of practical numbers in the Wikipedia article useful here.
I do not have any plans to resume posting here regularly. I just saw the Numberphile video and thought it would make a good challenge.
40
u/Zorander42 May 19 '23
Nice, I missed these!
16
u/No_Row2307 Sep 02 '24
why is this subreddit inactive its kinda sad
11
u/cbarrick May 20 '23
Rust
A quick and dirty naive solution in 35 lines of Rust, without the bonus.
Subject to change if I decide to attempt the bonus tomorrow.
https://github.com/cbarrick/practice/blob/master/reddit/dailyprogrammer/400-med/practical.rs
16
u/skeeto -9 8 May 19 '23 edited May 19 '23
C, multi-threaded with the 1019 bonus. On my system it computes that 1019 result in ~4.6 seconds. I reused my old dailyprogrammer prime test, which saved some implementation time.
https://github.com/skeeto/scratch/blob/master/misc/practical.c
(The Wikipedia article really does have all the shortcuts, though as usual with math topics it's rather cryptic.)
4
u/scher17 Jul 19 '23
Here is my python solution. I think that any of the previous solutions use a set in order like this I've done here:
It takes about 5 seconds to complete on my old computer (i5-4210H):
3
u/MattieShoes May 19 '23
One might note in the video that it shows old balance scales. Using those sorts of scales, it opens up the possibility of subtraction too, by placing some weights on the opposite side of the scale. For instance, a
1and a3weight can weigh 2 just fine, by placing the3on one side and the1on the other.I'm not interested in designing puzzles, but there exist problems to figure out the minimal number of weights required to measure up to an arbitrary amount -- 40 is common.
1
u/[deleted] Feb 01 '25
Just did this in Prolog, you can play with the solution in the browser here
1
u/gabyjunior 1 2 May 21 '23 edited May 24 '23
Ruby
With bonus implementing the formula provided in Wikipedia page.
Takes 2 mins to compute the 1020 bonus, I believe it would be much quicker using an optimized factorization function instead of the naive trial division.
EDIT New version which skips more trial divisions, now takes less than 2 minutes for challenge + 1019 / 1020 bonuses.
Output
1
u/[deleted] May 22 '23
lets go!!!
1
u/Goof_Vince Jul 03 '23
Paython
def pratcical (num):
divs = [1] #fount it easier just to include 1 as it is an ood one in the sequance
for i in range (2,num):
if sum(divs) < (i-1):
return False
else:
if num%i==0:
divs.append(i)
else:
pass
if sum(divs) >= (num-1):
return True
sum_of_all = [1,2] #list starts with 1 and 2 (and range begins with 4) since my def does not account for them.
for num in range (4,10001):
if pratcical(num) == True:
sum_of_all.append(num)
print (sum(sum_of_all))
1
u/hiroshiSama Sep 30 '23
!remindme 24 hours
1
u/Deep-Cheek-8599 Feb 21 '24
I've been a professional software engineer in Australia for just over 7 years now and love learning about new things in tech.
Since starting my own company (which is still quite small) I don't get exposed to many of the random discussions with colleagues about random things in tech. As as result I wanted to find some way to just expose myself to random topics in computer science or software engineering.
Since the release of gtp4 voice I've been asking each day for it to tell me about one random topic in computer science or software engineering.
I've actually been finding it pretty useful (I was fairly sceptical it would be good).
I made it into a free daily email and I'm trying to get my first user!
I would love any feedback if you want to try it out - emails are sent once a day in the morning (UTC+10) at 8am.
https://onenewthing.net
0
u/[deleted] May 19 '23
[deleted] — view deleted comment
0
u/[deleted] May 20 '23
[deleted] — view deleted comment