Click here to Skip to main content
       

Algorithms

 
GeneralRe: Factorials PinmemberIan Uy25-Jun-08 6:32 
GeneralRe: Factorials PinmemberTim Craig25-Jun-08 8:55 
GeneralRe: Factorials PinmvpLuc Pattyn25-Jun-08 11:01 
GeneralRe: Factorials PinmemberTim Craig25-Jun-08 21:06 
AnswerRe: Factorials Pinmembercp987624-Jun-08 12:33 
AnswerRe: Factorials PinmemberMatthew Butler24-Jun-08 12:38 
GeneralRe: Factorials Pinmembercp987624-Jun-08 14:05 
GeneralRe: Factorials PinmvpLuc Pattyn24-Jun-08 15:02 
GeneralRe: Factorials Pinmembercp987624-Jun-08 20:09 
GeneralRe: Factorials PinmvpLuc Pattyn24-Jun-08 23:53 
GeneralRe: Factorials Pinmembercp987625-Jun-08 2:27 
GeneralRe: Factorials PinmvpLuc Pattyn25-Jun-08 2:34 
GeneralRe: Factorials Pinmembercp987625-Jun-08 2:40 
AnswerRe: Factorials Pinmembertim_one14-Jul-08 8:37 
cp9876 wrote:
...

There is a little known fact (don't ask me how I know it!) that
 
if n = 5q+r with 0 <= r < 5
 
then n! has the same rightmost non-zero digit as 2^n x q! x r!


That should be 2^q rather than 2^n, and then it's valid for all n > 4. Removing recursion, here's one way to write it (in Python):
def f(n):
    result = 1
    while n > 1:
        n, r = divmod(n, 5)
        result = result * (6, 2, 4, 8)[n & 3] * (1, 1, 2, 6, 4)[r] % 10
    return result

and that can be simplified a bit by using a larger table:

FACTAB = 6, 6, 2, 6, 4, 2, 2, 4, 2, 8, 4, 4, 8, 4, 6, 8, 8, 6, 8, 2
 
def f(n):
    result = 1
    while n > 1:
        result = result * FACTAB[n % 20] % 10
        n //= 5
    return result


AnswerRe: Factorials PinmemberArash Partow25-Jun-08 2:14 
GeneralRe: Factorials PinmemberIan Uy25-Jun-08 6:26 
QuestionHow to determine the next x,y coordinate for a tank in a 2-D game... PinmemberEdmundisme18-Jun-08 8:35 
AnswerRe: How to determine the next x,y coordinate for a tank in a 2-D game... Pinmember73Zeppelin18-Jun-08 11:38 
GeneralRe: How to determine the next x,y coordinate for a tank in a 2-D game... PinmemberMatthew Butler18-Jun-08 11:44 
GeneralRe: How to determine the next x,y coordinate for a tank in a 2-D game... Pinmember73Zeppelin18-Jun-08 22:00 
GeneralRe: How to determine the next x,y coordinate for a tank in a 2-D game... PinmemberEdmundisme18-Jun-08 12:00 
AnswerRe: How to determine the next x,y coordinate for a tank in a 2-D game... PinmemberMatthew Butler18-Jun-08 11:41 
GeneralRe: How to determine the next x,y coordinate for a tank in a 2-D game... PinmemberEdmundisme18-Jun-08 12:01 
JokeRe: How to determine the next x,y coordinate for a tank in a 2-D game... PinmvpCPallini18-Jun-08 21:19 
QuestionPolyline offset algorithm Pinmemberbeko16-Jun-08 21:59 
AnswerRe: Polyline offset algorithm PinmemberAlan Balkany17-Jun-08 4:07 
GeneralRe: Polyline offset algorithm Pinmemberbeko17-Jun-08 4:43 
QuestionRe: Polyline offset algorithm Pinmvp CPallini17-Jun-08 7:03 
AnswerRe: Polyline offset algorithm Pinmemberbeko17-Jun-08 8:26 
GeneralRe: Polyline offset algorithm Pinmembercp987617-Jun-08 12:10 
AnswerRe: Polyline offset algorithm PinmemberArash Partow18-Jun-08 1:22 
GeneralRe: Polyline offset algorithm Pinmemberbeko18-Jun-08 19:55 
QuestionWhat's the period of (cos ax)(sin bx)? Pinmembersherifffruitfly14-Jun-08 10:00 
AnswerRe: What's the period of (cos ax)(sin bx)? Pinmembercp987614-Jun-08 19:03 
AnswerRe: What's the period of (cos ax)(sin bx)? PinmemberMarkBrock15-Jun-08 2:00 
AnswerRe: What's the period of (cos ax)(sin bx)? Pinmvp CPallini15-Jun-08 22:02 
QuestionQuaternion to Euler Pinmember Xmen 11-Jun-08 3:08 
AnswerRe: Quaternion to Euler Pinmembercmk11-Jun-08 11:27 
GeneralRe: Quaternion to Euler Pinmember Xmen 11-Jun-08 12:48 
AnswerRe: Quaternion to Euler PinmemberMark Churchill12-Jun-08 18:38 
QuestionGraphical Object drawing algorithms PinmemberMonty29-Jun-08 2:59 
AnswerRe: Graphical Object drawing algorithms Pinmember 73Zeppelin9-Jun-08 3:11 
GeneralRe: Graphical Object drawing algorithms PinmemberMonty29-Jun-08 3:24 
GeneralRe: Graphical Object drawing algorithms Pinmember 73Zeppelin9-Jun-08 3:37 
GeneralRe: Graphical Object drawing algorithms PinmemberTim Craig9-Jun-08 17:37 
GeneralRe: Graphical Object drawing algorithms PinmemberMonty29-Jun-08 21:02 
GeneralRe: Graphical Object drawing algorithms PinmemberTim Craig9-Jun-08 21:15 
GeneralRe: Graphical Object drawing algorithms PinmemberMonty29-Jun-08 22:17 
QuestionWhat's the most popular algorithm for video compression in industry? Pinmemberfollowait7-Jun-08 16:55 
AnswerRe: What's the most popular algorithm for video compression in industry? Pinmember 73Zeppelin8-Jun-08 8:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 22 Sep 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid