Proper divisors: Difference between revisions

Content added Content deleted
(→‎Python: The Simple Way: straightened up the sloppy code)
Line 4,644: Line 4,644:


=== Python: The Simple Way ===
=== 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>import random
<lang python>def pd(num):
propdiv = []
factors = []
num = random.randint(1,200)
for n in range(1, num+1):
for divisor in range(1,1+num//2):
if num % divisor == 0: factors.append(divisor)
propdiv = []
return factors
for x in range(1,n+1):
n = int(n)
if n%x == 0:
propdiv.append(x)
propdiv.pop(len(propdiv)-1)
print(str(propdiv) + " are the Proper Divisors of " + str(num))


def pdc(num):
print("\n")
count = 0
for divisor in range(1,1+num//2):
if num % divisor == 0: count += 1
return count


def fmtres(title, lmt, best, bestc):
Ften = 10
return "The " + title + " number up to and including " + str(lmt) + " with the highest number of proper divisors is " + str(best) + ", which has " + str(bestc)
propdten = []
for toten in range(1,11):
for n1 in range(1, toten+1):
propdten = []
for x in range(1,n1+1):
n1 = int(n1)
if n1%x == 0:
propdten.append(x)
propdten.pop(len(propdten)-1)
print(str(propdten) + " are the Proper Divisors of " + str(toten))


def showcount(limit):
print("\n")
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()


findiv = 1
lmt = 10
for i in range(1, lmt + 1):
normdiv = []
divs = pd(i)
greatestdiv = []
if len(divs) == 0:
twentykrng = 200
print("There are no proper divisors of", i)
for nnn in range(1, twentykrng+1):
if len(normdiv) > len(greatestdiv):
elif len(divs) == 1:
print(divs[0], "is the only proper divisor of", i)
greatestdiv = []
else:
greatestdiv = normdiv
print(divs, "are the proper divisors of", i)
findiv = nnn - 1
print()
normdiv = []
showcount(20000)
for xx in range(1,nnn+1):
showcount(25000)</lang>
if nnn%xx == 0:
{{out}}
normdiv.append(xx)
<pre style="white-space: pre-wrap;">There are no proper divisors of 1
normdiv.pop(len(normdiv)-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
print(findiv)
The highest number up to and including 20000 with the highest number of proper divisors is 18480, which has 79
print(len(greatestdiv))</lang>


The only number up to and including 25000 with the highest number of proper divisors is 20160, which has 83</pre>
{{Out}}
<pre>[1, 2, 5, 10, 17, 34, 85] are the Proper Divisors of 170


[] are the Proper Divisors of 1
[1] are the Proper Divisors of 2
[1] are the Proper Divisors of 3
[1, 2] are the Proper Divisors of 4
[1] are the Proper Divisors of 5
[1, 2, 3] are the Proper Divisors of 6
[1] are the Proper Divisors 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


15120
79</pre>


=={{header|Quackery}}==
=={{header|Quackery}}==