package margusmartseppcode.From_1_to_9;
public class Problem_6 {
public static void main(String[] args) {
int size = 100;
long qos = (int) Math.pow(size * (size + 1) / 2, 2);
long soq = size * (size + 1) * (2 * size + 1) / 6;
System.out.println(qos - soq);
}
}
Showing posts with label Arithmetic progression. Show all posts
Showing posts with label Arithmetic progression. Show all posts
Tuesday, September 8, 2009
Euler Problem 6 solution
Time (s): ~0.001
Euler Problem 1 solution
Time (s): ~0.001
package margusmartseppcode.From_1_to_9;
public class Problem_1 {
// Arithmetic progression
static int AP(int nr) {
return nr * (nr + 1) / 2;
}
static int nAP(int nr, int multible) {
return multible * nr * (nr + 1) / 2;
}
public static void main(String[] args) {
int size = 999;
int result = nAP(size / 3, 3) + nAP(size / 5, 5)
- nAP(size / 15, 15);
System.out.println(result);
}
}
Subscribe to:
Posts (Atom)