/ Published in: Other

<ol><li>save: pep1.vala</li><li>compile: valac pep1.vala -o pep1</li><li>execute ./pep1</li></ol>
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
/* * PROJECT EULER PROBLEM 1 * If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. * The sum of these multiples is 23. * Find the sum of all the multiples of 3 or 5 below 1000. * ans: 233168 */ public class Euler.Problem1 { private const int ceiling = 1000; public Problem1() { } private int sum_divisible_by(int factor) { int s = 0; s = (factor * (((ceiling - 1) / factor) * (((ceiling - 1) / factor) + 1) * 1/2)); return(s); } private void collect_sum() { int v = 0; v = sum_divisible_by(3) + sum_divisible_by(5) - sum_divisible_by(15); stdout.printf("sum = %d\n".printf(v)); } public static int main(string[] args) { Euler.Problem1 p = new Euler.Problem1(); p.collect_sum(); return(0); } }
Comments
