Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Simple Incrementing Problem

Name: Anonymous 2014-03-04 22:59

I don't know exactly what to do in this problem. It seems maddeningly simple, but I'm at a loss.

I want to find all of the multiples of 3 and 5 that are less than 1000, then find their sum.

I've got this:

while (x < 999 && y < 1000):
{
    x += 3;
    y += 5;
}

Where/how can I calculate the sum?

Name: Anonymous 2014-03-04 23:03

oh ya C++ by the way. But really I just want to understand the logic behind the right answer

Name: Anonymous 2014-03-04 23:08

install GNU/TempleOS

Name: Anonymous 2014-03-04 23:09

So it's impossible you're saying

Name: Anonymous 2014-03-04 23:19

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

Name: Anonymous 2014-03-04 23:20

Sorry scratch that above answer.  Didn't read "3 AND 5" thought you meant "OR" for some reason

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

Name: Anonymous 2014-03-04 23:35

>>5,6
You didn't initialize sum.

Name: Anonymous 2014-03-04 23:38

>>5,6
Horrible!

Name: Anonymous 2014-03-04 23:56

int sum = 0;
int i;
int j = 1;
while(true){
 if(i = 1000){
  goto END;
 }
 i = i + 1;
 if(i % 3 == 0 && i % 5 == 0){
  sum = sum + 1;
 }
}
END:
puts(sum);

Am I EXPERT yet?

Name: Anonymous 2014-03-05 0:12

[code]#include <cstdlib>
#include <stdio.h>
#include <vector>
#include <iostream>
#include <time.h>
#include <math.h>
using namespace std;
#ifndef FIVE
#define FIVE 5
#endif
#ifndef THREE
#define THREE 3
#endif
int main(){vector<int> threes; vector<int> fives; int three=0; int five=0;

bool keep3= true, keep5= true;
int sum =0; int lastnumb=0;
for(;;;){
three+=THREE;
five+=FIVE;
keep3 = (three>=1000)? false : keep3;
keep5 = (five<1000)? keep5 : false;
if(keep3)threes.push_back(three);
if(keep5)fives.push_back(five);

if(!(keep3 || keep5)) break; else continue;
}
threes.insert(threes.end(), fives.begin(), fives.end());// thx stackoverflow!
threes.sort(threes.begin(), threes.end());

vector<int>::iterator x;
for(x = threes.begin(); x < threes.end(); x++)
{
if(lastnumb==*x) continue; // lol nope!
else if(lastnumb!=*x){sum+=*x;}
else {try{;} catch(char * error)
     {
          cout << error;
     }
}
cout << sum << "is the sum;";
return 1;
}

Name: Anonymous 2014-03-05 0:14

I MENA
#include <cstdlib>
#include <stdio.h>
#include <vector>
#include <iostream>
#include <time.h>
#include <math.h>
using namespace std;
#ifndef FIVE
#define FIVE 5
#endif
#ifndef THREE
#define THREE 3
#endif
int main(){vector<int> threes; vector<int> fives; int three=0; int five=0;

bool keep3= true, keep5= true;
int sum =0; int lastnumb=0;
for(;;;){
three+=THREE;
five+=FIVE;
keep3 = (three>=1000)? false : keep3;
keep5 = (five<1000)? keep5 : false;
if(keep3)threes.push_back(three);
if(keep5)fives.push_back(five);

if(!(keep3 || keep5)) break; else continue;
}
threes.insert(threes.end(), fives.begin(), fives.end());// thx stackoverflow!
threes.sort(threes.begin(), threes.end());

vector<int>::iterator x;
for(x = threes.begin(); x < threes.end(); x++)
{
if(lastnumb==*x) continue; // lol nope!
else if(lastnumb!=*x){sum+=*x;}
else {try{;} catch(char * error)
     {
          cout << error;
     }
}
cout << sum << "is the sum;";
return 1;
}

Name: Anonymous 2014-03-05 0:19

>>8
What's horrible except the uninitialized sum?

Name: Anonymous 2014-03-05 4:36

>>7
>le pedophile sage

Name: Anonymous 2014-03-05 10:06

>>13
Yes, I am a pedophile. So? I dont see any problem. I embraced my childlover soul long ago and I am happy together with my girlfriend (who is a cute 6 y/o loli!). We fuck a lot of her friends with and without their consent and I am pretty slim and good looking. But thanks anyway asshole. Go and watch your stupid porn with grown women in it while I have SEX with my underaged girlfriend.

Name: Anonymous 2014-03-05 10:40

>>14
>le pedophile sage

Name: Anonymous 2014-03-05 11:16

range{1 1000}.keep{N => (N mod 3) is 0 or (N mod 5) is 0}.sum

Name: Anonymous 2014-03-05 11:39

YA DONT NEED ITERATION

int c = 15; // 3*5
int l = 1000;
int n = l/c;
printf("%d\n", (c*n*(1+n))/2);

Name: Anonymous 2014-03-05 22:17

i think you's are doing it wrong =D


int x=0, y=0, z=0;

while ((x < 999) + (y < 1000)){

  x += 3;

  y += 5;

  z += x * (x<1000) + y * (y<1000);

  }

Name: Anonymous 2014-03-05 22:20



int w=0, x=0, y=0, z=0;

while ((x < 999) + (y < 1000)){

  w += 15;

  x += 3;

  y += 5;

  z += x * (x<1000) + y * (y<1000) - w * (w<1000);

  }

Name: Anonymous 2014-03-05 22:28

+999

Name: Anonymous 2014-03-06 5:13

Can't people oneliner?

int i,sum=0;
for(i=0;i<1000;i++)if(i%15==0)sum+=i;

Name: Anonymous 2014-03-06 5:24

>>21
if dubs this is better:
[code]
int sum = 0;
for (int i = 0; i < 1000; i++)
  if (i % 15 == 0)
    sum += i;
[/code

Name: Anonymous 2014-03-06 5:36

>>22
I swear, I didn't forget the final ``]''

Name: Anonymous 2014-03-06 6:12

brace my anus

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2014-03-06 7:00

>>17
puts("33165");

Name: xpf 2014-03-06 8:13

I'm stuck on problem 3

heres problem 1 in python

sum = 0
for i in range(1, 1000):
    if (i%3 == 0)and(i%5 == 0):
        sum = sum + i
        print(i)
    elif i%5 == 0:
        sum = sum + i
        print(i)
    elif i%3 == 0:
        sum = sum + i
        print(i)
print sum

Name: Anonymous 2014-03-06 11:14

>>21
Even better:

int i=0,sum=0;
for(;i<1000;i++)if(!(i%15))sum+=i;

Name: Anonymous 2014-03-06 14:42

why don't you do it the easy way?

s = sum [x | x <- [0..1000], x `mod` 3 == 0, x `mod` 5 == 0]
main = print s

Name: Anonymous 2014-03-06 18:57

>>28
sum [x | x <- [0..1000], x `mod` 3 == 0 || x `mod` 5 == 0]
I think OP actually wants the sum of all multiples of 3 and/or 5 that are less than 1000, not all multiples of both 3 and 5, otherwise he would have probably said multiples of 15.

Name: Anonymous 2014-03-06 19:05

int cardinality(){
return 1000 / 3 + 1000 / 5 - 1000 / 15;
}


and the sum is something like

int sum(){
return (999 + 3) / 2 * (1000 / 3) + (995 + 5) / 2 * (1000 / 5) - (990 + 15) / 2 * (1000 / 15);
}

Don't change these.
Name: Email:
Entire Thread Thread List