Proper divisors: Difference between revisions
→Python: The Simple Way: straightened up the sloppy code
m (→Faster Proper Divisor Counting: typo) |
(→Python: The Simple Way: straightened up the sloppy code) |
||
Line 4,644:
=== Python: The Simple Way ===
Not all the code submitters realized that it's a tie for the largest number of factors inside the limit. The task description clearly indicates only one answer is needed. But both numbers are provided for the curious. Also shown is the result for 25000, as there is no tie for that, just to show the program can handle either scenario.
<lang python>def pd(num):
factors = []
for
if num % divisor == 0: factors.append(divisor)
return factors
def pdc(num):
count = 0
for divisor in range(1,1+num//2):
if num % divisor == 0: count += 1
return count
def fmtres(title, lmt, best, bestc):
return "The " + title + " number up to and including " + str(lmt) + " with the highest number of proper divisors is " + str(best) + ", which has " + str(bestc)
def showcount(limit):
best, bestc, bh, bhc = 0, 0, 0, 0
for i in range(limit+1):
divc = pdc(i)
if divc > bestc: bestc, best = divc, i
if divc >= bhc: bhc, bh = divc, i
if best == bh:
print(fmtres("only", limit, best, bestc))
else:
print(fmtres("lowest", limit, best, bestc))
print(fmtres("highest", limit, bh, bhc))
print()
for i in range(1, lmt + 1):
divs = pd(i)
if len(divs) == 0:
print("There are no proper divisors of", i)
print(divs[0], "is the only proper divisor of", i)
else:
print(divs, "are the proper divisors of", i)
print()
showcount(20000)
showcount(25000)</lang>
{{out}}
<pre style="white-space: pre-wrap;">There are no proper divisors of 1
1 is the only proper divisor of 2
1 is the only proper divisor of 3
[1, 2] are the proper divisors of 4
1 is the only proper divisor of 5
[1, 2, 3] are the proper divisors of 6
1 is the only proper divisor of 7
[1, 2, 4] are the proper divisors of 8
[1, 3] are the proper divisors of 9
[1, 2, 5] are the proper divisors of 10
The lowest number up to and including 20000 with the highest number of proper divisors is 15120, which has 79
The highest number up to and including 20000 with the highest number of proper divisors is 18480, which has 79
The only number up to and including 25000 with the highest number of proper divisors is 20160, which has 83</pre>
=={{header|Quackery}}==
|