 # Partition List line

This Python code is an perfect number that I wrote (an perfect number is a number whose sum divisible by it is equal to the number itself)
The first question is why that Partition List line does not work on the last line of code
The second question is how to write c # code with maximum execution speed?
perfect number
perfect number.gh (11.4 KB)

import rhinoscriptsyntax as rs
import ghpythonlib.components as gh

``````b=[]
t=[]
a=[]
w=[]
h=[]
for i in range(3,n):
for j in range(2,i+1,1):
if i%j==0:
b.append(i//j)
x.append(i//j)
c=sum(b)
if c==i:
t.append(c)
b=[]
print len(t)
for k in range(0,len(t),1):
v=t[k]
for j in range(2,v+1,1):
if v%j==0:
a.append(v//j)
h.append(v//j)
g=len(h)
w.append(g)
h=[]
m=gh.PartitionList(a,w)
``````

It wouldn’t hurt, explaining first what you’re trying to achieve!

Making code run faster, should first be a manner of code optimization, and not of jumping straight onto next “best” thing! Also if, you’re after “maximum code execution” speed, you should rewrite it in a fully compiled language, like C++.

``````limit = 101

for num in xrange(2, limit):
if all(num % i != 0 for i in xrange(2, num)):
num2 = (num * 2) - 1
for num2 in xrange(2, limit):
if all(num2 % i != 0 for i in xrange(2, num2)):
pnum = 2**(num - 1) * (2**num - 1)
print pnum, "is a perfect number"
break
break
``````

I reckon 12 ms for 25 perfect integer number is not too shabby! I’d say C# can go where the pepper grows! 1 Like

``````from ghpythonlib.parallel import run
def isPerfect(num):
if all(num % i != 0 for i in xrange(2, num)):
num2 = (num * 2) - 1
for num2 in xrange(2, limit):
if all(num2 % i != 0 for i in xrange(2, num2)):
pnum = 2**(num - 1) * (2**num - 1)
return pnum
return
a = filter(None, run(isPerfect, range(2, limit)))
``````

PerfectNumber.gh (5.5 KB)