Find the sum of all the multiples of 3 or 5 below 1000 or this bird will stab you

Find the sum of all the multiples of 3 or 5 below 1000 or this bird will stab you.

Other urls found in this thread:

pastebin.com/139TJdya
pastebin.com/CCQg5H8c
pastebin.com/13n3CA6U
math.stackexchange.com/questions/9259/find-the-sum-of-all-the-multiples-of-3-or-5-below-1000
twitter.com/AnonBabble

const sumOfAllTheMultiplesOf3Or5Below1000 = require('sum-of-all-the-multiples-of-3-or-5-below-1000');

Console.log(sumOfAllTheMultiplesOf3Or5Below1000());

Untested because mobilefag

reduce(lambda x, y: x + y, filter(lambda x: not (x % 3 and x % 5), range(1000)))

Total[Select[Range[1000], Divisible[#, 3] || Divisible[#, 5] &]]

SML
List.foldl op+ 0 (List.filter (fn x => x mod 3 = 0 orelse x mod 5 = 0) (List.tabulate (1000, fn n => n)))

int count = 0;
for(int i = 0; i < 1000; i++) {
if(i == 3)
count += i;
else if(i == 5)
count += i;
else if(i == 6)
count += i;
else if(i == 9)
count += i;
else if(i == 10)
count += i;
...

}

sum([i for i in range(1000) if i % 3 == 0 or i % 5 == 0])

Easy.
pastebin.com/139TJdya

do your own homework you fucking FAGGOT

Remake of FizzBuzz. You won't fool me.

Prelude> sum $ filter (\x -> x `mod` 3 == 0 || x `mod` 5 == 0) [1..9999]
23331668

Oh, below 1000, not 10000

$ awk 'BEGIN{sum=0;for(i=1;i

Enumerable.Range(1,1000).Where(a => a % 3 == 0 || a % 5 == 0).Sum()

a = sum(3:3:999) + sum(5:5:999);

God bless Matlab

wat languge be dat

c# with linq

xor ebp, ebp ; sum
mov ecx, 999
mov di, 3
mov si, 5
.l:
lea ebx, [ebp+ecx]
mov ax, cx
xor dx, dx
div di
test dx, dx
jz .c
mov ax, cx
xor dx, dx
div si
test dx, dx
.c:
cmovz ebp, ebx
loop .l

Do your homework yourself, pajeet

int i = 3+5+6+9+10+...+999+1000
return i

Neat.

Bash.
n=;for((i=0;i

#include
#define SIZE 999

int div_sum(int n){
int m = SIZE / n;
return n*(m*(m + 1)) / 2;
}

int main(){
printf ("%d\n", div_sum(3) + div_sum(5) - div_sum(15));
}

print 233168

Project Euler

int main()
{
unsigned int sum = 0;
for(unsigned int i = 0; i < 1000; i++)
{
if((i % 3 == 0) || (i % 5 == 0))
{
sum += i;
}
}

std::cout

First good answer.

my nigga

puts ((3..999).step(3).to_a|(5..995).step(5).to_a).reduce(:+)

Sum too high. You're adding the intersection between both ranges twice now.

Aperantly, im to smart and riched the limit of Sup Forums length.

here is my code
pastebin.com/CCQg5H8c

dude my solution was exactly the same. will you be my friendo?

>Using iteration
Absolutely disgusting

"- sum (15:15:999)"
(i am not familiar with syntax, following yours)

Yes.

You can omit the square brackets, thus the argument to sum will be a generator. This way the computer won't store all of the list in memory.

sum=(3..999).inject{|s,n|s+(n%3*n%5>0?0:n)}

count = 0;
for(int i = 0; i

that's pretty smart

good job user

#include
#include

/* requires C99 or better because of long long */
int main(void) {
unsigned long long a = 0;
/* loop through integers 1 through 999 inclusive */
for(size_t i = 1; i

>unnecessary braces
disgusting.

use strict;
use warnings;
use v5.10;
use List::Util qw(sum);

say sum(map { $_ if $_ % 3 == 0 or $_ % 5 == 0 } (1..999));

>reduce(lambda x, y: x + y,
2cool4sum
end your life my man

erlang
lists:sum([X+Y || X

var stupidfuckingquestion = Array.apply(null,{length:1000}).reduce((t,x,i)=>{
if (t == undefined) t = 0;
if ((i+1)%3==0 || (i+1)%5==0){return t+(i+1)}
else{return t}
})
console.log(stupidfuckingquestion)

Not using `rem` instead of `mod`

sum [x | x

pastebin.com/13n3CA6U

Why the fuck are all you brainlets iterating over 1000 numbers except for

Think before you code. Pic related from when I first saw the thread

why think when the compiler does it for you

Does this actually work? That's so few lines.

#include

int main(void)
{
for(int i = 3; i < 1000; i++)
{
if(!(i%3 && i%5))
printf("%d\n", i);
}
}

yes, result will be in register ebp. loops down from 999, 2 divisions per iteration

aw shit nibba, forgot the main goal
#include

int main(void)
{
for(int i = 3; i < 1000; i++)
{
if(!(i%3 && i%5))
printf("%d\n", i);
}
}

and also to copy the new code...
#include

int main(void)
{
int sum = 0;

for(int i = 3; i < 1000; i++)
{
if(!(i%3 && i%5))
sum += ("%d\n", i);
}

printf("%d", sum);
}

total = 0
for i in range(0, 1001):
if i % 3 == 0 or i % 5 ==0:
total += i
print(total)
[/code

int main()
{
unsigned long sum = 0;
for(int i = 0; i < 1000; i++)
{
if(i % 3 == 0 || i % 5 == 0)
{
sum = sum + i;
}
}
}

sum [x | x

PROGRAM Multiples3and5
IMPLICIT NONE
INTEGER :: COUNT, SUM

COUNT = 1
SUM = 0

DO WHILE (COUNT < 1000)

IF (MOD(COUNT, 3) ==0 .OR. MOD(COUNT, 5)== 0) THEN

SUM = SUM + COUNT

END IF

COUNT = COUNT + 1

END DO

PRINT *, SUM
END PROGRAM

unsigned Sum(unsigned n)
{
return n>0?n%3==0?n+Sum(n-1):n%5==0?n+Sum(n-1):Sum(n-1):0;
}

This is wrong. The correct answer is 233168. It says BELOW 1000

I'm not even angry at this point.

#include
using namespace std;
int main() {
int n=1000;
int sum=0;
for (int i=1; i

Learn to ((

(loop for i below 1000 when (or (= 0 (mod i 3)) (= 0 (mod i 5))) sum i)

here u go op
MSGBOX("233168")

3*(333*334/2) + 5*(199*200/2) - 15*(66*67/2) = 233168

Im retarded so explain this to me

why

>if(!(i%3 && i%5))
does && require both sides to be true? and for example 9%5 is 4, I assume modulo 0 is false but 4 is implicitly true?

I was wondering why not use something like

's
if (i%3==0 or i%5==0)

Educate me. How would you write it?

I've been golfing.

Go to sleep Gramps.

sum({3*i for i in xrange(1, 334)} | {5*i for i in xrange(1, 200)})

>if (i%3==0 || i%5==0)
is what you should use instead of
>if (!(i%3 && i%5))
because it's clearer.

They'll compile to the same thing at O3.

Has anyone done it in constant time yet

...

>recursion

There are infinite numbers below 1000

integers motherfucker, do you speak it?

No one is talking about floats or irrational numbers

The other user said infinite numbers below 1000.
You mentioned integers.

I want you to grab a glass of water, sit down, take a sip of water and read through your post again.

Yes I do speak it. Have you ever heard of negative numbers? Many of which are integers and divisible by 3 or 5?

I knew I should have mentioned negatives, I didn't think of it until after I posted

The whole thing is a reference to that primes between 0 and 2million meme going around anyway we all know this.

You think multiplication and division are constant time?

Yes, but not infinite multiples of 3 or 5.

This.

honestly i know some CS seniors in my classes that wouldn't be able to do this on their own. i always wonder how the fuck these people will get a job

This attempt is stupid but whatever...

Yeah this is the best solution, and then you just factor out the 3 of the sum and apply the sum formula n(n+1)/2 and voila

>else count += 0
really makes your neurons fire

for(var s=0,i=0;i

For JavaScript fags.
var SIZE=999;
function div_sum(n){
var m=(SIZE/n)|0;
return(n*(m*(m+1))/2)|0;
}
console.log(div_sum(3)+div_sum(5)-div_sum(15));

for(var i = 1, f = 0; i < 1000; f += i % 5 == 0 || i % 3 == 0 ? i : 0, i == 999 ? console.log(f) : 1, i++);

Is still shorter than

But it's a single statement. I rarely see people do this with for loops here.

That's because JavaScript might be the only language that can do that.

\sum _{a=0}^c a*b
is same as
b*(((c^2)/2)+(c/2))

d(b,c)=b(((c^2)/2)+(c/2))
e=3
f=5
g=lcm(e,f)
h=999
( d(e,floor(h/e))+d(f,floor(h/f)) )-d(g,floor(h/g))=233168

...

n = floor(1000 / 15);
n+1 * 3,5,6,9,10,12,15 + sum(1 to n)*15*7
+ 3 iif 1000

lol

x = floor(1000 / 3)
y = floor(1000 / 5)
z = floor(1000 / 15)
sum(1 to x) * 3 + sum(1 to y) * 5 - sum(1 to z) * 15

function gcd(a,b){return!b?a:gcd(b,a%b);}
function lcm(a,b){return(a*b)/gcd(a,b);}
function sum(mult,to){return mult*((Math.pow(to,2)/2)+(to/2));}
var a=3,b=5,size=999,l=lcm(a,b);
console.log((sum(a,Math.floor(size/a))+sum(b,Math.floor(size/b)))-sum(l,Math.floor(size/l)));
for(var s=0,i=0;i

Here's a lispy iterative solution:
>
(define (sumdiv st nr total)
(if (= st nr)
total
(if (or
(= (modulo st 3) 0)
(= (modulo st 5) 0) )
(sumdiv (1+ st) nr (+ total st))
(sumdiv (1+ st) nr total) ) ) )
(define (sumtox x)
(sumdiv 1 x 0))
>

print(3 + 5 + 6 + 9 + 10 + ...

for i in range(0, 1001):
if i % 3 == 0 or i % 5 == 0:
for num in range(i):
if num % 3 == 0 or num % 5 == 0:
summ = num + i
print(summ)
else:
continue


am i even close, Sup Forums? i'm still new to this

math.stackexchange.com/questions/9259/find-the-sum-of-all-the-multiples-of-3-or-5-below-1000