Created
September 3, 2015 17:49
-
-
Save joriki/756e29e06a55ff274b7d to your computer and use it in GitHub Desktop.
Search for arithmetic progressions in rows of Pascal's triangle -- see http://math.stackexchange.com/questions/1083818.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.math.BigInteger; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Question1083818 { | |
public static void main (String [] args) { | |
List<BigInteger> pascal = new ArrayList<BigInteger> (); | |
pascal.add (BigInteger.ONE); | |
for (int n = 1;;n++) { | |
List<BigInteger> next = new ArrayList<BigInteger> (); | |
for (int k = 0;k <= n;k++) | |
next.add ((k == 0 ? BigInteger.ZERO : pascal.get (k - 1)).add (k == n ? BigInteger.ZERO : pascal.get (k))); | |
pascal = next; | |
for (int i = 0;i + i <= n - 4;i++) | |
for (int j = i + 1,k = i + 2;k + k <= n;k++) { | |
BigInteger sum = pascal.get (i).add (pascal.get (k)); | |
if (!sum.testBit (0)) { | |
sum = sum.shiftRight (1); | |
int comparison; | |
while ((comparison = pascal.get (j).compareTo (sum)) < 0) | |
j++; | |
if (comparison == 0) { | |
System.out.println (n + " : " + i + ", " + j + ", " + k + " : " + pascal.get (i) + ", " + pascal.get (j) + ", " + pascal.get (k)); | |
if (n != 19) { | |
int a = (int) Math.sqrt (n + 4); | |
int diff = a * a - n; | |
boolean known = diff == 2 || diff == 4; | |
if (known) { | |
int a2 = (a * (a - 1)) / 2; | |
known &= i == a2 - diff && j == a2 - diff / 2 && k == a2; | |
} | |
if (!known) | |
throw new Error (); | |
} | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment