Posted By

kentoy on 08/09/10


Tagged

Euler Vala


Versions (?)

Vala [Euler Project] - Problem 1


 / Published in: Other
 

  1. save: pep1.vala
  2. compile: valac pep1.vala -o pep1
  3. execute ./pep1
  1. /*
  2. * PROJECT EULER PROBLEM 1
  3. * If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.
  4. * The sum of these multiples is 23.
  5. * Find the sum of all the multiples of 3 or 5 below 1000.
  6. * ans: 233168
  7. */
  8.  
  9. public class Euler.Problem1
  10. {
  11. private const int ceiling = 1000;
  12.  
  13. public Problem1() {
  14. }
  15.  
  16. private int sum_divisible_by(int factor) {
  17. int s = 0;
  18. s = (factor * (((ceiling - 1) / factor) * (((ceiling - 1) / factor) + 1) * 1/2));
  19. return(s);
  20. }
  21.  
  22. private void collect_sum() {
  23. int v = 0;
  24. v = sum_divisible_by(3) + sum_divisible_by(5) - sum_divisible_by(15);
  25. stdout.printf("sum = %d\n".printf(v));
  26. }
  27.  
  28. public static int main(string[] args) {
  29. Euler.Problem1 p = new Euler.Problem1();
  30. p.collect_sum();
  31. return(0);
  32. }
  33. }

Report this snippet  

You need to login to post a comment.