Re: [BangPypers] Implementing a fast factorial function in python

2009-09-15 Thread Vivek Khurana
On Mon, Sep 14, 2009 at 6:36 PM, Shashwat Anand wrote: > How do we calculate last 5-digits of 10**12 ignoring trailing zeros. The > code i wrote works good until 10**8 and after that take ages. > The source of problem is Project Euler : > http://projecteuler.net/index.php?section=problems&id=160 >

Re: [BangPypers] Implementing a fast factorial function in python

2009-09-14 Thread Sidharth Kuruvila
This should work, find f(6)**(10 raised to 7) On Mon, 14 Sep 2009 18:36:57 +0530, Shashwat Anand wrote: How do we calculate last 5-digits of 10**12 ignoring trailing zeros. The code i wrote works good until 10**8 and after that take ages. The source of problem is Project Euler : http://p

Re: [BangPypers] Implementing a fast factorial function in python

2009-09-14 Thread Shashwat Anand
@anand: There is a way to calculate number of leading zeros in n! Here i had implemented it : >>> x = 100 # number whose factorial is to be taken out >>> s = 0 #initial count of zeros >>> n = 1 >>> while x / 5**n: ... s += x / 5**n ... n +=1 ... >>> s 24 >>> import math >>> len(str(math.f

Re: [BangPypers] Implementing a fast factorial function in python

2009-09-14 Thread Shashwat Anand
@abhishek : Can you show me the code ? 10**12 is way too much. My code gives output within a minute for 10**8 and fails. Ofcourse I need to apply some algo here. What do you exactly mean by taking out rightmost 7 digits ? On Mon, Sep 14, 2009 at 8:14 PM, Abhishek Mishra wrote: > I remember doin

Re: [BangPypers] Implementing a fast factorial function in python

2009-09-14 Thread Abhishek Mishra
I remember doing something very inaccurate a long time ago for this - 1. find everything in a loop 2.everytime you encounter some zeros, strip them 3.everytime after stripping it exceeds say 7 digits, take out rightmost 7 digits for accuracy's sake 4. proceed till loop ends 5. print out the

Re: [BangPypers] Implementing a fast factorial function in python

2009-09-14 Thread Amit Saha
Shashwat Anand wrote: How do we calculate last 5-digits of 10**12 ignoring trailing zeros. The code i wrote works good until 10**8 and after that take ages. The source of problem is Project Euler : http://projecteuler.net/index.php?section=problems&id=160 You might consider using the Stirling's

Re: [BangPypers] Implementing a fast factorial function in python

2009-09-14 Thread Anand Chitipothu
> The key point i think is we do not need factorial but modulus 10**5 without > trailing zeros. Didn't helped though :( may be a better implementation could > help I think the right solution will NOT involve 10**12 computations. Think about it. ___ BangP

Re: [BangPypers] Implementing a fast factorial function in python

2009-09-14 Thread Shashwat Anand
The thought which comes to my mind is Modular exponentiation : for a ** b % r ( if a ** b very large ) I had coded it in C++ but in python the pow() have inbuilt modular-function made. so pow(a ,b, r) does the trick Here for factorial : F(n) = n * n-1 * n-2 ...* 2 * 1 Just like in earlier case wh

Re: [BangPypers] Implementing a fast factorial function in python

2009-09-14 Thread Navin Kabra
> print f(1) > > This is a HUGE number. I don't think you are expected to get the answer by direct calculation. I would guess that you are expected to use mathematical reasoning to figure out the answer (and not computation) ___ BangPypers ma

Re: [BangPypers] Implementing a fast factorial function in python

2009-09-14 Thread Anand Chitipothu
>  16 fac = int(str(fac * i).strip('0')) % 10 This is ugly hack. Can you think of something more mathematical? Anand ___ BangPypers mailing list BangPypers@python.org http://mail.python.org/mailman/listinfo/bangpypers

[BangPypers] Implementing a fast factorial function in python

2009-09-14 Thread Shashwat Anand
How do we calculate last 5-digits of 10**12 ignoring trailing zeros. The code i wrote works good until 10**8 and after that take ages. The source of problem is Project Euler : http://projecteuler.net/index.php?section=problems&id=160 The code is pasted here : http://paste.pocoo.org/show/139745/