-2

This is my code for the Codewars problem (Java) yet I cannot make it work. I'm pretty sure I've made a stupid mistake somewhere because of my lack of experience (coding for 4 months)

public static int zeros(int n) {
int f = 1;
int zerocount = 0;

for(int i = 2 ; i <= n; i++){
f *= i;
}

String factorial = String.valueOf(f);

String split [] = factorial.split("");



for(int i = 0; i < split.length; i++){
String m = split[i];
if(m.equals( "0")){
zerocount ++;
}
else {

zerocount = 0;
}

}
return zerocount;
}
}
Stidgeon
  • 2,673
  • 8
  • 20
  • 28
vicsonsam
  • 1
  • 3

4 Answers4

2

In fact, you do not need to calculate the factorial because it will rapidly explode into a huge number that will overflow even a long. What you want to do is count the number of fives and twos by which each number between 2 and n can be divided.

static int powersoffive(int n) {
    int p=0;
    while (n % 5 == 0) {
         p++;
         n /= 5;
     }
     return p;
}

 static int countzeros(int n) {
     int fives = 0;
     for (int i = 1; i <= n; i++) 
          fives += powersoffive(i);

     return fives;
}

Note: Lajos Arpad's solution is superior.

Tarik
  • 10,810
  • 2
  • 26
  • 40
  • You do not need to calculate the number of twos since for every five we have 2 twos. So we need to count fives only. – PM 77-1 Apr 18 '20 at 02:13
  • @PM 77-1: 1, 2, 3, 4, 5: one 5 and 3 twos. But, ok, got it. Plenty of twos :-) – Tarik Apr 18 '20 at 02:27
1

As pointed out by other users your solution will probably not be accepted because of the exploding factorial you are calculating.

About the code you wrote there are two mistakes you have made:

You are calculating the factorial in the wrong way. You should start with i = 2 in the loop

for(int i = 2; i <= n; i++){
    f *= i;
}

Also in Java you cannot compare strings using ==. This is not valid

if(m == "0")

You should compare them like this

if(m.equals("0"))

Anyway this is how I would have resolved the problem

public static int zeros(int n) {
    int zerocount = 0;
    for (int i = 5; n / i > 0; i *= 5) {
        zerocount += n / i;
    }
    return zerocount;
}
Flood2d
  • 1,338
  • 8
  • 9
  • public class Solution { public static int zeros(int n) { int f = 1; int zerocount = 0; for(int i = 2 ; i <= n; i++){ f *= i; } String factorial = String.valueOf(f); String split [] = factorial.split(""); for(int i = 0; i < split.length; i++){ String m = split[i]; if(m.equals( "0")){ zerocount ++; } else { zerocount = 0; } } return zerocount; } } This is what I have now, It still doesn't properly work though. – vicsonsam Apr 18 '20 at 02:33
  • Of course, I didn't know I could do that. It was a good answer and I learned that every 5 digits in a factorial are an extra 0 at the end, thank you, have a good day. – vicsonsam Apr 18 '20 at 02:42
1

A zero in a base-10 representation of a number is a 2*5. In order to determine the number of trailing zeroes you will need to determine how many times can you divide your number with ten, or, in other words, the minimum of the sum of 2 and 5 factors. Due to the fact that 5 is bigger than 2 and we go sequentially, the number of fives will be the number of trailing zeroes.

A naive approach would be to round down n/5, but that will only give you the number of items divisible with 5. However, for example, 25 is divisible by 5 twice. The same can be said about 50. 125 can be divided by 5 three times, no less.

So, the algorithm would look like this:

int items = 0;
int power = 5;
while (power < n) {
    items += (int) (n / power);
    power *= 5;
}

Here small numbers are in use in relative terms, but it's only a proof of concept.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
0

You do need to use brute force here and you integers will overflow anyway.

With multiplication trailing zero appears only as the result of 2*5.

Now imagine the factorial represented by a product of it's prime factors.

Notice that for every 5 (five) we will always have 2 (two).

So to calculate the number of zeroes we need to calculate the number of fives.

That can be implemented by continuously dividing N by five and totaling results

In Java code that will be something like this:

static int calculate(int n)
{
    int result = 0;
    while (n > 0 ) {
        n /= 5;
        result += n;
    }
    return result;
}
PM 77-1
  • 12,933
  • 21
  • 68
  • 111