Ravi Soni
Ravi Soni

Reputation: 141

Error because of using scientific notation

I attempted a Hackerearth tutorial question and was able to solve it correctly using following code:

    #include <iostream>
    using namespace std;
    int main() {
        const long int MOD = 1000000007;
        const long int SIZE = 100001;
        long int t, n;
        long int cache[SIZE];

        cache[0] = 1;

        for (long int i = 1; i < SIZE; i++) {
            cache[i] = ( i * cache[i-1] ) % MOD;
        }

        cin >> t;
        while (t--) {
            cin >> n;
            cout << cache[n] << endl;
        }

        return 0;
    }

However when using scientific notation to write MOD or SIZE, online judge reports incorrect answers. What am I missing here?

    #include <iostream>
    using namespace std;
    int main() {
        const long int MOD = 10e9+7;
        const long int SIZE = 100001;
        long int t, n;
        long int cache[SIZE];

        cache[0] = 1;

        for (long int i = 1; i < SIZE; i++) {
            cache[i] = ( i * cache[i-1] ) % MOD;
        }

        cin >> t;
        while (t--) {
            cin >> n;
            cout << cache[n] << endl;
        }

        return 0;
    }

Upvotes: 0

Views: 148

Answers (1)

Sami Kuhmonen
Sami Kuhmonen

Reputation: 31203

Scientific notation means “constant multiplied by ten to the power of x”. If you want 1000000007 you need 1e9, meaning “one followed by nine zeroes.” Now you use 10e9 which is “ten followed by nine zeroes” or “one followed by ten zeroes” so you’re off by a factor of ten.

Upvotes: 2

Related Questions