Processing not adding numbers correctly?!?

Dear Community

I try to find all multipliers of 3 and 5 below 1000. This is the first objective of the Project Euler. (Project Euler)

But its not working... Even though it should! Here is my code:

int solution = 0;
int number = 1;

void setup() {
  for (int i=0; i<350; i++) {
    if ((number*3)<1000) {
      int help = number*3;
      //println(number*3);
      solution =solution + help;
      help = 0;
    }
    if ((number*5)<1000) {
      int help2 = number*5;
      //println(number*5);
      solution = solution + help2;
      help2 = 0;
    }
    number++;
  }
  println("Solution: "+solution);
}

If I printline the math, its giving out the correct numbers! But the solution 266333 seems not to be correct.

Thanks for your help! SirChregeli

Tagged:

Answers

  • you're adding numbers that are divisible by 15 twice.

  • else if ((number*5)<1000) {
    
  • edited January 2015
    // forum.processing.org/two/discussion/9025/
    // processing-not-adding-numbers-correctly
    
    final int MAX = 1000, MIN = MAX/3 + 1;
    final IntList mult3 = new IntList(), mult5 = new IntList();
    
    int m5, num = 0;
    while (++num != MIN) {
      mult3.append(num*3);
      if ((m5 = num*5) < MAX)  mult5.append(m5);
    }
    
    println("Multiples of 3 below", MAX, ":\n\n", mult3, '\n');
    println("Multiples of 5 below", MAX, ":\n\n", mult5);
    
    exit();
    
  • edited January 2015 Answer ✓
    int i,t=0;for(i=0;i<1000;i++){if(i%3==0||i%5==0)t+=i;}println(t);
    
Sign In or Register to comment.