Outputs:
S(7,3931)=997651 (543 terms) in 85ms
S(7,3931)=997651 (543 terms) in 85ms
1487 4817 8147
2969 6299 9629
in 20ms
...797271283465789498383642350667978127819110846700 in 387msBut a more clever way of computing big numbers can be done here, simply using int or long primitives, by using modulo operations all the time. Modulo operation has the following properties:
a1 = r1 mod band
a2 = r2 mod b
a1+b2 = r1+r2 mod b
a1*b2 = r1*r2 mod b
a1^n = r1^n mod b
131+145=276,
276 mod 100 = 76
131*145=18995,
18995 mod 100 = 95
131^3=2248091,
2248091 mod 100 = 91
131 mod 100 = 31, 145 mod 100 = 45
131+145 mod 100 = ((131 mod 100) + (145 mod 100)) mod 100 = 76
131*145 mod 100 = ((131 mod 100) * (145 mod 100)) mod 100 = 1395 mod 100 = 95
131^3 mod 100 = (((31 mod 100) * (145 mod 100)) mod 100)*31 mod 100 = 91
9110846700 in 88ms
n = 2 * 3 * 5 * 7and we will increment by 1 each time. For each number n we need to extract its factors (such method has already be done in previous problems) and check its size. To extract the factors of a number, we need to have a prime list and check if numbers are prime. To avoid computing primes, a create a Sieve of 100000 primes and extends it in demand. Then i use this prime sieve to extract the factors of the number n.
134043: [3^1, 7^1, 13^1, 491^1]
134044: [2^2, 23^1, 31^1, 47^1]
134045: [5^1, 17^1, 19^1, 83^1]
134046: [2^1, 3^2, 11^1, 677^1]
in 3890ms
(i-p)/2
final long time = currentTimeMillis();
final TIntArrayList squares = new TIntArrayList(1000);
squares.add(new int[]{1, 4, 9, 16, 25, 36, 49, 64});
final TIntArrayList primes = new TIntArrayList(1000);
primes.add(new int[]{2, 3, 5, 7});
for (int i = 9; ; i++) {
squares.add(i * i);
if ((i & 1) == 1) {
if (Maths.isPrime(i, primes)) primes.add(i);
else {
int pos = -1;
for (int pIndex = 1, max = primes.size(), p; pIndex < max && (p = primes.getQuick(pIndex)) < i; pIndex++) {
pos = squares.binarySearch((i - p) >> 1);
if (pos >= 0) {
//out.println(i + "=" + p + "+2*" + squares.getQuick(pos));
break;
}
}
if (pos < 0) {
out.println("Not valid for: " + i + " in " + (currentTimeMillis() - time) + "ms");
return;
}
}
}
}
Not valid for: 5777 in 54ms
final long time = currentTimeMillis();
for (int i = 286; ; i++) {
final long ti = Maths.triangle(i);
final long n = Maths.isHexagonal(ti);
final long m = Maths.isPentagonal(ti);
if (n != -1 && m != -1) {
out.println("T(" + i + ")=P(" + m + ")=H(" + n + ")=" + ti);
out.println("T(" + i + ")=" + Maths.triangle(i));
out.println("P(" + m + ")=" + Maths.pentagonal(m));
out.println("H(" + n + ")=" + Maths.hexagonal(n));
out.println(" in " + (currentTimeMillis() - time) + "ms");
break;
}
}
P(1) = 1
P(n) = P(n-1) + 3n - 2
(sqrt{24*number+1}+1)/6 is a natural number.
public static boolean isPentagonal(int number) {
double test = (sqrt(24 * number + 1) + 1) / 6;
return test == (int) test;
}
v=t_n=0.5*n*(n+1)
0.5*n^2+0.5*n-v=0
n1=(-0.5+sqrt{0.5^2+4*0.5*v})/2*0.5 and n2=(-0.5-sqrt{0.5^2+4*0.5*v})/2*0.5n1=-0.5+sqrt{0.25+2*v} and n2=-0.5-sqrt{0.25+2*v}9+8+7+6+5+4+3+2+1=45
8+7+6+5+4+3+2+1=36
6+5+4+3+2+1=21
5+4+3+2+1=15
3+2+1=6
2+1=3
7+6+5+4+3+2+1=28
4+3+2+1=10
7652413 in 8msI reused isPandigital method and isPrime method from previous problems.
840 : [(210,280,350), (140,336,364), (315,168,357), (252,240,348), (350,120,370), (390,56,394), (399,40,401)] in 13msSource code here !`
192*1=192
192*2=384
192*3 576
9*1=9
9*2=18
9*3=27
9*4=36
9*5=45
10 <= n <= 98
n+1 <= d <= 99
10/10, 10/20, ...
11 <= n <= 98
n+1 <= d <= 99
9*8=72 (4 digits)
9*87=783 (6 digits)
9*876=7884 (8 digits)
9*1234=11106 (10 digits)
8*1234=9872 (9 digits)
98*76=7448 (8 digits)
98*765=74970 (10 digits)
12*345=4140 (9 digits)
200=200*a+100*b+50*c+20*d+10*e+5*f+2*g+1*h