Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 671 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find the sum of all the multiples of 3 or 5 below 1000

#1
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.
I have the following code but the answer does not match.

#include<stdio.h>
int main()
{
long unsigned int i,sum=0;
clrscr();
for(i=0;i<=1000;i++)
{
if((i%5==0)||(i%3==0))
{
sum=sum+1;
}
}
printf("%d\n",sum);
getchar();
return 0;
}
Reply

#2
Perhaps you should do

sum += i // or sum = sum + i

instead of

sum = sum + 1

Additionally, be careful when printing `long unsigned int`s with printf. I guess the right specifier is `%lu`.
Reply

#3
Two things:

* you're *including* 1000 in the loop, and
* you're adding one to the sum each time, rather than the value itself.

Change the loop to

for(i=0;i<1000;i++)

And the sum line to

sum=sum+i;
Reply

#4
Here's a python one-liner that gives the correct answer (233168):

reduce( lambda x,y: x+y, [ x for x in range(1000) if x/3.0 == int( x/3.0 ) or x/5.0 == int( x/5.0 ) ] )
Reply

#5


#include<stdio.h>
#include<time.h>
int main()
{
int x,y,n;
int sum=0;
printf("enter the valeus of x,y and z\n");
scanf("%d%d%d",&x,&y,&n);
printf("entered valeus of x=%d,y=%d and z=%d\n",x,y,n);
sum=x*((n/x)*((n/x)+1)/2)+y*((n/y)*((n/y)+1)/2)-x*y*(n/(x*y))*((n/(x*y))+1)/2;
printf("sum is %d\n",sum);
return 0;
}
// give x,y and n as 3 5 and 1000

Reply

#6
Just as an improvement you might want to avoid the loop altogether :

multiples of 3 below 1000 form an AP : 3k where k in [1, 333]
multiples of 5 below 1000 form an AP : 5k where k in [1, 199]

If we avoid multiples of both 3 and 5 : 15k where k in [1, 66]

`So the answer is : 333*(3+999)/2 + 199(5+995)/2 - 66*(15+990)/2 = 2331`68

Why you might want to do this is left to you to figure out.
Reply

#7
package com.venkat.test;

public class CodeChallenge {

public static void main(String[] args) {

int j, sum=0;

for ( j = 0; j <=1000; j++) {
if((j%5==0)||(j%3==0))
{
sum=sum+j;
}
}
System.out.println(sum);
}
}
Reply

#8
You might start by iterating from `3` to `1000` in steps of `3` (3,6,9,12,etc), adding them to the `sum` like,

int i = 3, sum = 0;
for (; i < 1000; i += 3) {
sum += i;
}

Then you could iterate from `5` to `1000` by `5` (skipping multiples of `3` since they've already been added) adding those values to the `sum` as well

for (i = 5; i < 1000; i += 5) {
if (i % 3 != 0) sum += i;
}

Then display the `sum`

printf("%d\n", sum);

Reply

#9
Python implementation of the problem. Short, precise and fat.

sum=0
for i in range(1000):
if (i%3==0) or (i%5==0):
sum=sum+i
print sum
Reply

#10
Rather than using range/loop based solutions you may wish to leverage more math than brute force.

There is a simple way to get the sum of multiples of a number, less than a number.

For instance, the sum of multiples of 3 up to 1000 are: 3 + 6 + 9 + ... + 999
Which can be rewritten as: 3* ( 1 + 2 + 3 + ... + 333)

There is a simple way to sum up all numbers 1-N:

Sum(1,N) = N*(N+1)/2

So a sample function would be

unsigned int unitSum(unsigned int n)
{
return (n*(n+1))/2;
}

So now getting all multiples of 3 less than 1000 (aka up to and including 999) has been reduced to:

3*unitSum((int)(999/3))

You can do the same for multiples of 5:

5*unitSum((int)(999/5))

But there is a caveat! Both of these count multiples of both such as 15, 30, etc
It counts them twice, one for each. So in order to balance that out, you subtract once.

15*unitSum((int)(999/15))

So in total, the equation is:

sum = 3*unitSum((int)(999/3)) + 5*unitSum((int)(999/5)) - 15*unitSum((int)(999/15))

So now rather than looping over a large set of numbers, and doing comparisons, you are just doing some simple multiplication!
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through