#Web designer
def factorial(x):
#-------------------------------------------------
#--- Code snippet from The Math Vault ---
#--- Calculate factorial (C) Arthur Smith 1999 ---
#-------------------------------------------------
result = str(1)
i = 1 #Thanks Adam
while i <= x:
#result = result * i #It's faster to use *=
#result = str(result * result + i)
#result = int(result *= i) #??????
result str(int(result) * i)
#result = int(str(result) * i)
i = i + 1
return result
print factorial(6)
class StandardMathematicsSystem(MathematicsSystem):
def __init__(self, ibase):
if ibase.getBase() != new (IntegralNumber, 2):
raise NotImplementedError
self.base = ibase.getBase()
def calculateFactorial(self, target):
result = new (IntegralNumber, 1)
i = new (IntegralNumber, 2)
while i <= target:
result = result * i
i = i + new (IntegralNumber, 1)
return result
print StandardMathematicsSystem.getInstance(new (InternalBase, new (IntegralNumber, 2))).calculateFactorial(new (IntegralNumber, 6))
>>161 >>> timeit.Timer("long(200) >> 1").timeit()
3.8013210296630859
>>> timeit.Timer("long(200) / 2").timeit()
4.186870813369751
in my book 1.1 times as fast isn't twice as fast.
Name:
Anonymous2009-03-13 7:32
>>162
Your ``benchmark'' is biased, since you do not only count the actual division/shift, but also the function call long. Since calling functions in Python is a costly operation, you'd naturally see suboptimal results.
Oh, look, the shift operator is now actually 3.5 times as fast as the division operator.
Name:
Anonymous2009-03-13 8:19
>>161-163
all those benchmarks are biased, because the division or shift is such a small part of the function: >>> timeit.Timer("factorial.factorial_div(10000)","import factorial").timeit(100)
13.16028904914856
>>> timeit.Timer("factorial.factorial_shift(10000)","import factorial").timeit(100)
13.174507141113281
Name:
Anonymous2009-03-13 8:44
>>164
Oh, we were actually talking about factorials? I didn't read the thread, so I didn't notice.
>>167 because the division or shift is such a small part of the function
Name:
Anonymous2009-03-13 19:31
>>157 >>157 >>160
Yes, I said bitwise operations would be faster for calculating the lg of an integer. Not simply that shifting is faster than dividing by two. Try actually thinking next time, you can find the lg in a constant number of shifts every time.
you can find the lg in a constant number of shifts every time.
no, you can't. you can't find the lg of an arbitrary-sized integer in a constant number of shifts.
>>171
there aren't any significant architectures which store integers as arbitrary-size.
Name:
Anonymous2009-03-14 5:00
>>171
Very convincing counter argument, good job finding a technological niche for which the described solution will not work. I'm sure your boss will be sure to give you a raise on monday when you recommend your company switch to your new amazing arbitrary-size integer architecture.
>>172-173
post your code that can find the binary logarithm of any long in python in a constant number of shifts.
that's the only way you're going to convince anyone that it's possible.
Name:
Anonymous2009-03-14 6:35
SEXHORSE
Name:
Anonymous2009-03-14 10:07
>>174
What are you retarded? Longs in python ARE arbitrary length- which was exactly what the quoted posts said it would NOT work for. Lets move past this however and discuss practicality. Who in their right mind that is writing a factorial function would make it accept a long parameter, just incase somebody wants to calculate the factorial of 2^31? Hell, I'll tell you what else. If someone, for a practical reason needed to calculate the factorial of a number greater than or equal to 2^31, they would most certainly NOT be using python. Get off your theoretical python programming high horse and actually think about how something would be implemented before you ask for an actual implementation.
Python programmers do not evolve. They will remain shit-eaters for all eternity, hoping that their species one day dies off so the world can have their peace of them.
Name:
Anonymous2009-03-21 19:47
>>184
LOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOLOL so clever! IHBT