Sunday, March 15, 2015

(C Language) (ENG) Sum Square Difference - Project Euler Problem 6

PROBLEM

In this problem, you asked to find difference between "square of summation of first 100 natural numbers" and "summation fo squares of first 100 natural numbers".

For example from Project Euler:

The sum of the squares of the first ten natural numbers is,
12 + 22 + ... + 102 = 385

The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)2 = 552 = 3025

Difference: 3025 − 385 = 2640.



This c problem is taken by me from: (https://projecteuler.net/problem=6)


SAMPLE RUN:

In addition to problem, i printed result on the screen.




SOLUTION

You can copy codes below to your compiler to execute.




#include <stdio.h>

int main(void) //main function.
{

int sumOfSq = 0; //it will hold summation of squares.
int sqOfSum = 0; //it will hold square of summation.

for (int k = 1; k <= 100; k++) //this loop will finde summation of squares.
sumOfSq = sumOfSq + k * k;
for (int k = 1; k <= 100; k++) //this loop will find summation of first 100 natural numbers.
sqOfSum = sqOfSum + k;
sqOfSum = sqOfSum * sqOfSum; //this line will find square of summation.

printf("difference between summation of squares and square of summation is %d", sqOfSum - sumOfSq); //this line will find difference and print it on the screen.

return 0;
}

(C Language) (ENG) Smallest Multiple - Project Euler Problem 5

PROBLEM

For example 2520 is the smallest positive number which can be divided by integers from 1 to 10(included) with remainder '0'.

What is the smallest positive number which can be divided by integers from 1 to 20(included) then?

This c problem is taken by me from: (https://projecteuler.net/problem=5)

SAMPLE RUN:

In addition to problem, i printed result on the screen.




SOLUTION

You can copy codes below to your compiler to execute.




#include <stdio.h>

int main(void) //main function.
{

int spn = 20; //smallest positive number that mentioned.
int flag = 0; //we will use it to get out from loop or continue.
int counter = 0; //this counter will show program how many times spn was divided with remainder 0.

while(flag == 0)
{
spn++; //this code will update spn to check next value.
for (int k = 1; k <= 20; k++) //this loop will find if spn is divisible for all integers 20 >= x > 1 with 0 remainder.
{
if (spn % k == 0)
counter++;
else break;
}
if (counter == 20) //if it is true, it means that we find spn that I searched.
flag = 1;
counter = 0; //this code will turn counter to 0 to use it again.
}

printf("smallest positive number is %d", spn);

return 0;
}

Friday, March 13, 2015

(C Language) (ENG) Even Fibonacci Numbers - Project Euler Problem 2

PROBLEM



Write a program calculating summation of even Fibonacci numbers starting from "1, 2, 3, 5, 8, 13..." below 4.000.000.

This c problem is taken by me from: (https://projecteuler.net/problem=2)

SAMPLE RUN:

In addition to problem, i printed result on the screen.





SOLUTION

You can copy codes below to your compiler to execute.


#include <stdio.h>

int main(void) //main function.
{
int sum = 0; //this variable will hold summation of even Fibonacci numbers.
int a, b, c; //due to I can find summation with only 3 elements, I will use 3 elements.

a = 1; //in this question, our series starts with 1...
b = 2; //...and continuous with 2.
c = 0; //this line causes c to get into loop.

while (c < 4000000) //this loop will find the summation of even numbers.
{
c = a + b; //it will find the next element's value.

if (c % 2 == 0) //this comparison will determine if c is even or not.
sum = sum + c; //it will calculate summation of c.
a = b; //we can keep next value(which is b after a) in a with that line.
b = c; //we can keep next value(which is c after b) in a with that line.
}

sum = sum + 2; // in loop above, we find the summation except b that we initialized which is also even.

printf("Summation of even Fibonacci numbers below 4.000.000 is %d", sum); //it will print the summation on the screen.

return 0;
}

Thursday, March 12, 2015

(C Language) (ENG) Multiples Of 3 And 5 - Project Euler Problem 1

PROBLEM

Write a program calculating summation of (multiples of 3 or 5).

This c problem is taken by me from: (https://projecteuler.net/problem=1)

SAMPLE RUN:


In addition to problem, i printed result on the screen.





SOLUTION

You can copy codes below to your compiler to execute.


#include <stdio.h>

int main(void) //main function.
{
int sum = 0; //this variable will hold summation of multiples of 3 or 5 below 1000.

for (int k = 3; k < 1000; k++) //this loop will find the summation of multiples of 3 or 5.
if (k % 3 == 0 || k % 5 == 0) //this comparison will determine if k is multiple of 3 or 5 or not.
sum = sum + k; //it will calculate summation of ks which are multiples of 3 or 5.

printf("Summation of multiples of 3 or 5 below 1000 is %d", sum); //it will print the summation on the screen.

return 0;
}