
-----------------------------------
Nischal Regmi
Tue May 25, 2010 4:59 am

a counting problem
-----------------------------------
I have faced a counting problem. How many 4-digit numbers are there having three same digits? I think it is 360, but i'm not sure. Can any one explain the correct answer?

-----------------------------------
andrew.
Tue May 25, 2010 2:42 pm

RE:a counting problem
-----------------------------------
What exactly do you mean? Do you mean like this:
0000
0001
0002
...
1110
1112
...
2111
...
2202

Are all of those numbers valid in your question?

-----------------------------------
jcollins1991
Tue May 25, 2010 3:06 pm

Re: a counting problem
-----------------------------------
If leading zero's are counted it'd be 410 ways or something, so he's probably looking for numbers starting at 1000 and ending at 9999

-----------------------------------
chrisbrown
Tue May 25, 2010 3:21 pm

Re: a counting problem
-----------------------------------
I have faced a counting problem. How many 4-digit numbers are there having three same digits? I think it is 360, but i'm not sure. Can any one explain the correct answer?
You are correct based on the following assumptions: 
(a)  4-digit numbers can start with 1 or more zeros, i.e. 0001, 0111 are both valid.
(b)  You exclude numbers with four identical digits, i.e. 0000, 1111 are invalid.

I can't explain how to find the answer mathematically, but this Quick n' Dirty? Java program will compute it:
public class CountDigits {

	private static String padWithZeros(String num) {
		if ( num.length() < 4 ) return padWithZeros("0" + num); // Delete this line to account for case (a)
		return num;		
	}
	
	private static boolean has3(String num) {
		int

-----------------------------------
andrew.
Tue May 25, 2010 3:49 pm

RE:a counting problem
-----------------------------------
Is 1101 valid?

-----------------------------------
Unnamed.t
Tue May 25, 2010 4:36 pm

Re: a counting problem
-----------------------------------
The following code should be quite precise. I'm sorry its so messy and yes I know it can be greatly shortened (for example, running all calculations in one for loop), but this is just a draft to see if it actually fits to answering the problem. 

I have counted numbers from 1000 to 9999 in one for loop and numbers 0000 to 0999 in a separated for loop. I have excluded the evaluation of numbers with 4 of the same digits (e.g. 1111). In the end there were 370 4 digit numbers that have 3 of the same digits (including numbers that start with 0 (e.g. 0001, 0002, 0100)). There are 333 numbers from the range 1000 to 9999 and 37 numbers from 0 to 999.

You can also view all the numbers that were counted by taking out the commented lines that output vars count and count2.


public class DigitCount
{
    public static void main (String

-----------------------------------
jcollins1991
Tue May 25, 2010 5:15 pm

Re: a counting problem
-----------------------------------
OOOOOOPSSSSSS... I thought I was under counting when I was actually over counting... Here's the right way:

You have 4 spots, 3 are going to be taken
_XXX, X_XX, XX_X, XXX_

So you have 4 ways you can place the 3 digits together

There are 10 options for the 3 digits together, so now you have 40 ways to place the 3 digits. Now to fill in the empty spot you don't want the same digit, so you only have 9 options after you've chosen the triplet digit. So 9 * 40 = 360 possible ways...

-----------------------------------
Unnamed.t
Tue May 25, 2010 5:53 pm

Re: a counting problem
-----------------------------------
You have 4 spots, 3 are going to be taken 
_XXX, X_XX, XX_X, XXX_ 

So you have 4 ways you can place the 3 digits together 

There are 10 options for the 3 digits together, so now you have 40 ways to place the 3 digits. Now to fill in the empty spot you don't want the same digit, so you only have 9 options after you've chosen the triplet digit. So 9 * 40 = 360 possible ways...

Wow what you said actually makes a lot of sense. But, there has to be some kind of mistake in that logic that oversees 10 possibilities. It is really hard to say, but my program says there are 370 in it doesn't seem to repeat any numbers or overdo any of the counting. 

Hopefully there will be more algorithms uploaded to support the 360 or 370 number possibilities.

-----------------------------------
jcollins1991
Tue May 25, 2010 6:04 pm

Re: a counting problem
-----------------------------------
It's 360, the extra 10 are when they're all the same making it 370 total... The question isn't completely clear as to only 3 of the same digit or atleast 3 of the same digit, so either answer is right...

-----------------------------------
chrisbrown
Tue May 25, 2010 9:03 pm

Re: a counting problem
-----------------------------------
The question isn't completely clear 
Interesting logic. Personally, I would seek clarification rather than exploit ambiguities.

-----------------------------------
jcollins1991
Tue May 25, 2010 9:34 pm

Re: a counting problem
-----------------------------------
The question isn't completely clear 
Interesting logic. Personally, I would seek clarification rather than exploit ambiguities.

*sigh*... OP said they though the answer was 360 at the very start, I came up with a explanation that gave the same result... 370 is what Unnamed.t said it should be based on their program which also counted when all 4 are the same, I only gave the explanation to why he was getting that...

note: 360 could also be if you're not counting leading zeros but you're counting ones with 4 digits the same

-----------------------------------
chrisbrown
Tue May 25, 2010 9:54 pm

Re: a counting problem
-----------------------------------
The question isn't completely clear Interesting logic. Personally, I would seek clarification rather than exploit ambiguities.*sigh*... OP said they though the answer was 360 at the very start, I came up with a explanation that gave the same result... 370 is what Unnamed.t said it should be based on their program which also counted when all 4 are the same, I only gave the explanation to why he was getting that...
That was rude of me, I apologize.


note: 360 could also be if you're not counting leading zeros but you're counting ones with 4 digits the same
I think that's 342 iirc, both of the cases in my first post.

-----------------------------------
jcollins1991
Tue May 25, 2010 10:06 pm

Re: a counting problem
-----------------------------------
Oh ya it is 342 >.