Jump to content

Java program to count the occurences of number 2


Recommended Posts

Posted

Hi, I read a Daily Mail article the other day with an interview question that said "If I wrote down all the numbers from 1 to 1,000,000, how many times would I have written down the number 2?". Daily Mail said the result was 111,111 which I didn't trust and another source said 599,985 so I decided to write a computer program to calculate the real answer. Java was always my strongest language at university so here is what I came out with:

 

Hey Jord, how are you guys? how's everything with the house?

 

 

You were always good with Java, can you tell me why this program is always returning 0 as the result?

 

 

import java.util.Scanner;
import java.util.Arrays;
class FindNumberOfOccurences
{
 public static void main (String[] args)
 {
   int number1 = 0;
   int number2 = 0;
   String currentNumber = "";
   int count = 0;
   String S;
   String numberInText;
   char two = 2;
   
   
   Scanner in = new Scanner(System.in);
   
   //Enter first number
   System.out.println("Please enter number 1");
   number1 = Integer.parseInt(in.nextLine());
   System.out.println("You entered " + number1);


   //Enter second number
   System.out.println("Please enter number 2");
   number2 = Integer.parseInt(in.nextLine());
   System.out.println("You entered " + number2);
   
   char[] digits = new char[number1];
   
   while (number1 <= number2)
   {
     System.out.println("number 1 is " + number1);
     //Convert number to String
     currentNumber = Integer.toString(number1);
     
     //Print current Number
     System.out.println("Current Number = " + currentNumber);
     
     //Convert String to character array
     digits = currentNumber.toCharArray();
     //Print current array
     System.out.println(digits);
     
     //Count occurences of "2" in array
     for (int i=0;i < digits.length;i++)
     {
       //Print current character in array
       System.out.println("Current Array Character is " + digits[i]);
       //If character in array = 2 then increment counter
       if (digits[i] == 2)
       {
         count++;
       }
     }
     //Display current count
     System.out.println("Current count = " + count);
     number1++;
   }
   //Print result
   System.out.println("Number 2 occurs " + count + " times"); 
 }
}

 

There are no compiler errors being thrown up but no matter what 2 numbers I input, the result of count always comes out as 0. Can anyone point out where I'm going wrong?

Posted
My first thought is the line "if digits == 2", you're comparing an int with a char, which won't match as the char value of 2 is not, well, 2.

 

THANK YOU! I added apostrophes around the 2 and it looks to be giving the correct result. Certainly for number1 = 1 and number2 = 2!

 

if digits[i] == '2'

Posted

Ha, yeah, that'll do it. :D

 

- - - Updated - - -

 

Also, surely the answer to the original question is 1? The number 2 is only written once, but the digit 2 is written however many times your program spits out...

Posted
Ha, yeah, that'll do it. :D

 

- - - Updated - - -

 

Also, surely the answer to the original question is 1? The number 2 is only written once, but the digit 2 is written however many times your program spits out...

 

Sorry, I was typing from memory, it was about the occurrences of the digit 2 not number 2!

 

Although, it would be a great trick question in the interview if they actually said 2!

Posted
This
$c=0;
for ($i=1; $i <= 1000000; $i++) {
$j = (string) $i;
$c = $c + substr_count($j,"2");
}
echo $c;
?>

says 600000 but it may or may not be correct code. It did come up with correct answer for 30, which is encouraging. It's damn hard to concentrate on code on Saturday, esp when it's college open day.

Posted

There we go. Think I have a simple way of doing it. Consider it as 000001 to 999999 (ie. take 1 away, which gives us 6 digits long). The answer is 1000000*6/10.

 

Works for other numbers too, if it were between 1 and 10,000 you end up with 10000*4/10, which is 4000.

 

That only works for 1 to N where N is 1000,10000,100000 etc... though. It'd need more work if the start finish numbers vary more.

 

Also, a simple way to do it in python - str(range(1,1000000)).count('2')

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...