Click here to Skip to main content
11,708,312 members (62,868 online)

Algorithms

 
QuestionRe: I know, Pin
CPallini3-Jul-08 0:11
mvpCPallini3-Jul-08 0:11 
AnswerRe: I know, Pin
sumit70343-Jul-08 0:28
membersumit70343-Jul-08 0:28 
GeneralRe: I know, Pin
cp98763-Jul-08 0:52
membercp98763-Jul-08 0:52 
GeneralRe: I know, Pin
CPallini3-Jul-08 0:56
mvpCPallini3-Jul-08 0:56 
GeneralRe: I know, Pin
sumit70343-Jul-08 1:36
membersumit70343-Jul-08 1:36 
GeneralRe: I know, Pin
Paul Conrad3-Jul-08 18:32
memberPaul Conrad3-Jul-08 18:32 
GeneralRe: I know, Pin
CPallini3-Jul-08 0:53
mvpCPallini3-Jul-08 0:53 
AnswerRe: NORMSDIST function Pin
73Zeppelin3-Jul-08 2:11
member73Zeppelin3-Jul-08 2:11 
QuestionRe: NORMSDIST function Pin
CPallini3-Jul-08 2:29
mvpCPallini3-Jul-08 2:29 
AnswerRe: NORMSDIST function Pin
73Zeppelin3-Jul-08 3:19
member73Zeppelin3-Jul-08 3:19 
GeneralRe: NORMSDIST function Pin
sumit70343-Jul-08 2:47
membersumit70343-Jul-08 2:47 
GeneralRe: NORMSDIST function Pin
Member 82494620-Dec-11 23:54
memberMember 82494620-Dec-11 23:54 
GeneralThe six rightmost non-zero digits of 1000000! Pin
Rod Gowdy2-Jul-08 4:50
memberRod Gowdy2-Jul-08 4:50 
GeneralRe: The six rightmost non-zero digits of 1000000! [modified] Pin
Robert.C.Cartaino2-Jul-08 6:33
memberRobert.C.Cartaino2-Jul-08 6:33 
The last six non-zero digits of 1,000,000! are 412544. Please, keep your money.

I wrote this code to calculate your answer:
// Calculate the least significant (non-zero) digits of large factorials.
// By Robert C. Cartaino
// Posted via http://www.codeproject.com
// 02-July-2008

using System;
 
class Program
{
    const ulong Target = 1000000;       // This is the number you are trying to find the factorial of.
    const ulong RoundOff = 10000000000; // This rounds off the answer so the intermediate results don't overflow.

    static void Main(string[] args)
    {
        ulong factorial = 1;
 
        // Iterate through all numbers up to 'Target', multiply by each to find the factorial of 'Target'.
        for (ulong n = 1; n <= Target; n++)
        {
            factorial *= n;
 
            // Remove the trailing zeros.
            while (factorial % 10 == 0)
            {
                factorial /= 10;
            }
 
            // We only need the right-most digits.
            factorial %= RoundOff;
 
            //Console.WriteLine("{0}! = {1}", n, factorial);
        }
 
        Console.WriteLine("{0}! (truncated) = {1}", Target, factorial);
    }
}

Why it works:

When multiplying numbers, the trailing zeros at the end will not change the outcome... so I threw those out.

Also, if you are only interested in the least significant digits, then the upper-most digits will not have an effect on the outcome of the lower digits... so I threw those out.

With all the rounding, I didn't have to worry about overflow so I was able to calculate the "truncated factorials" of very large numbers iteratively.

Enjoy,

Robert C. Cartaino

modified on Wednesday, July 2, 2008 1:26 PM

