Skip to content

Instantly share code, notes, and snippets.

@tmathmeyer
Last active December 19, 2015 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tmathmeyer/5884634 to your computer and use it in GitHub Desktop.
Save tmathmeyer/5884634 to your computer and use it in GitHub Desktop.
works, but takes abt 7 days
package edu.wpi.tmathmeyer.euler;
public class Problem433 {
int[][] mem = new int[50000][];
int size = 500000;
public static void main(String[] args){
new Problem433().problem();
}
public void problem(){
for(int i = 0; i < mem.length; i++)
{
mem[i] = new int[i+1];
}
long[] vals = new long[5000000];
int position = 0;
System.out.println("starting");
for(int i = 1; i <= size; i++)
{
for(int j = i; j <= size; j++)
{
vals[position] += gcdCount(i,j);
if (vals[position] > 9000000000000l)
{
position++;
}
}
if (i%1000==0)
{
System.out.println(((double)i * 100)/size+"% ");
}
}
System.out.println("done!");
long sum = (size*size - size)/2 - size;
//long sum = 0l;
for(int i = 0; i < vals.length; i++){
if (vals[i] == 0)
{
i = 500000000;
}
else
{
System.out.println(sum+=(vals[i]*2));
}
}
}
public int gcdCount(int a, int b){
if (a > b)
{
return 1 + gcdCount(b, a);
}
b = b%a+a;
if (b % a == 0)
{
if (a<mem.length && b<mem.length && mem[b][a]==0)
{
mem[b][a] = 1;
}
return 1;
}
if (a<mem.length && b<mem.length)
{
if (mem[b][a] != 0)
{
return mem[b][a];
}
else {
mem[b][a] = 1 + gcdCount(b%a, a);
return mem[b][a];
}
}
else
{
return 1 + gcdCount(b%a, a);
}
}
public int isMemioized(int a, int b)
{
if (b > a)
{
return isMemioized(b, a);
}
return mem[a][b];
}
public void printMem(){
for(int i = 0; i < mem.length; i++)
{
for(int j = i; j < mem.length; j++)
{
System.out.print(mem[j][i]);
}
System.out.println();
}
}
public long rowSum(int size, int i, long tempVals, int[] p){
long blockA = (size+1)/i - 1;
long blockB = sigma(p, 0, i);
long blockC = sigma(p, 0, (size + 1)%i);
return blockA*blockB + blockC + tempVals;
}
public long sigma(int[] p, int low, int high){
long result = 0l;
for(int i = low; i < high; i++)
{
result+=p[i];
}
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment