Project Euler – Problem 33

Problem 33:
The fraction 49/98 is a curious fraction, as 49/98 = 4/8, obtained by cancelling the 9s.

We shall consider fractions like, 30/50 = 3/5, to be trivial examples.

There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.

If the product of these four fractions is given in its lowest common terms, find the value of the denominator.


public class Fraction{
	int t,b;
	public Fraction(int top, int bot){
		t=top;b=bot;
	}
	public Fraction multiply(int x){
		return new Fraction(t*x,b*x);
	}
	
	public int getNumerator(){
		return t;
	}
	public int getDenominator(){
		return b;
	}
	
	public int compareTo(Fraction other){
		if(other.getNumerator() == t && other.getDenominator() == b) return 0;
		return -1;
	}
	public String toString(){
		return t+"/"+b;
	}
}

Actual Code:

import java.util.Collections;
import java.util.HashMap;
import java.util.Vector;

class runner
{
	private static boolean storageContainsFraction(HashMap> storage, Fraction current){
		for(Fraction key : storage.keySet()){
			Vector sub_array = storage.get(key);
			for(int i=0; i> storage = new HashMap>(); 
		
		//Setup
		for(int denominator = 2; denominator < 50; denominator++){//49*2 = 98, the limit of doubledigits
			for(int numerator = 1; numerator < denominator; numerator++){
				Fraction current = new Fraction(numerator, denominator);
				if(storageContainsFraction(storage, current)) continue;
				
				//System.out.print(current + " ");
				
				Vector sub_array = new Vector();
				sub_array.add(current);
				
				int i=2;
				Fraction multiplied = current.multiply(i);
				while(multiplied.getDenominator() < 100 && multiplied.getNumerator() < 100){
					sub_array.add(multiplied);
					
					//System.out.print(multiplied + " ");
					
					i++;
					multiplied = current.multiply(i);
				}
				//System.out.println();
				
				storage.put(current, sub_array);
			}	
		}

		Vector denominatorAnswer = new Vector();
		
		//Check
		for(Fraction key : storage.keySet()){
			Vector sub_array = storage.get(key);
			for(int i=0; i