#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))
Name:
Anonymous2007-09-26 3:47 ID:UgzNM2mX
>>120
first enter /prog/ -> realize how retarded lispfags are -> realize how shit lisp is -> move on to real languages and out of /prog/
Name:
Anonymous2007-09-26 3:55 ID:J2CzvopX
first enter /prog/ -> try to troll lispfags and/or say SICP sucks -> fail -> move out of /prog/
HI, I'M PAUL GRAHAM, FOUNDER AND CEO OF Y COMBINATOR. I HAVE MADE MANY INSIGHTFUL STATEMENTS IN MY ESSAY ENTITLED "YOUR MOTHER AND THE 48 VERY SMART PEOPLE" AND WOULD LIKE TO THANK TREVOR BLACKWELL, ROBERT MORRIS AND JESSICA LIVINGSTON FOR HAVING READ DRAFTS OF THIS ESSAY, WHICH YOU WILL BE ABLE TO FIND WITH 21 OTHERS ESSAYS IN MY UPCOMING BOOK "HACKERS AND YOUR SISTER". I GUARANTEE IT.
Name:
Anonymous2008-04-19 20:32
WRITING POINTLESS CODE IS POINTLESS I GET IT HA HA HA
>>148
you can't write efficient algorithms in python. guido, like most people, associates efficient algorithms with functional programming, and we all know how he feels about that...
Name:
Anonymous2009-03-13 1:08
>>149
Funny that, because when I said expert programmer; I wasn't referring to the imaginary one that still uses toy languages.
Name:
Anonymous2009-03-13 3:54
>>148-149
here's split recursive: def factorial(n):
if not isinstance(n, (int, long)): raise TypeError("expected %s but got %s" % ((int, long), type(n)))
if n < 0: raise ValueError("%d is less than 0" % n)
if n < 2: return 1
global N
p = r = N = high = 1
h = shift = 0
def floorlog2(n):
a = 0
while n > 1:
n /= 2
a += 1
return a
log2n = floorlog2(n)
def product(n):
global N
m = n / 2
if m == 0:
N += 2
return N
if n == 2:
N += 4
return N * (N - 2)
return product(n - m) * product(m)
while h != n:
shift += h
h = n >> log2n
log2n -= 1
len = high
high = (h - 1) | 1
len = (high - len) / 2
if len > 0:
p *= product(len)
r *= p
return r << shift
>>150
split recursive in python is still way too slow to be useful.
prime swing in haskell is fast enough to be useful, and takes about as much time to write as split recursive in python.
prime swing in python might be fast enough to be useful, but i'm not going to write that unless someone pays me at least 10 XAG to do it.
Name:
Anonymous2009-03-13 4:50
def floorlog2(n):
a = 0
while n > 1:
n /= 2
a += 1
return a
What, are you trying to make this slower than a regular one line recursive facts?
Name:
Anonymous2009-03-13 5:24
>>152
you think you can do it faster? where is your code?
Name:
Anonymous2009-03-13 5:46
>>153
I'm not going to write it because I'll be honest and say I'm not expert at python and I will get laughed at. I'll give you a protip however, integers are STORED in a base 2 representation that easily lends itself to describing the lg of its decimal representation, actually going and calculating this is completely redundant and will incur an O(lg(n)) time cost which is infact the same as a regular facts.
Name:
Anonymous2009-03-13 5:56
>>152
none of the recursive ones can even do factorial(10000): RuntimeError: maximum recursion depth exceeded