GeneralRe: The six rightmost non-zero digits of 1000000! Pin
Rod Gowdy3-Jul-08 3:42
memberRod Gowdy3-Jul-08 3:42 
GeneralRe: The six rightmost non-zero digits of 1000000! Pin
Robert.C.Cartaino3-Jul-08 10:06
memberRobert.C.Cartaino3-Jul-08 10:06 
QuestionSpecify numbers as product of Primes? Pin
Ian Uy25-Jun-08 19:37
memberIan Uy25-Jun-08 19:37 
AnswerRe: Specify numbers as product of Primes? Pin
cp987625-Jun-08 21:32
membercp987625-Jun-08 21:32 
GeneralRe: Specify numbers as product of Primes? Pin
Ian Uy26-Jun-08 0:42
memberIan Uy26-Jun-08 0:42 
AnswerRe: Specify numbers as product of Primes? Pin
Robert.C.Cartaino26-Jun-08 4:48
memberRobert.C.Cartaino26-Jun-08 4:48 
GeneralRe: Specify numbers as product of Primes? Pin
Ian Uy26-Jun-08 4:50
memberIan Uy26-Jun-08 4:50 
GeneralRe: Specify numbers as product of Primes? Pin
The Developer8-Aug-08 7:03
memberThe Developer8-Aug-08 7:03 
GeneralRe: Specify numbers as product of Primes? Pin
Ian Uy26-Jun-08 6:28
memberIan Uy26-Jun-08 6:28 
GeneralRe: Specify numbers as product of Primes? Pin
Paul Conrad28-Jun-08 5:44
memberPaul Conrad28-Jun-08 5:44 
GeneralRe: Specify numbers as product of Primes? Pin
Ian Uy28-Jun-08 5:46
memberIan Uy28-Jun-08 5:46 
GeneralRe: Specify numbers as product of Primes? Pin
Paul Conrad28-Jun-08 6:11
memberPaul Conrad28-Jun-08 6:11 
GeneralRe: Specify numbers as product of Primes? Pin
Ravi Bhavnani8-Aug-08 7:16
memberRavi Bhavnani8-Aug-08 7:16 
GeneralRe: Specify numbers as product of Primes? Pin
cp987628-Jun-08 20:27
membercp987628-Jun-08 20:27 
GeneralRe: Specify numbers as product of Primes? Pin
Luc Pattyn29-Jun-08 2:42
mvpLuc Pattyn29-Jun-08 2:42 
GeneralRe: Specify numbers as product of Primes? Pin
MarkBrock27-Jun-08 18:46
memberMarkBrock27-Jun-08 18:46 
GeneralRe: Specify numbers as product of Primes? Pin
Paul Conrad28-Jun-08 5:42
memberPaul Conrad28-Jun-08 5:42 
GeneralRe: Specify numbers as product of Primes? Pin
ChandraRam2-Jul-08 22:49
memberChandraRam2-Jul-08 22:49 
GeneralRe: Specify numbers as product of Primes? Pin
MarkBrock2-Jul-08 23:06
memberMarkBrock2-Jul-08 23:06 
AnswerRe: Specify numbers as product of Primes? Pin
Member 419459326-Jun-08 8:19
memberMember 419459326-Jun-08 8:19 
GeneralRe: Specify numbers as product of Primes? Pin
Robert.C.Cartaino26-Jun-08 8:47
memberRobert.C.Cartaino26-Jun-08 8:47 
GeneralRe: Specify numbers as product of Primes? Pin
Ian Uy26-Jun-08 17:51
memberIan Uy26-Jun-08 17:51 
GeneralRe: Specify numbers as product of Primes? Pin
cp987629-Jun-08 14:57
membercp987629-Jun-08 14:57 
AnswerRe: Specify numbers as product of Primes? Pin
Arash Partow29-Jun-08 0:48
memberArash Partow29-Jun-08 0:48 
GeneralRe: Specify numbers as product of Primes? [modified] Pin
cp987629-Jun-08 3:53
membercp987629-Jun-08 3:53 
GeneralRe: Specify numbers as product of Primes? Pin
Luc Pattyn29-Jun-08 4:40
mvpLuc Pattyn29-Jun-08 4:40 
GeneralRe: Specify numbers as product of Primes? Pin
cp987629-Jun-08 14:47
membercp987629-Jun-08 14:47 
GeneralRe: Specify numbers as product of Primes? Pin
Arash Partow30-Jun-08 2:54
memberArash Partow30-Jun-08 2:54 
GeneralRe: Specify numbers as product of Primes? Pin
cp987630-Jun-08 3:18
membercp987630-Jun-08 3:18 
GeneralRe: Specify numbers as product of Primes? Pin
Arash Partow30-Jun-08 4:27
memberArash Partow30-Jun-08 4:27 
GeneralRe: Specify numbers as product of Primes? Pin
Arash Partow30-Jun-08 2:17
memberArash Partow30-Jun-08 2:17 
Questionlogic of prob function in ms excel Pin
sumit703425-Jun-08 0:40
membersumit703425-Jun-08 0:40 
QuestionFactorials Pin
Ian Uy24-Jun-08 8:17
memberIan Uy24-Jun-08 8:17 
AnswerRe: Factorials Pin
Tim Craig24-Jun-08 10:29
memberTim Craig24-Jun-08 10:29 
GeneralRe: Factorials Pin
Ian Uy25-Jun-08 6:32
memberIan Uy25-Jun-08 6:32 
GeneralRe: Factorials Pin
Tim Craig25-Jun-08 8:55
memberTim Craig25-Jun-08 8:55 

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
Web04 | 2.8.150819.1 | Last Updated 31 Aug 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid