Vala [Euler Project] - Problem 1


/ Published in: Other
Save to your folder(s)

<ol><li>save: pep1.vala</li><li>compile: valac pep1.vala -o pep1</li><li>execute ./pep1</li></ol>


Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.