Why the end of factorial numbers have too many zero digits
I have an interesting question, it is not about programming, maybe it is about math.
This is simple code on Scala - factorial number
def smartFactorial(number: Int): BigInt = {
def factorial(number: Int, accumulator: BigInt) : BigInt = {
if (number <= 1) accumulator
else
factorial(number - 1, number * accumulator)
}
factorial(number, 1)
}
println(smartFactorial(1000))
So, when I run this code for the first time I got a result with many zero digits on the end. Firstly, I thought "it is a mistake", and tried to run this code with other parameters (also greater 1000) - results were the same. After several googling this question, I totally can't decide: Why the resulting number of factorial has too many zero digits on the end, and how we can explain it?
Also, I wrote a little script to calculate count of end-zero-digits on several input numbers
def countOfEndZeros(number: String): Int = {
def countOfEndZeros(number: String, index: Int, accumulator: Int) : Int = {
if (number.charAt(index) != '0') accumulator
else if (index - 1 < 0) accumulator
else countOfEndZeros(number.substring(0, index), index - 1, accumulator + 1)
}
countOfEndZeros(number, number.length - 1, 0)
}
val countExamples = 1000
val inputs: Stream[Int] = Stream.from(1)
inputs
.map(i => (i, factorial(i).toString()))
.map(t => (t._1, countOfEndZeros(t._2)))
.take(countExamples)
.foreach(println)
Interesting fact, if you run the last script you will see, that there is no number with 5 zero digits on the end, but all other digits are represented. I can't figure out it or explain it; maybe there are some math theorems that can explain it?
math numbers
add a comment |
I have an interesting question, it is not about programming, maybe it is about math.
This is simple code on Scala - factorial number
def smartFactorial(number: Int): BigInt = {
def factorial(number: Int, accumulator: BigInt) : BigInt = {
if (number <= 1) accumulator
else
factorial(number - 1, number * accumulator)
}
factorial(number, 1)
}
println(smartFactorial(1000))
So, when I run this code for the first time I got a result with many zero digits on the end. Firstly, I thought "it is a mistake", and tried to run this code with other parameters (also greater 1000) - results were the same. After several googling this question, I totally can't decide: Why the resulting number of factorial has too many zero digits on the end, and how we can explain it?
Also, I wrote a little script to calculate count of end-zero-digits on several input numbers
def countOfEndZeros(number: String): Int = {
def countOfEndZeros(number: String, index: Int, accumulator: Int) : Int = {
if (number.charAt(index) != '0') accumulator
else if (index - 1 < 0) accumulator
else countOfEndZeros(number.substring(0, index), index - 1, accumulator + 1)
}
countOfEndZeros(number, number.length - 1, 0)
}
val countExamples = 1000
val inputs: Stream[Int] = Stream.from(1)
inputs
.map(i => (i, factorial(i).toString()))
.map(t => (t._1, countOfEndZeros(t._2)))
.take(countExamples)
.foreach(println)
Interesting fact, if you run the last script you will see, that there is no number with 5 zero digits on the end, but all other digits are represented. I can't figure out it or explain it; maybe there are some math theorems that can explain it?
math numbers
There are about n factors 2 and n/4 factors 5 in n!, thus the same amount of around n/4 trailing zeros.
– LutzL
Jan 2 at 23:33
add a comment |
I have an interesting question, it is not about programming, maybe it is about math.
This is simple code on Scala - factorial number
def smartFactorial(number: Int): BigInt = {
def factorial(number: Int, accumulator: BigInt) : BigInt = {
if (number <= 1) accumulator
else
factorial(number - 1, number * accumulator)
}
factorial(number, 1)
}
println(smartFactorial(1000))
So, when I run this code for the first time I got a result with many zero digits on the end. Firstly, I thought "it is a mistake", and tried to run this code with other parameters (also greater 1000) - results were the same. After several googling this question, I totally can't decide: Why the resulting number of factorial has too many zero digits on the end, and how we can explain it?
Also, I wrote a little script to calculate count of end-zero-digits on several input numbers
def countOfEndZeros(number: String): Int = {
def countOfEndZeros(number: String, index: Int, accumulator: Int) : Int = {
if (number.charAt(index) != '0') accumulator
else if (index - 1 < 0) accumulator
else countOfEndZeros(number.substring(0, index), index - 1, accumulator + 1)
}
countOfEndZeros(number, number.length - 1, 0)
}
val countExamples = 1000
val inputs: Stream[Int] = Stream.from(1)
inputs
.map(i => (i, factorial(i).toString()))
.map(t => (t._1, countOfEndZeros(t._2)))
.take(countExamples)
.foreach(println)
Interesting fact, if you run the last script you will see, that there is no number with 5 zero digits on the end, but all other digits are represented. I can't figure out it or explain it; maybe there are some math theorems that can explain it?
math numbers
I have an interesting question, it is not about programming, maybe it is about math.
This is simple code on Scala - factorial number
def smartFactorial(number: Int): BigInt = {
def factorial(number: Int, accumulator: BigInt) : BigInt = {
if (number <= 1) accumulator
else
factorial(number - 1, number * accumulator)
}
factorial(number, 1)
}
println(smartFactorial(1000))
So, when I run this code for the first time I got a result with many zero digits on the end. Firstly, I thought "it is a mistake", and tried to run this code with other parameters (also greater 1000) - results were the same. After several googling this question, I totally can't decide: Why the resulting number of factorial has too many zero digits on the end, and how we can explain it?
Also, I wrote a little script to calculate count of end-zero-digits on several input numbers
def countOfEndZeros(number: String): Int = {
def countOfEndZeros(number: String, index: Int, accumulator: Int) : Int = {
if (number.charAt(index) != '0') accumulator
else if (index - 1 < 0) accumulator
else countOfEndZeros(number.substring(0, index), index - 1, accumulator + 1)
}
countOfEndZeros(number, number.length - 1, 0)
}
val countExamples = 1000
val inputs: Stream[Int] = Stream.from(1)
inputs
.map(i => (i, factorial(i).toString()))
.map(t => (t._1, countOfEndZeros(t._2)))
.take(countExamples)
.foreach(println)
Interesting fact, if you run the last script you will see, that there is no number with 5 zero digits on the end, but all other digits are represented. I can't figure out it or explain it; maybe there are some math theorems that can explain it?
math numbers
math numbers
asked Jan 2 at 20:11


Almaz MurzabekovAlmaz Murzabekov
31
31
There are about n factors 2 and n/4 factors 5 in n!, thus the same amount of around n/4 trailing zeros.
– LutzL
Jan 2 at 23:33
add a comment |
There are about n factors 2 and n/4 factors 5 in n!, thus the same amount of around n/4 trailing zeros.
– LutzL
Jan 2 at 23:33
There are about n factors 2 and n/4 factors 5 in n!, thus the same amount of around n/4 trailing zeros.
– LutzL
Jan 2 at 23:33
There are about n factors 2 and n/4 factors 5 in n!, thus the same amount of around n/4 trailing zeros.
– LutzL
Jan 2 at 23:33
add a comment |
2 Answers
2
active
oldest
votes
There are plenty of 2's available in any factorial, so every 5 that you have will add a trailing 0. However, multiples of higher powers of 5 (e.g. 25, 50, ...) give you more trailing 0's. This is what you noticed: 20! through 24! have 4 trailing 0's, but 25! has 6 trailing 0's, because 25 is 5*5, which adds two trailing 0's.
add a comment |
Large factorials have a lot of trailing zeroes for two main reasons.
They're composed of a lot of numbers that end in zero. Think of all the numbers like 10, 20, 30, etc. that you multiplied in to the factorial of 1000 to arrive at the answer. Each one of the adds a 0 (two zeroes for 100, 200, etc.), since any number multiplied by 10 will end in a 0.
They're also composed of a lot of numbers that are multiples of 2 or 5. Since 2 * 5 = 10, You can add a zero to the end of a factorial for each multiple of 2 and 5 that are in its expansion.
Ok, thanks very much! I thought it was easy to determine about zeros on the end (multiplying by 10, 20, etc), and what about there are no numbers with 5 zero digits?
– Almaz Murzabekov
Jan 2 at 20:23
2
@AlmazMurzabekov Because 24! has four trailing 0s, and when you multiply that by 25 you get two more (because 25 is 5*5, and both of those 5s pair up with an even number to add a trailing 0). You should see something similar happen at 125.
– Bill the Lizard
Jan 2 at 20:32
add a comment |
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54012549%2fwhy-the-end-of-factorial-numbers-have-too-many-zero-digits%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
There are plenty of 2's available in any factorial, so every 5 that you have will add a trailing 0. However, multiples of higher powers of 5 (e.g. 25, 50, ...) give you more trailing 0's. This is what you noticed: 20! through 24! have 4 trailing 0's, but 25! has 6 trailing 0's, because 25 is 5*5, which adds two trailing 0's.
add a comment |
There are plenty of 2's available in any factorial, so every 5 that you have will add a trailing 0. However, multiples of higher powers of 5 (e.g. 25, 50, ...) give you more trailing 0's. This is what you noticed: 20! through 24! have 4 trailing 0's, but 25! has 6 trailing 0's, because 25 is 5*5, which adds two trailing 0's.
add a comment |
There are plenty of 2's available in any factorial, so every 5 that you have will add a trailing 0. However, multiples of higher powers of 5 (e.g. 25, 50, ...) give you more trailing 0's. This is what you noticed: 20! through 24! have 4 trailing 0's, but 25! has 6 trailing 0's, because 25 is 5*5, which adds two trailing 0's.
There are plenty of 2's available in any factorial, so every 5 that you have will add a trailing 0. However, multiples of higher powers of 5 (e.g. 25, 50, ...) give you more trailing 0's. This is what you noticed: 20! through 24! have 4 trailing 0's, but 25! has 6 trailing 0's, because 25 is 5*5, which adds two trailing 0's.
answered Jan 2 at 20:27


Craig MeierCraig Meier
1,245612
1,245612
add a comment |
add a comment |
Large factorials have a lot of trailing zeroes for two main reasons.
They're composed of a lot of numbers that end in zero. Think of all the numbers like 10, 20, 30, etc. that you multiplied in to the factorial of 1000 to arrive at the answer. Each one of the adds a 0 (two zeroes for 100, 200, etc.), since any number multiplied by 10 will end in a 0.
They're also composed of a lot of numbers that are multiples of 2 or 5. Since 2 * 5 = 10, You can add a zero to the end of a factorial for each multiple of 2 and 5 that are in its expansion.
Ok, thanks very much! I thought it was easy to determine about zeros on the end (multiplying by 10, 20, etc), and what about there are no numbers with 5 zero digits?
– Almaz Murzabekov
Jan 2 at 20:23
2
@AlmazMurzabekov Because 24! has four trailing 0s, and when you multiply that by 25 you get two more (because 25 is 5*5, and both of those 5s pair up with an even number to add a trailing 0). You should see something similar happen at 125.
– Bill the Lizard
Jan 2 at 20:32
add a comment |
Large factorials have a lot of trailing zeroes for two main reasons.
They're composed of a lot of numbers that end in zero. Think of all the numbers like 10, 20, 30, etc. that you multiplied in to the factorial of 1000 to arrive at the answer. Each one of the adds a 0 (two zeroes for 100, 200, etc.), since any number multiplied by 10 will end in a 0.
They're also composed of a lot of numbers that are multiples of 2 or 5. Since 2 * 5 = 10, You can add a zero to the end of a factorial for each multiple of 2 and 5 that are in its expansion.
Ok, thanks very much! I thought it was easy to determine about zeros on the end (multiplying by 10, 20, etc), and what about there are no numbers with 5 zero digits?
– Almaz Murzabekov
Jan 2 at 20:23
2
@AlmazMurzabekov Because 24! has four trailing 0s, and when you multiply that by 25 you get two more (because 25 is 5*5, and both of those 5s pair up with an even number to add a trailing 0). You should see something similar happen at 125.
– Bill the Lizard
Jan 2 at 20:32
add a comment |
Large factorials have a lot of trailing zeroes for two main reasons.
They're composed of a lot of numbers that end in zero. Think of all the numbers like 10, 20, 30, etc. that you multiplied in to the factorial of 1000 to arrive at the answer. Each one of the adds a 0 (two zeroes for 100, 200, etc.), since any number multiplied by 10 will end in a 0.
They're also composed of a lot of numbers that are multiples of 2 or 5. Since 2 * 5 = 10, You can add a zero to the end of a factorial for each multiple of 2 and 5 that are in its expansion.
Large factorials have a lot of trailing zeroes for two main reasons.
They're composed of a lot of numbers that end in zero. Think of all the numbers like 10, 20, 30, etc. that you multiplied in to the factorial of 1000 to arrive at the answer. Each one of the adds a 0 (two zeroes for 100, 200, etc.), since any number multiplied by 10 will end in a 0.
They're also composed of a lot of numbers that are multiples of 2 or 5. Since 2 * 5 = 10, You can add a zero to the end of a factorial for each multiple of 2 and 5 that are in its expansion.
answered Jan 2 at 20:18
Bill the LizardBill the Lizard
296k158500792
296k158500792
Ok, thanks very much! I thought it was easy to determine about zeros on the end (multiplying by 10, 20, etc), and what about there are no numbers with 5 zero digits?
– Almaz Murzabekov
Jan 2 at 20:23
2
@AlmazMurzabekov Because 24! has four trailing 0s, and when you multiply that by 25 you get two more (because 25 is 5*5, and both of those 5s pair up with an even number to add a trailing 0). You should see something similar happen at 125.
– Bill the Lizard
Jan 2 at 20:32
add a comment |
Ok, thanks very much! I thought it was easy to determine about zeros on the end (multiplying by 10, 20, etc), and what about there are no numbers with 5 zero digits?
– Almaz Murzabekov
Jan 2 at 20:23
2
@AlmazMurzabekov Because 24! has four trailing 0s, and when you multiply that by 25 you get two more (because 25 is 5*5, and both of those 5s pair up with an even number to add a trailing 0). You should see something similar happen at 125.
– Bill the Lizard
Jan 2 at 20:32
Ok, thanks very much! I thought it was easy to determine about zeros on the end (multiplying by 10, 20, etc), and what about there are no numbers with 5 zero digits?
– Almaz Murzabekov
Jan 2 at 20:23
Ok, thanks very much! I thought it was easy to determine about zeros on the end (multiplying by 10, 20, etc), and what about there are no numbers with 5 zero digits?
– Almaz Murzabekov
Jan 2 at 20:23
2
2
@AlmazMurzabekov Because 24! has four trailing 0s, and when you multiply that by 25 you get two more (because 25 is 5*5, and both of those 5s pair up with an even number to add a trailing 0). You should see something similar happen at 125.
– Bill the Lizard
Jan 2 at 20:32
@AlmazMurzabekov Because 24! has four trailing 0s, and when you multiply that by 25 you get two more (because 25 is 5*5, and both of those 5s pair up with an even number to add a trailing 0). You should see something similar happen at 125.
– Bill the Lizard
Jan 2 at 20:32
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54012549%2fwhy-the-end-of-factorial-numbers-have-too-many-zero-digits%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
There are about n factors 2 and n/4 factors 5 in n!, thus the same amount of around n/4 trailing zeros.
– LutzL
Jan 2 at 23:33