What am I missing

Alright so I've attempted to complete two problems from this site one of which I'm done with(The small factorial challenge). The one I'm stuck on is the trailing zeros one. The algorithm that I created was by finding out that a good portion of the factorial numbers created a trailing zero every time you increment by 5(2 zeros after incrementing by 5 five times). For the most, my theory was correct. But when I inputted 1024! I got 244 trailing zeros when the site says it's 253. Where do you think I when wrong?

link to the problem http://www.spoj.com/problems/FCTRL/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <limits>

using namespace std;

long long factorialFinder(long x)
{
    if(x==0)
    {
        return 1;
    }
    else
    {
        return x*factorialFinder(x-1);
    }
}

int trailingZeros(int x)
{
    int tempNum = 0;
    int zeros = 0;
    int index = 1;
    int counter = 1;
    tempNum = x;

    while(tempNum > 0)
    {
        if(index == counter * 5)
        {
            index++;
            zeros += 2;
            counter++;
            tempNum -= 5;
        }
        else if(tempNum % 5 == 0)
        {
            index++;
            zeros++;
            tempNum -= 5;
        }
        else
            tempNum--;
    }

    return zeros;
}

void checkingInput(long long &num)
{
    while(!(cin) || num < 0)
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cerr << "\nThis is not a number or a positive number. ";
        cin >> num;
    }
}

void checkingInput(int &num)
{
    while(!(cin) || num < 0)
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cerr << "\nThis is not a number or a positive number. ";
        cin >> num;
    }
}

int main()
{
    long long x;
    int testCases = 0;

    cout << "How many test cases would you like to perform! ";
    cin >> testCases;
    checkingInput(testCases);

    cout << endl;
    cout << "Enter a number and I will find its factorial and how many trailing zeros it has. " << endl;

    while(testCases > 0)
    {
        cin >> x;
        checkingInput(x);
        cout << endl;
        cout << "This factorial has " << trailingZeros(x) << " trailing zeros. " << endl;
        cout << "The factorial product for this number is " << factorialFinder(x) << endl;
        testCases--;
        cout << endl;
    }
}
Last edited on
i would like to count the zeros this way:
1
2
3
4
5
6
for(int i=1; i<=N; i++) {
/*
1. count how many zeros are at the end of each numbers, add them
2. find how many pairs (5,2) || (5,4) || (5,6) || (5,8)  -> pairs are from the end of two numbers(which you are multiplying), discarding zeros
*/
}
Last edited on
Topic archived. No new replies allowed.