Friday, June 7, 2013

Project Euler- Problem Number 1 Solution

Project Euler 1: Multiples of 3 and 5
Project Euler- Problem Number 1
Project Euler-Problem Number 1 Solution
Project Euler-Problem Number 1 Solution in C
Multiples of 3 and 5 Problem in Project Euler
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.

My first suggestion to solve this problem, read more carefully the problem and think that how should we have to do to solve it. well. Now we need to iterate/count the numbers from 1 to 1000. Now what should we have done? Don't worry.. Now we take a number  and check it if it's divisible by 3 or 5. .If any number is divisible by 3 or 5 then we will remark this number and go to the next step. 
After checking all numbers which is divisible by 3 or 5 with re-mark now  we will sum all numbers one by one which is divisible by 3 or 5.
The we will find out final result. I use C programming language to solve this problem. I think C programming is the best programming. Below is my C programming source code: 

//Find the sum of all the multiples of 3 or 5 below 1000
#include<stdio.h>
#include<conio.h>
void main ()
{
int a,b,i,Sum=0;
    for (i=1;i<1000;i++)
    {
       a=i%3;
       b=i%5;
        if(a==0||b==0)
        {
            Sum+=i;
        }
    }
    printf("\nThe sum of all the multiples of 3 or 5 below 1000: %d\n",Sum);


To solve this problem I use some modulo operator such as for, % ,||,if etc.
For loop help us to count number from 1 to 1000. 
In 2nd step C program check the number if it divisor by 3 (a) or 5 (b).
In 3rd option if the number is divisor by any one of 3 and 5 then the sum variable will add (sum) this number
After completing the for loop condition the C program will excite it's result.

6 comments:

  1. I have compiled your program. The output is -28976. I have used Turbo c++ compiler standard edition. The projecteuler.net says that your output is wrong. The correct output is available in google that is 233168. I need the correct source code based on problem-1 of euler.net. Thanks

    ReplyDelete
    Replies
    1. I agree with you..

      Delete
    2. this code is correct in c programming.pls check your compiler.

      Delete
  2. I use GNU GCC compiler..& the correct result is 233168..I run this code in "Code Blocks" with GNU GCC compiler..I hope if you use this compiler you will get correct result in this code.

    ReplyDelete
  3. Yap!!! This code is correct for problem number-1 in project Euler..plz check yours compiler

    ReplyDelete