Euler Problem: Solution of Problem-6 in C++

Q)The sum of the squares of the first ten natural numbers is,

#include  

#define N 100

using namespace std;

long raiseTo ( const long num, const long n ) {
    int ans = num;
    for ( int i = 1; i < n; i++ ) {
        ans *= num;
    }
    return ans;
}

int main ( int argc, char * argv[] ) {
    long result =     (             ( raiseTo ( N, 4 ) / 4 ) 
                            +     ( raiseTo ( N, 3 ) / 6 ) 
                            -     ( raiseTo ( N, 2 ) / 4 ) 
                            -    ( N / 6 ) 
                    ); 
    cout << result << endl;
}

// 25164150

Published by

Sreejith

A strong believer of: 1. Knowledge is power 2. Progress comes from proper application of knowledge 3. Reverent attains wisdom 4. For one's own salvation, and for the welfare of the world

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s