Click here to Skip to main content
12,764,177 members (41,127 online)

Algorithms

 
GeneralRe: Factorials Pin
cp987624-Jun-08 15:05
membercp987624-Jun-08 15:05 
GeneralRe: Factorials Pin
Luc Pattyn24-Jun-08 16:02
mvpLuc Pattyn24-Jun-08 16:02 
GeneralRe: Factorials Pin
cp987624-Jun-08 21:09
membercp987624-Jun-08 21:09 
GeneralRe: Factorials Pin
Luc Pattyn25-Jun-08 0:53
mvpLuc Pattyn25-Jun-08 0:53 
GeneralRe: Factorials Pin
cp987625-Jun-08 3:27
membercp987625-Jun-08 3:27 
GeneralRe: Factorials Pin
Luc Pattyn25-Jun-08 3:34
mvpLuc Pattyn25-Jun-08 3:34 
GeneralRe: Factorials Pin
cp987625-Jun-08 3:40
membercp987625-Jun-08 3:40 
AnswerRe: Factorials Pin
tim_one14-Jul-08 9:37
membertim_one14-Jul-08 9: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 Pin
Arash Partow25-Jun-08 3:14
memberArash Partow25-Jun-08 3:14 
GeneralRe: Factorials Pin
Ian Uy25-Jun-08 7:26
memberIan Uy25-Jun-08 7:26 
QuestionHow to determine the next x,y coordinate for a tank in a 2-D game... Pin
Edmundisme18-Jun-08 9:35
memberEdmundisme18-Jun-08 9:35 
AnswerRe: How to determine the next x,y coordinate for a tank in a 2-D game... Pin
73Zeppelin18-Jun-08 12:38
member73Zeppelin18-Jun-08 12:38 
GeneralRe: How to determine the next x,y coordinate for a tank in a 2-D game... Pin
Matthew Butler18-Jun-08 12:44
memberMatthew Butler18-Jun-08 12:44 
GeneralRe: How to determine the next x,y coordinate for a tank in a 2-D game... Pin
73Zeppelin18-Jun-08 23:00
member73Zeppelin18-Jun-08 23:00 
GeneralRe: How to determine the next x,y coordinate for a tank in a 2-D game... Pin
Edmundisme18-Jun-08 13:00
memberEdmundisme18-Jun-08 13:00 
AnswerRe: How to determine the next x,y coordinate for a tank in a 2-D game... Pin
Matthew Butler18-Jun-08 12:41
memberMatthew Butler18-Jun-08 12:41 
GeneralRe: How to determine the next x,y coordinate for a tank in a 2-D game... Pin
Edmundisme18-Jun-08 13:01
memberEdmundisme18-Jun-08 13:01 
JokeRe: How to determine the next x,y coordinate for a tank in a 2-D game... Pin
CPallini18-Jun-08 22:19
mvpCPallini18-Jun-08 22:19 
QuestionPolyline offset algorithm Pin
beko16-Jun-08 22:59
memberbeko16-Jun-08 22:59 
AnswerRe: Polyline offset algorithm Pin
Alan Balkany17-Jun-08 5:07
memberAlan Balkany17-Jun-08 5:07 
GeneralRe: Polyline offset algorithm Pin
beko17-Jun-08 5:43
memberbeko17-Jun-08 5:43 
QuestionRe: Polyline offset algorithm Pin
CPallini17-Jun-08 8:03
mvp CPallini17-Jun-08 8:03 
AnswerRe: Polyline offset algorithm Pin
beko17-Jun-08 9:26
memberbeko17-Jun-08 9:26 
GeneralRe: Polyline offset algorithm Pin
cp987617-Jun-08 13:10
membercp987617-Jun-08 13:10 
AnswerRe: Polyline offset algorithm Pin
Arash Partow18-Jun-08 2:22
memberArash Partow18-Jun-08 2:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    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.170217.1 | Last Updated 24 Feb 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid