[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Find the sum of all the multiples of 3 or 5 below 1000 or this

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 169
Thread images: 17

File: 1487388387377.jpg (6KB, 239x250px)
1487388387377.jpg
6KB, 239x250px
Find the sum of all the multiples of 3 or 5 below 1000 or this bird will stab you.
>>
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)))
>>
>>59753337
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)))
>>
>>59753337
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.
https://pastebin.com/139TJdya
>>
>>59753337
do your own homework you fucking FAGGOT
>>
>>59753337
Remake of FizzBuzz. You won't fool me.
>>
Prelude> sum $ filter (\x -> x `mod` 3 == 0 || x `mod` 5 == 0) [1..9999]
23331668
>>
>>59754040
Oh, below 1000, not 10000
>>
>>59753337
$ awk 'BEGIN{sum=0;for(i=1;i<1000;i++)if(!(i%3)||!(i%5))sum+=i;printf("%d\n", sum);}'
233168
>>
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
>>
>>59754359
wat languge be dat
>>
>>59754404
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
>>
>>59753337
Do your homework yourself, pajeet
>>
int i = 3+5+6+9+10+...+999+1000
return i
>>
File: 1490548507278.png (261KB, 1048x1024px) Image search: [Google]
1490548507278.png
261KB, 1048x1024px
>>59754322
Neat.

Bash.
n=;for((i=0;i<1000;i++));{ [[ $[i%3] = 0||$[i%5] = 0 ]]&&n+=+$i;};echo $[n]
>>
#include <stdio.h>
#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
>>
File: 562.gif (106KB, 450x258px) Image search: [Google]
562.gif
106KB, 450x258px
>>59753337
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 << sum << std::endl;
return 0;
}

>>
>>59754782
First good answer.
>>
>>59754782

my nigga
>>
>>59753337
puts ((3..999).step(3).to_a|(5..995).step(5).to_a).reduce(:+)
>>
>>59754385
Sum too high. You're adding the intersection between both ranges twice now.
>>
Aperantly, im to smart and riched the limit of 4chan length.

here is my code
https://pastebin.com/CCQg5H8c
>>
>>59754040
dude my solution was exactly the same. will you be my friendo?
>>
>>59753561
>Using iteration
Absolutely disgusting
>>
>>59754385
"- sum (15:15:999)"
(i am not familiar with syntax, following yours)
>>
>>59755287
Yes.
>>
>>59753576
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)}
>>
File: Loops1.png (100KB, 646x422px) Image search: [Google]
Loops1.png
100KB, 646x422px
count = 0;
for(int i = 0; i <= 1000; i++){
if(i % 3 == 0)
count += i;
else if(i % 5 == 0)
count += i;
else
count += 0;
}

cout << "The count is " << count;


Your well cum, OP.
>>
>>59754782
that's pretty smart

good job anon
>>
>>59753337
#include <stdio.h>
#include <stdlib.h>

/* 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 <= 1000; ++i) {
if((i % 3 == 0) || (i % 5 == 0)) a += i;
}
/* doesn't work on Windows because their C library is broken */
printf("The sum of all positive integer multiples of 3 or 5 below 1000 is %ulld.\n", a);
return EXIT_SUCCESS;
}
>>
>>59754858
>unnecessary braces
disgusting.
>>
>>59753337
use strict;
use warnings;
use v5.10;
use List::Util qw(sum);

say sum(map { $_ if $_ % 3 == 0 or $_ % 5 == 0 } (1..999));
>>
>>59753458
>reduce(lambda x, y: x + y,
2cool4sum
end your life my man

erlang
lists:sum([X+Y || X <- lists:seq(0, 999, 3), Y <- lists:seq(0, 999, 5)]).
>>
>>59753337
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)
>>
>>59754040
Not using `rem` instead of `mod`
>>
>>59754040
sum [x | x<-[3,6..1000], mod x 5 == 0]
square up senpai
>>
File: duke.jpg (17KB, 480x360px) Image search: [Google]
duke.jpg
17KB, 480x360px
https://pastebin.com/13n3CA6U
>>
Why the fuck are all you brainlets iterating over 1000 numbers except for

>>59754782

Think before you code. Pic related from when I first saw the thread
>>
>>59758887
why think when the compiler does it for you
>>
>>59754439
Does this actually work? That's so few lines.
>>
>>59753337
#include <stdio.h>

int main(void)
{
for(int i = 3; i < 1000; i++)
{
if(!(i%3 && i%5))
printf("%d\n", i);
}
}
>>
>>59759027
yes, result will be in register ebp. loops down from 999, 2 divisions per iteration
>>
>>59759067
aw shit nibba, forgot the main goal
#include <stdio.h>

int main(void)
{
for(int i = 3; i < 1000; i++)
{
if(!(i%3 && i%5))
printf("%d\n", i);
}
}
>>
>>59759106
and also to copy the new code...
#include <stdio.h>

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);
}
>>
>>59753337
total = 0
for i in range(0, 1001):
if i % 3 == 0 or i % 5 ==0:
total += i
print(total)
[/code
>>
>>59753337
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 <-[1..1000], x `mod` 5 == 0 || x `mod` 3 == 0]
>>
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
>>
>>59753337
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;
}
>>
>>59758887
This is wrong. The correct answer is 233168. It says BELOW 1000
>>
>>59753877
I'm not even angry at this point.
>>
#include<bits/stdc++.h>
using namespace std;
int main() {
int n=1000;
int sum=0;
for (int i=1; i<n; i++) {
if (i%3==0 or i%5==0) {
sum+=i;
}
}
cout << "The sum of all integers below " << n << " dividable by 3 or 5 is "
<< sum << '\n';
}
>>
>>59754626
Learn to ((
>>
>>59753337
(loop for i below 1000 when (or (= 0 (mod i 3)) (= 0 (mod i 5))) sum i)
>>
here u go op
MSGBOX("233168")
>>
>>59753337
3*(333*334/2) + 5*(199*200/2) - 15*(66*67/2) = 233168
>>
>>59759197
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

>>59760598's
if (i%3==0 or i%5==0)
>>
>>59760599
Educate me. How would you write it?
>>
>>59760263
Go to sleep Gramps.
>>
>>59753337
sum({3*i for i in xrange(1, 334)} | {5*i for i in xrange(1, 200)})
>>
>>59760973
>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
>>
>>59761511

>>59754782
>>
>>59755363
>recursion
>>
There are infinite numbers below 1000
>>
>>59762343
integers motherfucker, do you speak it?

No one is talking about floats or irrational numbers
>>
>>59762546
The other anon 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.
>>
>>59762546
Yes I do speak it. Have you ever heard of negative numbers? Many of which are integers and divisible by 3 or 5?
>>
>>59762608
>>59762601

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.
>>
>>59762343
Yes, but not infinite multiples of 3 or 5.
>>
>>59754782
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
>>
File: IMG_20170406_010609.jpg (2MB, 4048x3036px) Image search: [Google]
IMG_20170406_010609.jpg
2MB, 4048x3036px
This attempt is stupid but whatever...
>>
>>59763021
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
>>59754782
>>
>>59756050
>else count += 0
really makes your neurons fire
>>
>>59753337
for(var s=0,i=0;i<1000;i++){if(i%3==0||i%5==0){s+=i;}}console.log(s);
>>
>>59754782
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++);
>>
>>59763680
Is still shorter than
>>59763809
>>
>>59763827
But it's a single statement. I rarely see people do this with for loops here.
>>
>>59763839
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
>>
File: dis.png (11KB, 478x139px) Image search: [Google]
dis.png
11KB, 478x139px
>>59764055
>>
>>59753337

n = floor(1000 / 15);
n+1 * 3,5,6,9,10,12,15 + sum(1 to n)*15*7
+ 3 iif 1000 <= n*15 +3
+ 5 iif 1000 <= n*15 +5
..
>>
>>59764200
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<=size;i++){if(i%a==0||i%b==0){s+=i;}}console.log(s);
>>
>>59753337
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))
>
>>
>>59753561
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, /g/? i'm still new to this
>>
>>59753337
http://math.stackexchange.com/questions/9259/find-the-sum-of-all-the-multiples-of-3-or-5-below-1000
>>
>>59753337
def sum(maxint): 
s = 0
for i in range(maxint):
if i % 3 == 0 or i % 5 == 0:
s += i
return s

print(sum(1000))

Too easy, birdmin.
>>
File: stabbypupper.jpg (41KB, 513x307px) Image search: [Google]
stabbypupper.jpg
41KB, 513x307px
>>59753337
fn main() {
let sum: u64 = (0..1000).filter(|x| x % 3 == 0 || x % 5 == 0).sum();
println!("{}", sum);
}
>>
>>59766098
what language is this?
>>
>>59766204
Rust
>>
>>59766211
I see..that's some nice syntactic sugar there.
>>
>>59753337

Dirty JS hack coming in..

function fb(a) {return a%3==0||a%5==0}
function sum(a,b) {return a+b}

s = Array
.apply(null, {length: 1000})
.map(Number.call, Number)
.filter(fb)
.reduce(sum);
>>
>>59753458
> hurr durr check out my fuctional programming

get the FUCk out of here you little twink cuck faggot. this is a board for MEN
>>
>>59766302
OOL NI OOP
>>
>>59766098

It's astonishing how Rust looks like Ruby..

(0..1000).select{|x| x % 3 == 0 || x % 5 == 0}.reduce(:+)
>>
>>59766302
hi pajeet. sorry to hear about your H1B going away.
>>
>>59766318
The syntax for closures and ranges definitely look the same.
Apart from that there is not much more similarity than with other languages.
>>
>>59766411

Well, Rust also has this "function!()" syntax, but AFAIK the differnce is:
In Ruby, a functions with a quotation mark has side effects or works destructively, whereas in Rust this is used for indicating macros.


Here is a beautiful Ruby solution, BTW:
a = (0..999).step(3).to_a
b = (0..999).step(5).to_a

p (a+b).uniq.reduce(:+)
>>
>>59766501

Sorry, I meant "exclamation mark".
>>
>>59766302
JUST

for men
>>
s(0, 0) :- !.
s(M, N) :- divisible(M), Y is M - 1, s(Y, X), N is X + M,!.
s(M, N) :- X is M - 1, s(X, N).

divisible(M) :- 0 is mod(M, 5).
divisible(M) :- 0 is mod(M, 3).
>>
>>59766501
(reduce '+ (union (loop for x below 1000 by 3 collect x)
(loop for x below 1000 by 5 collect x)))
>>
>>59753337
At least come up with a question that actually needs some coding to solve not just math, you stupid bird
3*sum(range(334))+5*sum(range(200))-15*sum(range(67))
>>
>>59755287
I really hope who ever made that used a script to generate that file. :/
>>
>tfw procrastinating actual work

public class shit {
public static void main(String[] args){
int sum = 0;
for(int i = 0; i < 1000; i++){
if((i % 3 == 0) || (i % 5 == 0)){
sum = sum + i;
}
}
System.out.println(sum);
}
}
>>
>>59753458

sum([x for x in range(1000) if x % 3 == 0 or x % 5 == 0])
>>
I tried to do it without code, but got it wrong some how...

3 999 1002
6 996 1002

1002 * ( 999 / 3 / 2)
>166833

5 1000 1005
10 995 1005

1005 * ( 1000 / 5 / 2 )
>100500

267500

15 990 1005
30 975 1005

1005 * ( 990 / 15 / 2 )
>33165


>166833 + 100500 - 33165
>>234.168
>>
>>59766954

What language is this? CL?

Here's my cool Java approach:

class Main {
public static void main(String[] args){

int i = 3;
int j = 5;
int sum = 0;

while (i<1000 || j<1000) {
if (i<j) {
sum += i;
i += 3;
} else if (j<i) {
sum += j;
j += 5;
} else {
sum += i;
i += 3;
j += 5;
}
}

System.out.println(sum);
}
}
>>
>>59767532
>CL?
Yep.
>>
>>59753337
We're not doing your homework, you fucking faggot.

>tfw hundreds of posts doing OP's arbitrary CS homework
>>
>>59767561
Is this CS homework? Surely there isn't a university that makes people do stuff this basic to begin with.
>>
>>59767561
Shitty homework assignment, if you ask me. Don't worry, if OP can't handle something like this, there is no way they'll get much further.
>>
>>59753337
this is essentially just fizzbuzz but worded in supermaths

error: compiler refuses to do your fucking homework


>>
>>59767561
This is literally the first question on Project Euler
>>
>>59753337
threes = [3, 6..]
fives = [5, 10...]
sums = zipWith (+) threes fives
answer = take 1000 sums


is this ok?
>>
>>59767581
Even a CS major has to start with the basics, even if they won't stay there for long. Or do universities in the US expect people to actually know programming beforehand?
>>
>>59767542

SICP is definately on my list of things I want to go through this year.. I tried it before, but stopped after a few dozen of pages because I didn't have the time.

Meanwhile I'm practicing my mediocre skills in Java, JavaScript and Ruby..

>4chan: your personal blog
>>
>>59767687
If it makes you feel any better, I never read that book. Learn by doing.
>>
File: brd.gif (122KB, 500x281px)
brd.gif
122KB, 500x281px
>>59767561
He didn't ask us to code anything though. The.. ehm.. bird maybe just wants the answer. Everyone here is just assuming it wants code for some reason. What is a bird going to do with code?
>>
>>59767780
OP here
Thanks guys for doing all my homework
>>
>>59767793
>all homework
Only one question? Where's the rest?

I'm bored.
>>
>>59767780
We just don't know.
>>
>>59767775
Doing people's homework is my hobby though.
>>
File: feels_good_man.png (53KB, 453x244px) Image search: [Google]
feels_good_man.png
53KB, 453x244px
>>59767775

You're welcome.

>tfw when it's "procrastinating with birdmin" time..
>>
>>59767775
>>59767775
If he was not able to do a simple exercise, what will he do with so many solutions written in 10+ different languages?
Also here is th recursive solution from >>59764286
>
(define (recsumd a n total P x y)
(if (= a n)
total
(if (P a x y)
(+ a (recsumd (1+ a) n total P x y))
(recsumd (1+ a) n total P x y) )))
(define (sumd n)
(recsumd 1 n 0 (lambda (a x y) (or (= (modulo a x) 0)
(= (modulo a y) 0) ) )
3 5 ) )
>
>>
File: 1464243506168.png (398KB, 595x571px) Image search: [Google]
1464243506168.png
398KB, 595x571px
>>59768355
Fix
#include <stdio.h>

/* Sum of multiples of I below B */
#define SUM_BELOW(I,B) ((I * (B/I)*(B/I + 1)) / 2)

int main(int argc, char** argv)
{
unsigned int result = SUM_BELOW(3, 1000) + SUM_BELOW(5, 1000) - SUM_BELOW(15, 1000);
printf("Result is %u\n", result);
return 0;
}
>>
>>59762546
There are finite floats below 1000.
>>
#consider <FEMINIST_RAGE.Xir>

xe womain (xe RequestCount, strong *RequestList[]) {

//Always check your privilege
CheckPrivilege();

// subtle mental tyranny for what it truly is
xe ArbitraryBeginning accepts(present(-50));
xe ArbitraryEnd accepts(present(50));

// naturally, everything revolves around this
xe ThePlaceBetween accepts(present(0));

among(ThePlaceBetween accepts(ArbitraryBeginning),
ThePlaceBetween honors(ArbitraryEnd),
ThePlaceBetween improvesBy(present(1))) {

check(ThePlaceBetween envelops(present(3))) {
yell(present(1));
ENDMISOGYNY

recheck(ThePlaceBetween envelops(present(5))) {
yell(present(1));
ENDMISOGYNY

unpack {
// strength & independence!
yell(present(ThePlaceBetween));
ENDMISOGYNY;

yell(present("\n"));
ENDMISOGYNY;

present(Satisfaction);

ENDMISOGYNY;
>>
>>59769252

Classy.
>>
>>59753337
>System.out.println(muhammadsClass.getSum(3,5,1000));
Done
>>
File: zulwi fuckers.gif (628KB, 320x180px)
zulwi fuckers.gif
628KB, 320x180px
>>59753337
var 3[1000] = "";
var 5[1000] = "";
for(x = 0; x++; x > 1000)
{
if(x/3%){
3[x] = x;
}

if(x/5%){
5[x] = x;
}
}
>>
sum(num for num in range(1000+1) if  num % 3 == 0 or num % 5 == 0)
>>
File: 1488398991660.gif (2MB, 311x362px) Image search: [Google]
1488398991660.gif
2MB, 311x362px
sum([x for x in range(1000) if x % 3 == 0 or x % 5 == 0])


EZPZ pls no stab
>>
>>59771762
He said BELOW 1000

YOU GET STABBED
>>
>>59753337

Two sums of two arithmetic progressions minus sum of third arithmetic progression.
>>
>>59753337
https://www.google.ca/search?q=what+is+the+sum+of+all+multiples+of+3+and+5+below+1000&oq=what+is+the+sum+of+all+multiples+of+3&aqs=chrome.1.69i57j0l5.9590j0j7&sourceid=chrome&ie=UTF-8
>>
>>59772131
sum(filter(lambda n:(n%3==0 or n%5==0), range(1,1001)))

git gud nigga
>>
>>59772181
oh fuck replace 1001 with 1000 in >>59772363
>>
>>59760649
(loop for n below 1000
if (or (= (mod n 3) 0)
(= (mod n 5) 0))
sum i)
>>
>>59772374
No u

>>59772363
What's the benefit of using two functions instead of one list comprehension
>>
>>59756784
I always do like this x) but it's true : braces --> useless
>>
>>59772391
filter is lazy so if you don't actually sum everything it's much, much, much faster. see screenshot for a shitty benchmark example
>>
>>59772520
Huh, well there you go, I learned something new.

Cheers m8
>>
>>59772580
yep. it's pretty cool. the functional tools in python are neat little gems that are definitely worth exploring and playing around with. in some cases it's not any better than a list comprehension (and bear in mind that the primary value of python is readability, so if you're taking a huge hit on how comprehensible the code is then it might not be worth it), but sometimes it makes it easier to provide a really neat little solution to a whole iterative thing that's a pain in the ass.
>>
>O(1) solutions already posted
>O(n) keep getting suggested
fucking hell, /g/
>>
5*200*201 + 3*333*334
>>
>>59772788
You count the multiples of 15 twice.
>>
>>59767775
Consider a hobby instead of autistically replying to people who don't give a fuck about your autism
>>
>all these people iterating over 1000 numbers
jesus christ /g/
it really is full of codemonkeys
>>
>>59754782
can someone explain this solution more please?
>>
int total = 0;
for(int i = 0; i < 1000; i++){
if(i % 3 == 0)
total += i;
if(i % 5 == 0)
total += i;
}
cout << total;
>>
>>59761020
for i in {0..1000}; do (( (i%3 == 0 || i%5 == 0) && (n += i) )); done
>>
>>59754626
>>59774089
Also don't use $[], it was deprecated in in the early 90s in favor of $(()).
>>
>>59753337
sum([x for x in range(1001) if x % 3 == 0 or x % 5 == 0]
fuck you I'll use python if I want
>>
>>59774089
Thanks. Check my result:
for((i=1000;i>0;i--));{(((i%3&&i%5)||(n+=i)));};echo $n
>>
>>59773784
Any number of the form 3N is divisible by 3. The question asks for the sum of divisors less than 1000, so that would be (3 + 6 + 9 + ... + 999). The 3 can be factored out to obtain 3*(1 + 2 + 3 ... + 333). This summation have a closed form solution of 3n(n+1)/2 where n = 333. The same method can be used to obtain the sum of all numbers divisible by 5 below 1000, which gives the formula 5n(n+1)/2 where n = 199.

Adding these two numbers together will provide the sum of all numbers divisible by 3 or 5 below 100. But numbers that are divisible by both 3 and 5 have been counted twice. Numbers that are divisible by both 3 and 5 will have the form 3*5N so we just need to subtract the sum of all numbers divisible by 15 using the same method.
Thread posts: 169
Thread images: 17


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.