이경수 선생님의 수학실험실

Problem 33(Digit cancelling fractions) 본문

Project Euler

Problem 33(Digit cancelling fractions)

(이경수) 2019. 5. 27. 22:13

Problem 33(Digit cancelling fractions)

The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is 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.

 

In Python:

import time
import math

startTime = time.time()
numerator = 1
denominator = 1
for i in range(11, 100):
    for j in range(10, i):
        for n in range(0, 2):
            if int(list(str(j))[1 - n]) == int(list(str(i))[n]) and int(list(str(i))[1 - n]) != 0:
                if int(list(str(j))[n]) / int(list(str(i))[1 - n]) == j / i:
                    numerator *= j
                    denominator *= i
denominator /= math.gcd(numerator, denominator)
print(denominator)
print(time.time() - startTime, "seconds")

Run time: 0.02585887908935547 seconds

 

In Java:

package project_euler31_40;

import java.util.ArrayList;

public class Euler33 {
	public static int findgcd(int i, int j) {
		if (i < j) {
			int temp = i;
			i = j;
			j = temp;
		}
		if (j == 0) {
			return i;
		} else {
			return findgcd(j, i % j);
		}
	}
	public static void main(String[] args) {
		long startTime = System.currentTimeMillis();
		int numerator = 1;
		int denominator = 1;
		String idata[];
		String jdata[];
		ArrayList<Integer> iList = new ArrayList<Integer>();
		ArrayList<Integer> jList = new ArrayList<Integer>();
		for (int i = 11; i < 100; i++) {
			for (int j = 10; j < i; j++) {
				
				String iStr = Integer.toString(i);
				String jStr = Integer.toString(j);
				iList.clear();
				jList.clear();
				
				idata = iStr.split("");
				for (int k = 0; k < iStr.length(); k++) {
					iList.add(Integer.parseInt(idata[k]));
				}
				jdata = jStr.split("");
				for (int k = 0; k < jStr.length(); k++) {
				jList.add(Integer.parseInt(jdata[k]));
				}
				for (int n = 0; n < 2; n++) {
					if (iList.get(n) == jList.get(1 - n) && iList.get(1 - n) != 0) {
						if ((double)jList.get(n) / iList.get(1 - n) == (double)j / i) {
							numerator *= j;
							denominator *= i;
						}
					}
				}
			}
		}
		denominator /= findgcd(numerator, denominator);
		System.out.println(denominator);
		long endTime = System.currentTimeMillis();
		System.out.println((double)(endTime - startTime)/(double)1000 + "seconds");
	}
}

Run time: 0.063seconds

 

Solution: 100

'Project Euler' 카테고리의 다른 글

Problem 35(Circular primes)  (0) 2019.06.04
Problem 34(Digit factorials)  (0) 2019.06.02
Problem 32(Pandigital products)  (0) 2019.05.19
Problem 31(Coin sums)  (0) 2019.05.12
Problem 30(Digit fifth powers)  (0) 2019.05.12
Comments