<repo_name>chiranthsiddappa/ProjectEuler<file_sep>/Problem34.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projecteuler;

/**
 *
 * @author Chiranth
 */
/**
 * 145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145.
 *
 * Find the sum of all numbers which are equal to the sum of the factorial of
 * their digits.
 *
 * Note: as 1! = 1 and 2! = 2 are not sums they are not included.
 *
 * @author Chiranth
 */
public class Problem34 {

    public static long isEqualSum(int upperBound) {
        long factorialSum = 0, sum = 0, digitFactSum = 0;
        for (long i = 3; i <= upperBound; i++) {
            if (digitsFactorialSum(i) == i) {
                factorialSum += i;
            }
        }
        return factorialSum;
    }

    public static long digitsFactorialSum(long num) {
        long sum = 0;
        int nextDigit = 0, limit = length(num);
        for (int i = 0; i < limit; i++) {
            nextDigit = (int) num % 10;
            sum += factorial(nextDigit);
            num -= nextDigit;
            num /= 10;
        }
        return sum;
    }

    public static int length(double x) {
        return (int) java.lang.Math.floor(java.lang.Math.log10(x) + 1);
    }

    public static long factorial(int n) {
        long factorial = 1;
        for (int i = 1; i <= n; i++) {
            factorial *= i;
        }
        return factorial;
    }

    public static void main(String args[]) {
        //
        System.out.println(isEqualSum(100000));
    }
}
<file_sep>/Problem43.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projecteuler;

import java.lang.Math;

/**
 *
 * @author chiranth
 */
/**
 * The number, 1406357289, is a 0 to 9 pandigital number because it is made up
 * of each of the digits 0 to 9 in some order, but it also has a rather
 * interesting sub-string divisibility property.
 *
 * Let d1 be the 1st digit, d2 be the 2nd digit, and so on. In this way, we note
 * the following: d2d3d4=406 is divisible by 2 d3d4d5=063 is divisible by 3
 * d4d5d6=635 is divisible by 5 d5d6d7=357 is divisible by 7 d6d7d8=572 is
 * divisible by 11 d7d8d9=728 is divisible by 13 d8d9d10=289 is divisible by 17
 *
 * Find the sum of all 0 to 9 pandigital numbers with this property.
 */
public class Problem43 {

    public static int length(long x) {
        return (int) Math.floor(Math.log10(x) + 1);
    }

    public static String replaceChar(String str, int index, char replace) {
        if (str == null) {
            return str;
        } else if (index < 0 || index >= str.length()) {
            return str;
        }
        char[] chars = str.toCharArray();
        chars[index] = replace;
        return String.valueOf(chars);
    }

    public static long lexicographicPermutationSum(String string) {
        String tempS = "", reverse = "", temp = string;
        char tempC = '\n';
        boolean foundAk = false, foundAl = false;
        int nPermutations = factorial(string.length()), permutation = 0;
        int ak = 0, al = 0;
        long sum = 0;
        //First permutation is the starting string
        temp = string; // make a copy for the first combination
        do {
            if (hasDivisibilityProperties(temp)) {
                sum += Long.parseLong(temp);
            }
            //reset variables
            foundAk = false;
            foundAl = false;
            //Step 1 a[k] < a[k + 1]
            for (int k = 0; k < temp.length() - 1; k++) {
                if (temp.charAt(k) < temp.charAt(k + 1)) {
                    ak = k;
                    foundAk = true;
                }
            }
            //Step 2 find a[k] < a[l], if another perm exists
            if (foundAk) {
                for (int l = ak + 1; l < string.length(); l++) {
                    if (temp.charAt(l) > temp.charAt(ak)) {
                        al = l;
                    }
                }
                //Step 3 swap a[k] with a[l]
                tempC = temp.charAt(ak);
                tempS = replaceChar(temp, ak, temp.charAt(al));
                tempS = replaceChar(tempS, al, tempC);
                //Step 4 reverse the sequence from a[k+1], switch the numbers backwards
                reverse = tempS.substring(ak + 1);
                //Still needs work...
                for (int charNext = ak + 1, charRev = reverse.length() - 1; charNext < tempS.length() && charRev >= 0; charNext++, charRev--) {
                    tempS = replaceChar(tempS, charNext, reverse.charAt(charRev));
                }
                temp = tempS;
            }
        } while (foundAk && permutation < nPermutations);
        return sum;
    }

    public static int factorial(int n) {
        int factorial = 1;
        for (int i = 1; i <= n; i++) {
            factorial *= i;
        }
        return factorial;
    }

    public static boolean hasDivisibilityProperties(String x) {
        boolean d234by2, d345by3, d456by5, d567by7, d678by11, d789by13,
                d8910by17;
        d234by2 = Long.parseLong(x.substring(1, 4)) % 2 == 0;
        d345by3 = Long.parseLong(x.substring(2, 5)) % 3 == 0;
        d456by5 = Long.parseLong(x.substring(3, 6)) % 5 == 0;
        d567by7 = Long.parseLong(x.substring(4, 7)) % 7 == 0;
        d678by11 = Long.parseLong(x.substring(5, 8)) % 11 == 0;
        d789by13 = Long.parseLong(x.substring(6, 9)) % 13 == 0;
        d8910by17 = Long.parseLong(x.substring(7, 10)) % 17 == 0;
        if (d234by2 && d345by3 && d456by5 && d567by7 && d678by11 && d789by13 && d8910by17) {
            return true;
        } else {
            return false;
        }
    }

    public static void main(String[] args) {
        long sum = lexicographicPermutationSum("1023456789");
        System.out.println(sum);
    }
}
<file_sep>/Problem44.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projecteuler;

import java.lang.Math;
import java.util.Arrays;

/**
 *
 * @author chiranth
 */
/**
 * Pentagonal numbers are generated by the formula, Pn=n(3n-1)/2. The first ten
 * pentagonal numbers are:
 *
 * 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ...
 *
 * It can be seen that P4 + P7 = 22 + 70 = 92 = P8. However, their difference,
 * 70 22 = 48, is not pentagonal.
 *
 * Find the pair of pentagonal numbers, Pj and Pk, for which their sum and
 * difference is pentagonal and D = |Pk Pj| is minimised; what is the value of
 * D?
 * 
 * Solving Pn=n(3n-1)/2 produces sqrt(1 + 24*n) % 6 == 5 % 6
 */
public class Problem44 {
    
    public static int Pn(int n) {
        return n*(3*n - 1) / 2;
    }
    
    public static boolean isPentagonal(int x) {
        return Math.sqrt(24*x + 1) % 6 == 5;
    }
    
    public static int[] push(int[] current, int newInt) {
        int[] newStack = Arrays.copyOf(current, current.length + 1);
        newStack[current.length] = newInt;
        return newStack;
    }
    
    public static int pentagonalDifference() {
        int D = 0;
        int Pk = 0;
        int Pj = 0;
        int tempP = 0;
        int[] pentagonals = new int[1];
        pentagonals[0] = 0;
        boolean pentagonalDFound = false;
        //Calculate D with Math.abs(Pk - Pj), two for loops
        for(Pk = 1; !pentagonalDFound; Pk = Pj) {
            tempP = Pn(Pk);
            pentagonals = push(pentagonals, tempP);
            // test the values just in case...
            for(Pj = 1; Pj <= Pk && !pentagonalDFound; Pj++) {
                D = Math.abs(pentagonals[Pk] - pentagonals[Pj]);
                pentagonalDFound = isPentagonal(pentagonals[Pk] + pentagonals[Pj]) &&
                        isPentagonal(D);
            }
        }
        return D;
    }
    
    public static void main(String[] args) {
        System.out.println(pentagonalDifference());
    }
}
<file_sep>/Problem61.rb
aTri = []
aSquare = []
aPent = []
aHex = []
aHept = []
aOct = []
cyclicals = []
perms = []

def triangle(n, aTri)
  if (aTri.length() - 1) < n
    for i in (aTri.length())..(n)
      aTri.push i*(i + 1) / 2
    end
    return aTri[n]
  else
    return aTri[n]
  end
end

def square(n, aSquare)
  if (aSquare.length() - 1) < n
    for i in (aSquare.length())..(n)
      aSquare.push i**2
    end
    return aSquare[n]
  else
    return aSquare[n]
  end
end

def pentagonal(n, aPent)
  if (aPent.length() - 1) < n
    for i in (aPent.length())..(n)
      aPent.push i*(3*i - 1)/2
    end
    return aPent[n]
  else
    return aPent[n]
  end
end

def hexagonal(n, aHex)
  if (aHex.length() -1) < n
    for i in (aHex.length())..(n)
      aHex.push i*(2*i - 1)
    end
    return aHex[n]
  else
    return aHex[n]
  end
end

def heptagonal(n, aHept)
  if (aHept.length() -1) < n
    for i in (aHept.length())..(n)
      aHept.push i*(5*i - 3)/2
    end
    return aHept[n]
  else
    return aHept[n]
  end
end

def octagonal(n, aOct)
  if (aOct.length() - 1) < n
    for i in (aOct.length())..(n)
      aOct.push i*(3*i - 2)
    end
    return aOct[n]
  else
    return aOct[n]
  end
end

def next_triangle(aTri)
  triangle(aTri.length, aTri)
end

def next_square(aSquare)
  square(aSquare.length, aSquare)
end

def next_pentagonal(aPent)
  pentagonal(aPent.length, aPent)
end

def next_hexagonal(aHex)
  hexagonal(aHex.length, aHex)
end

def next_heptagonal(aHept)
  heptagonal(aHept.length, aHept)
end

def next_octagonal(aOct)
  octagonal(aOct.length, aOct)
end


def isCyclic?(set)
  valid = true
  curr_digits = []
  curr_string = ""
  for i in 0..set.length() - 2
    curr_string = set[i].to_s
    curr_digits[0] = curr_string[curr_string.length - 1]
    curr_digits[1] = curr_string[curr_string.length - 2]
    return false if ((curr_digits[0] != set[i+1].to_s[0] or curr_digits[1] != set[i+1].to_s[1]) and (curr_digits[0] != set[i+1].to_s[1] or curr_digits[1] != set[i+1].to_s[0]))
  end
  curr_string = set[set.length() -1].to_s
  curr_digits[0] = curr_string[curr_string.length - 1]
  curr_digits[1] = curr_string[curr_string.length - 2]
  return ((curr_digits[0] == set[0].to_s[0] and curr_digits[1] == set[0].to_s[1]) or (curr_digits[0] == set[0].to_s[1] and curr_digits[1] == set[0].to_s[0]))
end

def lex_perm(a)
  permutations = []
  begin
    k = -1
    l = -1
    index = 0
    permutations.push []
    a.each {|int| permutations[permutations.length() -1].push int}
    for i in 0..(a.length - 2) do
      k = i if a[i] < a[i + 1]
    end
    for i in 0..(a.length - 1) do
      l = i if a[k] < a[i]
    end
    if k >= 0
      a[k],a[l] = a[l],a[k]
      a[k+1, a.length-1] = a[k+1, a.length-1].reverse
    end
  end while k >= 0
  return permutations
end

puts isCyclic?([8128, 2882, 8281])
for i in 1..8
  perms.push lex_perm((0..(i -1)).to_a)
end
puts "Perm array created."
<file_sep>/Problem17.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projecteuler;

/**
 *
 * @author Chiranth
 */

/*
 * If the numbers 1 to 5 are written out in words: one, two, three, four, five,
 * then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
 *
 * If all the numbers from 1 to 1000 (one thousand) inclusive were written out
 * in words, how many letters would be used? *
 *
 * NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and
 * forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20
 * letters. The use of "and" when writing out numbers is in compliance with
 * British usage.
 * 
*/
public class Problem17 {

    public static int digits(int n) {
        int i;
        int mod;
        for (i = 0; n > 0; i++) {
            mod = n % 10;
            n -= mod;
            n /= 10;
        }
        return i;
    }

    public static String stringOfNumbersInWords(int n) {
        int i;
        String stringOfNumbers = "";
        for (i = 1; i <= n; i++) {
            stringOfNumbers += stringOfNumber(i);
        }
        return stringOfNumbers;
    }

    public static String stringOfNumber(int n) {
        if (n < 10) {
            if (n == 1) {
                return "one";
            } else if (n == 2) {
                return "two";
            } else if (n == 3) {
                return "three";
            } else if (n == 4) {
                return "four";
            } else if (n == 5) {
                return "five";
            } else if (n == 6) {
                return "six";
            } else if (n == 7) {
                return "seven";
            } else if (n == 8) {
                return "eight";
            } else if (n == 9) {
                return "nine";
            }
        } else if (n >= 10 && n < 100) {
            if (n >= 10 && n < 20) {
                if (n == 10) {
                    return "ten";
                } else if (n == 11) {
                    return "eleven";
                } else if (n == 12) {
                    return "twelve";
                } else if (n == 13) {
                    return "thirteen";
                } else if (n == 14) {
                    return stringOfNumber(4) + "teen";
                } else if (n == 15) {
                    return "fifteen";
                } else if (n == 16) {
                    return stringOfNumber(6) + "teen";
                } else if (n == 17) {
                    return stringOfNumber(7) + "teen";
                } else if (n == 18) {
                    return stringOfNumber(8) + "een";
                } else if (n == 19) {
                    return stringOfNumber(9) + "teen";
                }
            } else if (n >= 20 && n < 30) {
                return "twenty" + stringOfNumber(n % 10);
            } else if (n >= 30 && n < 40) {
                return "thirty" + stringOfNumber(n % 10);
            } else if (n >= 40 && n < 50) {
                return "forty" + stringOfNumber(n % 10);
            } else if (n >= 50 && n < 60) {
                return "fifty" + stringOfNumber(n % 10);
            } else if (n >= 60 && n < 70) {
                return "sixty" + stringOfNumber(n % 10);
            } else if (n >= 70 && n < 80) {
                return "seventy" + stringOfNumber(n % 10);
            } else if (n >= 80 && n < 90) {
                return "eighty" + stringOfNumber(n % 10);
            } else if (n >= 90 && n < 100) {
                return "ninety" + stringOfNumber(n % 10);
            }
        } else if (n >= 100 && n < 1000) {
            if (n % 100 == 0) {
                return stringOfNumber(n / 100) + "hundred";
            } else {
                return stringOfNumber(n / 100) + "hundredand" + stringOfNumber(n % 100);
            }
        } else if (n >= 1000 && n < 10000) {
            return stringOfNumber(n / 1000) + "thousand" + stringOfNumber(n % 1000);
        } else {
            return "";
        }
        return "";
    }

    public static void main(String[] args) {
        System.out.println(stringOfNumbersInWords(1000).length());
    }
}
<file_sep>/README.markdown
#Project Euler Solutions

This repository contains solutions to over 30 Project Euler problems, solved in Java, Python, Ruby, and C, by <NAME>. This code has been provided as a reference only.
<file_sep>/Problem61.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projecteuler;

import java.util.Arrays;
import java.lang.Math;

/**
 *
 * @author Chiranth
 */

/*Triangle, square, pentagonal, hexagonal, heptagonal, and octagonal numbers 
 * are all figurate (polygonal) numbers and are generated by the following 
 * formulae:
 * Triangle	 	P3,n=n(n+1)/2	 	1, 3, 6, 10, 15, ...
 Square	 	P4,n=n2	 	1, 4, 9, 16, 25, ...
 Pentagonal	 	P5,n=n(3n1)/2	 	1, 5, 12, 22, 35, ...
 Hexagonal	 	P6,n=n(2n1)	 	1, 6, 15, 28, 45, ...
 Heptagonal	 	P7,n=n(5n3)/2	 	1, 7, 18, 34, 55, ...
 Octagonal	 	P8,n=n(3n2)	 	1, 8, 21, 40, 65, ...


 The ordered set of three 4-digit numbers: 8128, 2882, 8281, has three 
 * interesting properties.
 The set is cyclic, in that the last two digits of each number is the first two 
 * digits of the next number (including the last number with the first).
 Each polygonal type: triangle (P3,127=8128), square (P4,91=8281), and 
 * pentagonal (P5,44=2882), is represented by a different number in the set.
 This is the only set of 4-digit numbers with this property.

 Find the sum of the only ordered set of six cyclic 4-digit numbers for which 
 * each polygonal type: triangle, square, pentagonal, hexagonal, heptagonal, 
 * and octagonal, is represented by a different number in the set.
 */
public class Problem61 {

    public static int length(int n) {
        return (int) Math.floor(Math.log10(n) + 1);
    }

    public static int factorial(int n) {
        int factorial = 1;
        for (int i = 1; i <= n; i++) {
            factorial *= i;
        }
        return factorial;
    }

    public static int triangleForm(int n) {
        return n * (n + 1) / 2;
    }

    public static int squareForm(int n) {
        return n * n;
    }

    public static int pentForm(int n) {
        return n * (3 * n - 1) / 2;
    }

    public static int hexForm(int n) {
        return n * (2 * n - 1);
    }

    public static int heptForm(int n) {
        return n * (5 * n - 3) / 2;
    }

    public static int octForm(int n) {
        return n * (3 * n - 2);
    }

    public static int[] bubbleSortIncreasing(int[] n) {
        int temp;
        boolean moves = true;
        while (moves) {
            moves = false;
            for (int i = 0; i < n.length - 1; i++) {
                if (n[i] > n[i + 1]) {
                    moves = true;
                    temp = n[i + 1];
                    n[i + 1] = n[i];
                    n[i] = temp;
                }
            }
        }
        return n;
    }

    public static boolean testTwoDigits(int[] x, int[] y) {
        if ((x[0] == y[0] && x[1] == y[1])
                || (x[0] == y[1] && x[1] == y[0])
                || (x[1] == y[0] && x[0] == y[1])) {
            return true;
        } else {
            return false;
        }
    }
    //Assumes the length of all numbers is 4

    public static boolean checkOrderedSet(int[][] set) {
        int[] prevDigits = new int[2];
        int[] currDigits = new int[2];
        boolean valid;
        for (int i = 0; i < set.length; i++) { //length of the array[][]
            valid = true;
            for (int n = 0; n < set[i].length && valid; n++) { //length of the array[]
                prevDigits[0] = (set[i][n] % 100) % 10;
                prevDigits[1] = ((set[i][n] % 100) - (set[i][n] % 10)) / 10;
                if (n < set[i].length - 1) {
                    currDigits[0] = set[i][n + 1] / 1000;
                    currDigits[1] = (set[i][n + 1] / 100) % 10;
                } else { // wrap around
                    currDigits[0] = set[i][0] / 1000;
                    currDigits[1] = (set[i][0] / 100) % 10;
                }
                if (testTwoDigits(prevDigits, currDigits)) {
                    valid = true;
                } else {
                    valid = false;
                }
            }
            if (valid) {
                return true;
            }
        }
        return false;
    }

//Find the largest index k such that a[k] < a[k + 1]. If no such index exists, the permutation is the last permutation.
//Find the largest index l such that a[k] < a[l]. Since k + 1 is such an index, l is well defined and satisfies k < l.
//Swap a[k] with a[l].
//Reverse the sequence from a[k + 1] up to and including the final element a[n].
    public static int[][] permutations(int[] a) {
        int permutations[][] = new int[1][a.length];
        int[] tempCopy, aCopy;
        boolean foundAk;
        int k = 0, l = 0, n, p = 0, al, ak, r;
        a = bubbleSortIncreasing(a); //reset the array for algorithm
        permutations[p++] = Arrays.copyOf(a, a.length);
        aCopy = permutations[0];
        for (; p < factorial(aCopy.length); p++) {
            foundAk = false; //reset variable
            //Step 1 find k such that a[k] < a[k+1]
            for (ak = 0; ak < a.length - 1; ak++) {
                if (aCopy[ak] < aCopy[ak + 1]) {
                    foundAk = true;
                    k = ak;
                }
            }
            if (foundAk) {
                //Step 2 find the largest l such that a[k] < a[l]
                for (al = k + 1; al < aCopy.length; al++) {
                    if (aCopy[k] < aCopy[al]) {
                        l = al;
                    }
                }
                //Step 3 swap a[k] with a[l]
                ak = aCopy[k];
                aCopy[k] = aCopy[l];
                aCopy[l] = ak;
                //Step 4 reverse sequence from a[k+1] inclusively to final element
                tempCopy = Arrays.copyOfRange(aCopy, k + 1, aCopy.length);
                //revisit loop to use the numbers from tempCopy properly
                for (r = k + 1, n = tempCopy.length - 1;
                        r < aCopy.length && n >= 0; r++, n--) {
                    aCopy[r] = tempCopy[n];
                }
                permutations = arrayAddLine(permutations, aCopy);
            }
        }
        return permutations;
    }

    public static int[] arrayAddInt(int[] arr, int next) {
        int[] temp;
        temp = Arrays.copyOf(arr, arr.length + 1);
        temp[arr.length] = next;
        return temp;
    }

    public static int[][] arrayAddLine(int[][] orig, int[] newLine) {
        int[][] temp = new int[orig.length + 1][orig.length];
        for (int i = 0; i < orig.length; i++) {
            temp[i] = Arrays.copyOf(orig[i], orig[i].length);
        }
        temp[temp.length - 1] = Arrays.copyOf(newLine, newLine.length);
        return temp;
    }

    public static void printArray(int[] a) {
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
        System.out.println();
    }

    public static int sumArray(int[] a) {
        int sum = 0;
        for (int i = 0; i < a.length; i++) {
            sum += a[i];
        }
        return sum;
    }

    public static void main(String[] args) {
        int hept, hex, oct, pent, square, triangle, sum = 0, tempCalc, tempLength;
        boolean foundCyclic = false;
        int[] aHept = new int[0], aHex = new int[0], aOct = new int[0],
                aPent = new int[0], aSquare = new int[0],
                aTriangle = new int[0], nthArr = new int[6];
        boolean foundHept = false, foundHex = false, foundOct = false,
                foundPent = false, foundSquare = false, foundTriangle = false;

        for (int n = 1; !foundHept || !foundHex || !foundOct || !foundPent
                || !foundSquare || !foundTriangle; n++) {
            if (!foundHept) {
                tempCalc = heptForm(n);
                tempLength = length(tempCalc);
                if (tempLength == 4) {
                    aHept = arrayAddInt(aHept, tempCalc);
                } else if (tempLength == 5) {
                    foundHept = true;
                }
            }
            if (!foundHex) {
                tempCalc = hexForm(n);
                tempLength = length(tempCalc);
                if (tempLength == 4) {
                    aHex = arrayAddInt(aHex, tempCalc);
                } else if (tempLength == 5) {
                    foundHex = true;
                }
            }
            if (!foundOct) {
                tempCalc = octForm(n);
                tempLength = length(tempCalc);
                if (tempLength == 4) {
                    aOct = arrayAddInt(aOct, tempCalc);
                } else if (tempLength == 5) {
                    foundOct = true;
                }
            }
            if (!foundPent) {
                tempCalc = pentForm(n);
                tempLength = length(tempCalc);
                if (tempLength == 4) {
                    aPent = arrayAddInt(aPent, tempCalc);
                } else if (tempLength == 5) {
                    foundPent = true;
                }
            }
            if (!foundSquare) {
                tempCalc = squareForm(n);
                tempLength = length(tempCalc);
                if (tempLength == 4) {
                    aSquare = arrayAddInt(aSquare, tempCalc);
                } else if (tempLength == 5) {
                    foundSquare = true;
                }
            }
            if (!foundTriangle) {
                tempCalc = triangleForm(n);
                tempLength = length(tempCalc);
                if (tempLength == 4) {
                    aTriangle = arrayAddInt(aTriangle, tempCalc);
                } else if (tempLength == 5) {
                    foundTriangle = true;
                }
            }
        }
        for (hept = 0; hept < aHept.length; hept++) {
            nthArr[0] = aHept[hept];
            for (hex = 0; hex < aHex.length; hex++) {
                nthArr[1] = aHex[hex];
                for (oct = 0; oct < aOct.length; oct++) {
                    nthArr[2] = aOct[oct];
                    for (pent = 0; pent < aPent.length; pent++) {
                        nthArr[3] = aPent[pent];
                        if (checkOrderedSet(permutations(Arrays.copyOfRange(nthArr, 0, 4)))) {
                            for (square = 0; square < aSquare.length; square++) {
                                for (triangle = 0; triangle < aTriangle.length; triangle++) {
                                    nthArr[4] = aSquare[square];
                                    nthArr[5] = aTriangle[triangle];
                                    printArray(nthArr);
                                    if (checkOrderedSet(permutations(nthArr))) {
                                        sum = sumArray(nthArr);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        System.out.println(sum);
    }
}

<file_sep>/Problem100.py


def gcd(a , b):
    t = 0
    while(b != 0):
        t = b
        b = a % t
        a = t
    return a
a = 4
b = 12
print(gcd(a , b))
print(gcd(1071, 462))
print(gcd(462, 1071))
<file_sep>/Problem70.rb
include Math

def lex_perm(a)
  permutations = []
  begin
    k = -1
    l = -1
    index = 0
    permutations.push []
    a.each {|int| permutations[permutations.length() -1].push int}
    for i in 0..(a.length - 2) do
      k = i if a[i] < a[i + 1]
    end
    for i in 0..(a.length - 1) do
      l = i if a[k] < a[i]
    end
    if k >= 0
      a[k],a[l] = a[l],a[k]
      a[k+1, a.length-1] = a[k+1, a.length-1].reverse
    end
  end while k >= 0
  return permutations
end

def compare_with_permutations(num, phi_n, perms)
  return false if num.to_s.length() != phi_n.to_s.length()
  isSame = false
  arr_str = ""
  perms[phi_n.to_s.length].each do |perm|
    arr_str = ""
    for ii in 0..(perm.length() - 1)
      arr_str << phi_n.to_s[perm[ii]]
    end
    puts arr_str
    return true if num == arr_str.to_i
  end
  return false
end

perms = [[]]
for nLength in 0..7
  tempPermArr = []
  for ii in 0..nLength
    tempPermArr.push ii
  end
  perms.push lex_perm(tempPermArr)
end

puts (compare_with_permutations(102, 210, perms))
<file_sep>/Problem57.py
import math

def length(n):
  return math.floor(math.log10(n) + 1)

numerator = 3
denominator = 2
iterations = 0
longerNumerators = 0
while (iterations < 1000):
	numerator = numerator + 2*denominator
	denominator = numerator - denominator
	if length(numerator) > length(denominator) :
		longerNumerators = longerNumerators + 1
	iterations = iterations + 1

print(longerNumerators)

<file_sep>/Problem104.rb
panSum = 1+2+3+4+5+6+7+8+9

def strAddition(x , y)
  return 0.to_s
end

def first_pandigital(x)
  if x.length < 9
    return false
  else
    arr = []
    for iter in 0..8
      arr.push(x[iter] - 48)
    end
    for iter in 1..9
      if !arr.include?(iter)
        return false
      end
    end
    return true
  end
end

def last_pandigital(x)
  if x.length < 9
    return false
  else
    arr = []
    for iter in (x.length - 9)..(x.length - 1)
      arr.push(x[iter] - 48)
    end
    for iter in 1..9
      if !arr.include?(iter)
        return false
      end
    end
    return true
  end
end

def fibonacci(k)
  iter = 0
  fk = 0
  fk_0 = 0
  fk_1 = 0
  if k == 0
    return 0
  elsif k == 1
    return 1
  elsif k > 1
    iter = 2
    fk = 1
    fk_0 = 0
    fk_1 = 1
    while(iter <= k)
      fk = fk_0 + fk_1
      fk_0 = fk_1
      fk_1 = fk
      iter += 1
    end
    return fk
  else
    return 0
  end
end

def find_k_pandigital
  k = 1
  fk = 1
  fk_0 = 0
  fk_1 = 1
  while(last_pandigital(fk.to_s) == false || first_pandigital(fk.to_s) == false)
    fk = fk_0 + fk_1
    fk_0 = fk_1
    fk_1 = fk
    k += 1
  end
  return k
end

puts first_pandigital(fibonacci(2749).to_s)
puts last_pandigital(fibonacci(541).to_s)
puts find_k_pandigital()
<file_sep>/Problem160.py
def removeZeros(n):
	nCopy = n
	while nCopy % 10 == 0:
		nCopy = nCopy / 10
	return nCopy

def reduceSize(n):
	return n % 100000

x = 1
lastFive = 1
while x < 1000000000000 :
	lastFive = lastFive*(reduceSize(removeZeros(x)))
	lastFive = reduceSize(removeZeros(lastFive))
	x = x + 1

print(lastFive)
<file_sep>/Problem25.py
import math

def nthFibonacciNum(nthTerm):
        phi = (1 + math.sqrt(5)) / 2
        n = math.pow(phi, nthTerm)/math.sqrt(5) + 1/2
        return math.floor(n)

def thousandDigitFibonacci():
        fibonacci = 0
        f1 = 1
        f2 = 1
        term = 2
        digits = 0
        while (digits < 1000):
                term = 1 + term
                fibonacci = f1 + f2
                f1 = f2
                f2 = fibonacci
                digits = math.floor(math.log10(fibonacci) + 1)
        return term

print(math.floor(math.log10(nthFibonacciNum(12)) + 1))
print(thousandDigitFibonacci())
<file_sep>/Problem21.py
def sumOfFactors(x):
    sum = 0
    for i in range(1, x/2 + 1):
        if(x % i == 0):
            sum = sum + i
    return sum

def d(a, secondNumber, b):
    sum = 0
    if(secondNumber == 0):
        sum = sumOfFactors(a)
        return d(a, True, sum)
    elif(secondNumber == 1):
        return a == sumOfFactors(b)

#sumOfAmicableNumbers not working
def sumOfAmicableNumbers(x):
    sum = 0
    for i in range(0, x):
        if(d(x, False, 0) == True):
            sum = sum + i
    return sum

print(sumOfAmicableNumbers(10000))

<file_sep>/Problem71.rb
require 'rational'
fractions = []
n = 1
d = 1*10**6
dMax = 1*10**4
maxRat = 3/7.to_f
minDiff = 1
immediateRational = Rational(3,7)
currDiff = 1

def euclid_gcd(m , n)
  while n != 0
    r = m % n
    m = n
    n = r
  end
  return m
end

while(d > 1) do
  n = (d/4).ceil
  currFrac = Rational(n,d)
  while (currFrac.to_f) <= maxRat do
    if euclid_gcd(d,n) == 1
      currDiff = maxRat - currFrac.to_f
      if(currDiff < minDiff && currDiff > 0)
        immediateRational = currFrac
        minDiff = currDiff
      end
    end
    n += 1
    currFrac = Rational(n,d)
  end
  puts immediateRational
  d -= 1
end
puts "Done looping..."
puts immediateRational
<file_sep>/Problem30.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projecteuler;
import java.lang.Math;
/**
 *
 * @author Chiranth
 */
public class Problem30 {
    
    public static int length(double x) {
        return (int) java.lang.Math.floor(java.lang.Math.log10(x) + 1);
    }
    
    public static double factorial(int n) {
        double factorial = 1;
        if (n <= 1) { //When you get to one, return
            return 1;
        } else {
            factorial = (double) n * factorial(n - 1); //multiply andstack down to 2
            return factorial;
        }
    }
    
    public static boolean checkNumber(int num) {
        int length = length(num), factNumSum = 0;
        int currentDigit;
        for(int i = 0, j = length -1; i < length; i++, j--) {
            currentDigit = (int) num / (int) Math.pow(10, j);
            factNumSum += factorial(currentDigit);
        }
        return false; // Just return false, cause I don't know what it's doing
    }
    
    public static void main(String args[]) {
        
    }
}
<file_sep>/Problem1.rb
divisibles = []
for i in 1..(1000-1)
  divisibles.push i if (i % 3 == 0 or i % 5 == 0)
end
sum = 0
divisibles.each {|nextNum| sum += nextNum}
puts sum
<file_sep>/Problem60.rb
require 'mathn'

class OptimusPrime
	def initialize()
		@primes = [2 , 3]
	end
	
	
	def primeSieve(n)
	isPrimeArr = Array.new(n + 1 , true)
	for i in 2..Math.sqrt(n)
		if(isPrimeArr[i] == true)
			index = i*i
			while(index <= n)
				isPrimeArr[index] = false
				index += i
			end
		end
	end
	return isPrimeArr
	end
	
	def addUpTo(n)
		puts "Checking #{n}"
		sieveList = primeSieve(n)
		primesList = Array.new
		sieveList.each_with_index {|isPrime , index|
			primesList << index if isPrime == true
		}
		primesList.delete	0
		primesList.delete	1
		primesList.each do |prime|
			if(!(@primes.include? prime))
				@primes.push	prime
			end
		end
		puts "Done"
	end
	
	def isPrime?(n)
		if(@primes[@primes.length - 1] >= n)
			return @primes.include? n
		else
			addUpTo n
			return @primes.include? n
		end
	end
	
	def printPrimes
		@primes.each {|prime| puts "#{prime}"}
	end
	
	def nthPrime(n)
		if(n < @primes.length)
			return @primes[n]
		else return false
		end
	end
	
	def numOfPrimes
		return @primes.length
	end
end

def isPrimePairSet?(primesObj , set)
	set.each do |n1|
		set.each do |n2|
			if(n1 != n2)
				if(!(primesObj.isPrime? (n1.to_s + n2.to_s).to_i))
					return false
				end
			end
		end
	end
	return true
end
  
def setSum(set)
	sum = 0
	set.each {|n| sum += n}
	return sum
end

def factorial(n)
	fact = 1
	for i in 1..n
		fact *= i
	end
	return fact
end

def addPrimeSet(primesObj , length , pPairSet)
	foundSetLength = false
	primes = 0
	iterations = 0
	nextSet = []
	while(not foundSetLength)
		primes += 1
		for i in 0..length-1	#check for the lengths
			if(i == 0)
				for n in 0..primes
					if(!(pPairSet.include? [(primesObj.nthPrime n)]))
						pPairSet.push	[(primesObj.nthPrime n)]
					end
				end
				foundSetLength = length == 1
			else	#end checking for single primes
				for n in 0..primes
					pPairSet.each do |set|
						#check prime set logic here
						nextSet = (set + [(primesObj.nthPrime n)]).sort
						if(!(set.include? primesObj.nthPrime n) and (isPrimePairSet?(primesObj , nextSet)))
							if(!(pPairSet.include? nextSet))
								pPairSet.push nextSet
							end
							foundSetLength = pPairSet[pPairSet.length - 1].length == length
						end
					end
				end	#primes
			end #else
		end	#end checking individual lengths
	end
end

primesObj = OptimusPrime.new
primesObj.isPrime?(51)
primesObj.isPrime?(200000)
primesObj.printPrimes
pPairSet = []
addPrimeSet(primesObj , 3 , pPairSet)
puts "Set Listing: #{pPairSet}"
pPairSet.each do |set|
	if(set.length == 3)
		puts "#{set} Sum: #{setSum set}"
	end
end<file_sep>/Problem60.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projecteuler;

/**
 *
 * @author Chiranth
 */

/*The primes 3, 7, 109, and 673, are quite remarkable. By taking any two primes 
 * and concatenating them in any order the result will always be prime. For 
 * example, taking 7 and 109, both 7109 and 1097 are prime. The sum of these 
 * four primes, 792, represents the lowest sum for a set of four primes with 
 * this property.

 Find the lowest sum for a set of five primes for which any two primes 
 * concatenate to produce another prime.
 */
import java.lang.Math;
import java.util.Arrays;

public class Problem60 {

    public static int factorial(int n) {
        int factorial = 1;
        for (int i = 1; i <= n; i++) {
            factorial *= i;
        }
        return factorial;
    }

    public static class OptimusPrime {

        private long[] primes = new long[1];

        public OptimusPrime() {
            primes[0] = 2;
        }

        public boolean isPrime(long checkPrime) {
            while (primes[primes.length - 1] < checkPrime) {
                addNextPrime();
            }
            if (Arrays.binarySearch(primes, checkPrime) >= 0) {
                return true;
            } else {
                return false;
            }
        }

        public boolean findIsPrime(long n) {
            if (n > 2) {
                for (int i = 2; i <= Math.sqrt(n); i++) {
                    if (n % i == 0) {
                        return false;
                    }
                }
                return true;
            } else {
                return false;
            }
        }

        public long findNextPrime(long currPrime) {
            long nextPrime;
            for (nextPrime = ++currPrime; !findIsPrime(nextPrime); nextPrime++);
            return nextPrime;
        }

        public void addNextPrime() {
            long[] temp = Arrays.copyOf(primes, primes.length + 1);
            temp[temp.length - 1] = findNextPrime(temp[temp.length - 2]);
            primes = temp;
        }

        public void printPrimes() {
            for (int i = 0; i < primes.length; i++) {
                System.out.println(primes[i]);
            }
        }

        public long[] getPrimes() {
            return primes;
        }
    }

    public static long[][] lexicographic(long[] primes) {
        long[][] permutations = new long[factorial(primes.length)][primes.length];
        Arrays.sort(primes);


        return permutations;
    }

    public static boolean testForAnyTwo(OptimusPrime prime, long[] test) {
        String concatenated;
        for (int i = 0; i < test.length; i++) {
            for (int j = 0; j < test.length; j++) {
                if (i != j) {
                    concatenated = Long.toString(test[i]) + Long.toString(test[j]);
                    if (!prime.isPrime(Long.parseLong(concatenated))) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    public static double sumArray(long[] arr) {
        double sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        return sum;
    }

    public static String[] addString(String[] arr, String newS) {
        String[] temp = Arrays.copyOf(arr, arr.length + 1);
        temp[temp.length - 1] = newS;
        return temp;
    }

    public static void main(String[] args) {
        OptimusPrime primeObj = new OptimusPrime();
        long[] primes = primeObj.getPrimes(), testArr = new long[5];
        String[] tested = new String[0];
        String currentTest;
        boolean foundFive = false;
        int currentMax = 1;
        int minSum = 0xFFFFF;

        testArr = new long[4];
        testArr[0] = 3;
        testArr[1] = 7;
        testArr[2] = 109;
        testArr[3] = 673;
        System.out.println(testForAnyTwo(primeObj, testArr));
        while (true) {
            for (int i1 = 0; i1 < currentMax; i1++) {
                for (int i2 = 0; i2 < currentMax && i2 != i1; i2++) {
                    for (int i3 = 0; i3 < currentMax && i3 != i2; i3++) {
                        for (int i4 = 0; i4 < currentMax && i4 != i3; i4++) {
                            currentTest = "" + i1 + " " + i2 + " " + i3 + " " + i4;
                            if (Arrays.binarySearch(tested, currentTest) < 0) {
                                testArr[0] = primes[i1];
                                testArr[1] = primes[i2];
                                testArr[2] = primes[i3];
                                testArr[3] = primes[i4];
                                if(testForAnyTwo(primeObj , testArr)) {
                                    System.out.println(sumArray(testArr));
                                    if(sumArray(testArr) < minSum) {
                                        minSum = (int) sumArray(testArr);
                                        System.out.println("Min: " + minSum);
                                    }
                                }
                                tested = addString(tested , currentTest);
                            }
                        }
                    }
                }
            }
            primes = primeObj.getPrimes();
            if(primes.length > currentMax) {
                ++currentMax;
            } else {
                primeObj.addNextPrime();
                primes = primeObj.getPrimes();
                ++currentMax;
            }
        }
    }
}
<file_sep>/Problem62.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projecteuler;

import java.lang.Math;
import java.util.Arrays;

/**
 *
 * @author Chiranth
 */
/**
 * Cubic permutations Problem 62 The cube, 41063625 (345^3), can be permuted to
 * produce two other cubes: 56623104 (384^3) and 66430125 (405^3). In fact,
 * 41063625 is the smallest cube which has exactly three permutations of its
 * digits which are also cube.
 *
 * Find the smallest cube for which exactly five permutations of its digits are
 * cube.
 *
 *
 *
 */
public class Problem62 {

    public static class Cube {
        private long firstCube;
        private long perfect;
        private String cubeDigits;
        private short instances = 0;

        public Cube(long perfect, long cube) {
            firstCube = cube;
            this.perfect = perfect;
            this.cubeDigits = sortDigits(cube);
            ++instances;
        }

        public String getCubeDigits() {
            return cubeDigits;
        }

        public boolean addInstance(long perfect) {
            if (this.cubeDigits.equals(sortDigits(perfect))) {
                ++instances;
                return true;
            } else {
                return false;
            }
        }

        public short getInstances() {
            return instances;
        }

        public String sortDigits(long l) {
            char[] arrDigits = Long.toString(l).toCharArray();
            Arrays.sort(arrDigits);
            return new String(arrDigits);
        }

        public long getPerfect() {
            return perfect;
        }
        
        public long getFirstCube() {
            return firstCube;
        }
    }

    public static class Cubes {
        private short highestInstance = 0;
        private long highestPerfect = 0;
        private long highestInstanceCube = 0;
        Cube[] cubes = new Cube[0];

        public Cubes() {
        }

        public void addCube(long perfect, long cube) {
            boolean added = false;
            for (int i = 0; i < cubes.length && !added; i++) {
                if (cubes[i].addInstance(cube)) {
                    added = true;
                    if(cubes[i].getInstances() > highestInstance) {
                        highestInstance = cubes[i].getInstances();
                        highestPerfect = cubes[i].getPerfect();
                        highestInstanceCube = cubes[i].getFirstCube();
                    }
                }
            }
            if (!added) {
                Cube newCube = new Cube(perfect, cube);
                cubes = push(cubes, newCube);
            }
        }

        public Cube[] push(Cube[] current, Cube newCube) {
            Cube[] newStack = Arrays.copyOf(current, current.length + 1);
            newStack[current.length] = newCube;
            return newStack;
        }
        
        public void print() {
            for(int i = 0; i < cubes.length; i++) {
                System.out.println(cubes[i].getPerfect() + " " +
                        cubes[i].getCubeDigits() + " " + 
                        cubes[i].getInstances());
            }
        }
        
        public short getHighestInstance() {
            return highestInstance;
        }
        
        public long getHighestPerfect() {
            return highestPerfect;
        }
        
        public long getHighestInstanceCube() {
            return highestInstanceCube;
        }
    }

    public static void main(String[] args) {

        int cube = 330;
        long testNum = 344, testRemainder;
        //System.out.println(testNum / Math.pow(10, length(testNum) - 1));
        Cubes c = new Cubes();
        for( ; c.getHighestInstance() < 5; cube++) {
            c.addCube(cube, (long) Math.pow(cube, 3));
        }
        System.out.println(c.getHighestInstance());
        System.out.println(c.getHighestPerfect());
        System.out.println(c.getHighestInstanceCube());
    }
}
<file_sep>/Problem73.rb
require 'mathn'
require 'timeout'

def euclid_gcd(m , n)
  while n != 0
    r = m % n
    m = n
    n = r
  end
  return m
end


def create_sieve(d)
  isPrimeArr = Array.new(d+1, true)
  for i in 2..Math.sqrt(d)
    if(isPrimeArr[i] == true)
      index = i*i
      while index <= d
        isPrimeArr[index] = false
        index += i
      end
    end
  end
  return isPrimeArr
end

Timeout::timeout(7200) {
sieve_arr = create_sieve(12000)
set_length = 0
for d in 1..12000
  for n in (d/3.0).round..(d/2.0).round
    if(Rational(n,d).to_f < 1/2.0 and Rational(n,d) > 1/3.0)
      if(euclid_gcd(n,d) == 1)
        set_length += 1
      end
    end
  end
end
puts set_length
}
<file_sep>/Problem74.rb
fact_arr = [1]

def factorial(x, arr)
  if arr[x] == nil
    if arr[x - 1] == nil
      factorial(x - 1, arr)
    end
    arr[x] = arr[x -1] * x
    return arr[x]
  else
    return arr[x]
  end
end

def factorial_chain(x, numbers, fact_arr)
  if !numbers.include? x
    numbers.push x
    str = x.to_s
    curr_sum = 0
    for i in 0..str.length() - 1
      curr_sum += factorial(str[i].to_i , fact_arr)
    end
    return 1 + factorial_chain(curr_sum, numbers, fact_arr)
  else
    return 0
  end
end

sixty = 0
for i in 0..1*10**6 - 1
  puts i
  sixty += 1 if factorial_chain(i, [], fact_arr) == 60
end
puts sixty
<file_sep>/Problem22.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projecteuler;

import java.io.*;
import java.util.*;

/**
 *
 * @author csiddapp
 */
public class Problem22 {

    public static String[] getNames() throws FileNotFoundException {
        String[] names = new String[5163];
        String temp = "";
        int i = 0; // counter variable
        Scanner nameScanner = new Scanner(new File("names.txt"));
        for (i = 0; nameScanner.hasNext(); i++) {
            temp += nameScanner.next();
        }
        for (i = 0; i < names.length; i++) { //Just clear to non-nulls
            names[i] = "";
        }
        for (i = 0; temp.length() > 4; i++) { //insertion for-loop
            if (temp.indexOf("ALONSO") > 2) {
                names[i] = temp.substring(temp.indexOf("\"") + 1, temp.indexOf("\","));
                temp = temp.substring(names[i].length() + 3, temp.lastIndexOf("\"") + 1);
            } else {
                names[i] = "ALONSO";
                temp = "";
            }
        }
        System.out.println("Names: " + i);
        return names;
    }

    public static int sumOfNameValues(String[] names) {
        int sum = 0, i = 0, j = 0, nameValue = 0;
        for (i = 0; i < names.length; i++) { //number in list
            for (j = 0; j < names[i].length(); j++) {
                if(names[i].charAt(j) != ' ')
                nameValue += (int)names[i].charAt(j) - 64; //number of each letter
            }
            sum += (nameValue * (i + 1));
            nameValue = 0;
        }
        return sum;
    }
    
    

    public static void main(String[] args) throws FileNotFoundException {
        char blank = ' ';
        String[] names = getNames();
        Arrays.sort(names);
        System.out.println(sumOfNameValues(names));
    }
}
<file_sep>/Problem63.py
import math

def length(n):
    return math.floor(math.log10(n) + 1)

def isNthPow(n):
    nthPow = length(n)
    base = 1
    while(length(base**nthPow) <= nthPow):
        if(base**nthPow == n):
            return 1 #indicates that a case was found
        base = base + 1
    return 0 #indicates that no match was found

def nthPows(nthPow):
    base = 1
    results = 0
    while(length(base**nthPow) <= nthPow):
        if(length(base**nthPow) == nthPow):
            results = results + 1
        base = base + 1
    return results

nthPow = 1
results = 0
while(nthPow < 1000):
    results = results + nthPows(nthPow)
    nthPow = nthPow + 1
print(results)
<file_sep>/Problem72.rb
require 'mathn'
require 'timeout'

=begin
Find lowest gcd == 1 cases, and multiply upwards
=end


def euclid_gcd(m , n)
  while n != 0
    r = m % n
    m = n
    n = r
  end
  return m
end

def add_multiples(set, n, d)
  #multiply up to 1*10**6
end


Timeout::timeout(300) {
  set_length = 0
  non_gcd_sets = [[],[]]
  for d in 1..1*10**6
    for n in 1..(d - 1)
      
    end
    puts d
  end
puts set_length
}
<file_sep>/Problem15.c
#include <stdio.h> //standard input/output
#include <stdlib.h> //standard library

//Represents the function n!, uses the stack
double factorial(int n) {
    double temp = 1; //set to a safe multiplicative value
    if (n <= 1) { //When you get to one, return
        return (double) 1;
    } else {
        temp = n * factorial(n-1); //Multiply and stack down to 2
        return temp;
    }
}

//find n(n-1)(n-2)...(n-r+1)
double nCrNumerator(int n, int r) {
        double temp = 1; //
        int i; //counter variable
        for (i = n; i >= (n - r + 1); i--) {
            temp *= (double) i;
        }
        return temp;
    }

//finds ((n(n-1)(n-2)(n-3)...(n-r+1))/r!) for n choose r
double nCr(int n, int r) {
    return (double) nCrNumerator(n, r) /(double) factorial(r);
}

int main() {
    printf("%f \n", nCr(40, 20));
    return EXIT_SUCCESS; //stdlib var
}<file_sep>/Problem5.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projecteuler;

/**
 *
 * @author Chiranth
 */
/**
 * 2520 is the smallest number that can be divided by each of the numbers from 1
 * to 10 without any remainder.
 *
 * What is the smallest positive number that is evenly divisible by all of the
 * numbers from 1 to 20?
 *
 */
public class Problem5 {

    public static boolean isDivisible(int n) {
        for(int i = 1; i <= 20; i++) {
            if(n % i != 0) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        int divisibleNumber;
        for(divisibleNumber = 1; !isDivisible(divisibleNumber); divisibleNumber++);
        System.out.println(divisibleNumber);
    }
}
<file_sep>/Problem1.py
#Problem 3
sum = 0
for i in range(1,1000):
        if(i %3 == 0 or i % 5 == 0):
                sum = sum + i

print(sum)
<file_sep>/Problem19.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projecteuler;

/**
 *
 * @author Chiranth
 */

/*
 * You are given the following information, but you may prefer to do some
 * research for yourself. 1 Jan 1900 was a Monday. Thirty days has September,
 * April, June and November. All the rest have thirty-one, Saving February
 * alone, Which has twenty-eight, rain or shine. And on leap years, twenty-nine.
 * A leap year occurs on any year evenly divisible by 4, but not on a century
 * unless it is divisible by 400.
 *
 * How many Sundays fell on the first of the month during the twentieth century
 * (1 Jan 1901 to 31 Dec 2000)?
 * 
*/

/*
 * Use the file named Problem19.txt for all the months of the years
 *
 */
import java.io.*;
import java.util.*;

public class Problem19 {

    public static int numberOfFirstSundays() throws FileNotFoundException {
        Scanner calReader = new Scanner(new File("Problem19.txt"));
        int firstSundays = 0, i = 0, nextSunday = 0;
        boolean findNextSunday = false;
        String nextLine = "";
        for (i = 0; calReader.hasNextLine(); ++i) {
            nextLine = calReader.nextLine();
            //Find a blank line and add two by default
            if (nextLine.trim().isEmpty()) {
                nextSunday = i + 3;
                findNextSunday = true;
            }
            if (findNextSunday && i == nextSunday) {
                findNextSunday = false;
                if (isFirstCharNum(nextLine)) {
                    ++firstSundays;
                }
            }
        }
        return firstSundays;
    }

    public static boolean isFirstCharNum(String nextLine) {
        String firstThreeChars = "";
        if (nextLine.length() > 3) {
            firstThreeChars = nextLine.substring(0, 3);
            if (!firstThreeChars.isEmpty() && !firstThreeChars.trim().isEmpty()
                    && !firstThreeChars.equalsIgnoreCase(" Su")) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public static void main(String[] args) throws FileNotFoundException {
        System.out.println(numberOfFirstSundays());
    }
}
<file_sep>/Problem60.py
import math

def isPrime(n):
    i = 2 #counter variable
    while i <= math.sqrt(n):
        if(n % i == 0):
            return False
        i = i + 1
    return True

print(isPrime(25))
print(isPrime(13))
<file_sep>/Problem92.rb
#number chains
require 'benchmark'
chain_truth = [false]
squares = [0,1]
def is89?(n, chain_truth, squares)
  curr_nums = [n]
  curr_num = curr_nums[0]
  if chain_truth[n] != nil
    return chain_truth[n]
  else
    chain_truth[n] = is89?(num_sum(n, squares), chain_truth, squares)
  end
end

def num_sum(n, squares)
  str_rep = n.to_s
  sum = 0
  for i in 0..str_rep.length() -1
    squares[str_rep[i].to_i] = (str_rep[i].to_i)**2 if squares[str_rep[i].to_i] == nil
    sum += squares[str_rep[i].to_i]
  end
  return sum
end

chain_truth[1] = false
chain_truth[89] = true
sum_89s = 0
puts Benchmark.realtime {
for i in 0..10000000
  sum_89s += 1 if is89?(i, chain_truth, squares)
end
}
puts sum_89s
<file_sep>/Problem81.py
file = open("matrix.txt", 'r')
matrix = []
global sum_matrix
sum_matrix = []
global min_sum
min_sum = 0
for line in file.readlines():
    nums = []
    char_strings = line.split(',')
    for i in char_strings:
        nums.append(int(i))
    matrix.append(nums)

def min_sum_path(matrix, x, y, curr_sum):
    global min_sum
    global sum_matrix
    if(x < len(matrix) - 1 and y < len(matrix) - 1):
        if(sum_matrix[x][y] == 0 or sum_matrix[x][y] > curr_sum + matrix[x][y]):
            sum_matrix[x][y] = curr_sum + matrix[x][y]
            min_sum_path(matrix, x + 1, y, curr_sum + matrix[x][y])
            min_sum_path(matrix, x, y + 1, curr_sum + matrix[x][y])
    elif (x == len(matrix) - 1 and y < len(matrix)):
        if(sum_matrix[x][y] == 0 or sum_matrix[x][y] > curr_sum + matrix[x][y]):
            sum_matrix[x][y] = curr_sum + matrix[x][y]
            min_sum_path(matrix, x, y + 1, curr_sum + matrix[x][y])
    elif (y == len(matrix) - 1 and x < len(matrix)):
        if(sum_matrix[x][y] == 0 or sum_matrix[x][y] > curr_sum + matrix[x][y]):
            sum_matrix[x][y] = curr_sum + matrix[x][y]
            min_sum_path(matrix, x + 1, y, curr_sum + matrix[x][y])
    else:
        if(min_sum == 0 or curr_sum < min_sum):
            print(curr_sum)
            min_sum = curr_sum

for ii in xrange(0, len(matrix)):
    sum_matrix.append([])
    for jj in xrange(0, len(matrix)):
        sum_matrix[ii].append(0)
min_sum_path(matrix, 0 , 0, 0)
print(min_sum)
<file_sep>/Problem76.rb
# for the number 5, there are six possibilities

def sums_of(n , highest, sums)
  if(n > 1)
    #do something
  else
    return sums[n - 1]
  end
end


sums = [1,1,2]
puts sums_of(5, sums)
<file_sep>/Problem66.py
import math

def isDiophantine(x , y , d):
	return x**2 - d*y**2 == 1

def isSquare(n):
        return math.floor(math.sqrt(n))**2 == n

def diophantineXVal(y , d):
        return math.floor(math.sqrt(d*y**2 + 1))

def length(n):
        return math.floor(math.log10(n) + 1)

maxX = 0
maxD = 0
d = 215
x = 1
y = 1
minSolFound = False
while(d <= 1000):
        x = 1
        y = 1
        while(not minSolFound and not isSquare(d) and length(x) <= 38):
                x = diophantineXVal(y , d)
                if(isDiophantine(x , y , d)):
                        minSolFound = True
                        if(x > maxX):
                                maxX = x
                                maxD = d
                y = y + 1
        minSolFound = False
        d = d + 1
        print("D: " + str(d))

print(maxD)
<file_sep>/Problem155.py
def capacitances(n) :
    if(n > 1):
        return (2*capacitances(n-1) + 1)
    else:
        return 1

print(capacitances(18))
<file_sep>/Problem41.java
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projecteuler;

import java.lang.Math;

/**
 *
 * @author chiranth
 */
/**
 * We shall say that an n-digit number is pandigital if it makes use of all the
 * digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is
 * also prime.
 *
 * What is the largest n-digit pandigital prime that exists?
 * 
 * Note: All pandigital numbers greater than 754321 can be divided by 3, using
 * the sum of digits rule, for 8 the digits = 36
 */
public class Problem41 {

    public static int length(long x) {
        return (int) Math.floor(Math.log10(x) + 1);
    }

    public static boolean isPrime(long x) {
        if (x <= 1) {
            return false;
        } else if (x % (int) Math.sqrt(x) == 0) {
            return false;
        } else {
            for (long i = 2; i < Math.sqrt(x); i++) {
                if (x % i == 0) {
                    return false;
                }
            }
        }
        return true;
    }

    public static boolean isPandigital(long x) {
        int sum = 0, i = 0, nextDigit;
        int lengthOfX = length(x); //length of x will change in following loop
        int[] numbers = new int[10];
        boolean isPandigital = true;
        //numbers represents occurrences of the numbers, set them to 0 occurrs
        for (i = 0; i < numbers.length; i++) {
            numbers[i] = 0;
        }
        //Go through x for its' numbers
        for (i = 0; i < lengthOfX; i++) {
            nextDigit = (int) x % 10;
            if (nextDigit == 0 || nextDigit > lengthOfX) {
                return false;
            }
            numbers[nextDigit] += 1; //Another occurance of this number
            x -= nextDigit;
            x /= 10;
        }
        //Now test the numbers array for more than one occurrence
        for (i = 0; i < numbers.length; i++) {
            if (numbers[i] > 1) {
                isPandigital = false;
                return false;
            }
        }
        return isPandigital;
    }

    public static long maxPandigitalPrime() {
        long maxPandigitalPrime = 0, startPos = 0, digitsSum = 3;
        boolean foundPrime = false;
        //Find the first pandigital not divisible by 3
        for(int i = 9; i > 0 && digitsSum % 3 == 0; i--) {
            digitsSum = 0;
            for(int j = 1; j <= i; j++) {
                digitsSum += j;
            }
            String temp = "";
            if(digitsSum % 3 != 0) {
                for(int k = i; k >= 1; k--) {
                    temp += k;
                }
                startPos = Long.parseLong(temp);
            }
        }
        for (long i = startPos; i > 4231 && !foundPrime; i--) {
            if (isPandigital(i)) {
                if (isPrime(i)) {
                    maxPandigitalPrime = i;
                    foundPrime = true;
                }
            }
        }
        return maxPandigitalPrime;
    }

    public static void main(String[] args) {
        System.out.println(maxPandigitalPrime());
    }
}
<file_sep>/Problem20.py
def factorial(upperBound):
	factorial = 1
	for i in range(1, upperBound):
		factorial = factorial * i
	return factorial
def sumOfDigits(number):
	sum = 0
	while(number > 0):
		sum = sum + (number % 10)
		number = number - (number % 10)
		number = number / 10
	return sum
number = 0
print(sumOfDigits(factorial(100)))
<file_sep>/problem1.c
#include <stdlib.h>
#include <stdio.h>

int main() {
  int multiple, sum = 0, iter, limit = 1000;
  bool multiples[1000] = {0};
  for(multiple = 3; multiple < limit; multiple += 3) multiples[multiple] = true;
  for(multiple = 5; multiple < limit; multiple += 5) multiples[multiple] = true;
  for(iter = 0; iter < limit; iter++) {
    if(multiples[iter]) sum += iter;
  }
  printf("%d\n", sum);
  return 0;
}
