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

#11
int Sum(int N) {
long long c = 0;
N--; //because you want it less than 1000 if less than or equal delete this line
int n = N/3,b = N/5,u = N/15;
c+= (n*(n+1))/2 * 3;
c+= (b*(b+1))/2 * 5;
c-= (u*(u+1))/2 * 15;
return c;
}


Reply

#12
Using the stepping approach, you can make a version:

#include <stdio.h>

int main ( void ) {

int sum = 0;

for (int i = 0; i < 1000; i += 5) {
sum += i;
}
for (int i = 0; i < 1000; i += 3) {
if (i % 5) sum += i; /* already counted */
}
printf("%d\n", sum);
return 0;
}

which does a whole lot fewer `modulo` computations.

In fact, with a counter, you can make a version with none:

#include <stdio.h>

int main ( void ) {

int sum = 0;
int cnt = 6;

for (int i = 0; i < 1000; i += 5) {
sum += i;
}
for (int i = 0; i < 1000; i += 3) {
if (--cnt == 0) cnt = 5;
else sum += i;
}
printf("%d\n", sum);
return 0;
}
Reply

#13
It should be `sum = sum + i` instead of `1`.
Reply

#14
Interesting fact of the difference in time between this 2 methods... The second method just calculate only with needed numbers and dont iterate through a loop.
Example: 3 * (1+2+3+4...333) (Because 3 * 333 = 999 and 999 < 1000.

static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();

Calc calculator = new Calc();
long target = 999999999;
sw.Start();
Console.WriteLine("Result: " + (calculator.calcMultipleSumFast(3,target) + calculator.calcMultipleSumFast(5, target) - calculator.calcMultipleSumFast(15, target)));
sw.Stop();
Console.WriteLine("Runtime: " + sw.Elapsed);
Console.WriteLine();
sw.Start();
Console.WriteLine("Result: " + (calculator.calcMultiplesSum(3, 5,1000000000)));
sw.Stop();
Console.WriteLine("Runtime: " + sw.Elapsed);

Console.ReadKey();
}

public class Calc
{
public long calcMultiplesSum(long n1, long n2, long rangeMax)
{
long sum = 0;
for (long i = 0; i < rangeMax; i++)
{
if ((i % n1 == 0) || (i % n2 == 0))
{
sum = sum + i;
}
}
return sum;
}

public long calcMultipleSumFast(long n, long rangeMax)
{
long p = rangeMax / n;

return (n * (p * (p + 1))) / 2;
}
}

[![enter image description here][1]][1]


[1]:
Reply

#15
int main(int argc, char** argv)
{
unsigned int count = 0;
for(int i = 1; i < 1001; ++i)
if(!(i % 3) && !(i % 5))
count += i;
std::cout << i;
return 0;
}
Reply

#16
Think declaratively - what you want to do, rather than how you want to do.

In plain language, it boils down to exactly what the problem asks you to do:

1. Find all multiples of 3 or 5 in a certain range (in this case 1 to 1000)
2. Add them all up (aggregate sum)

In LINQ, it looks like:

Enumerable.Range(1, 1000)
.Where(x => (x % 3 == 0) || (x % 5 == 0))
.Aggregate((accumulate, next) => accumulate += next);

Now, you can convert this into an iterative solution - but there is nothing wrong with using a declarative solution like above - because it is more clear and succinct.
Reply

#17
I attempt the **Project Euler #1**: Multiples of 3 and 5. And got 60 points for that.

private static long euler1(long N) {
long i, sum = 0;
for (i = 3; i < N; i++) {
if (i % 3 == 0 || i % 5 == 0) {
sum += i;
}
}
return sum;
}

I use Java for it and you can use this logic for C. I worked hard and find an optimal solution.

private static void euler1(long n) {
long a = 0, b = 0, d = 0;

a = (n - 1) / 3;
b = (n - 1) / 5;
d = (n - 1) / 15;

long sum3 = 3 * a * (a + 1) / 2;
long sum5 = 5 * b * (b + 1) / 2;
long sum15 = 15 * d * (d + 1) / 2;
long c = sum3 + sum5 - sum15;
System.out.println©;
}
Reply

#18
#include <bits/stdc++.h>
using namespace std;

int main()
{
int i,sum=0; //initialize sum with 0
for(i=0;i<1000;i++) //run loop from 0 999 (1000 is not included) because
//we want less than 1000.
{
if(i%5==0||i%3==0) //check if it satisfy one of the
//condition either
//it divides with 5 or 3 completely
//i.e remainder0
{
sum+=i; //increment sum by that number as
//particular number
//satisfy the condition
}
}
cout<<sum; //print sum the value of sum
return 0;
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

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