とんちゃんといっしょ

Cloudに関する技術とか日常とかについて書いたり書かなかったり

Project Euler Problem 33

土曜日にわんくまの数学Dayに参加してきた。


数学難しいです><


ってなったあと・・・っていうか最中に現実逃避に、
Project Eulerをやり始めた(ぉ


今回はProblem 33

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.

http://projecteuler.net/index.php?section=problems&id=33

ようは2桁の分数において分母と分子におんなじ数字があれば消せと。
分母分子の共通を消して出来上がった分数と、
もとの分数が同じである4つの分数の積から約分した分母の数を答えろってことらしい。


最初4つの分数ってなんのことかと思ったが、
他の人の解釈を探してようやく理解できた(ぉ


今回は解説はコメントに書いてみたが、
なんせ英語力がほぼ皆無のためあってるかどうか不明。


でも英語みたいなコメントで晒してみる!
ツッコミ募集(ぉ

require 'mathn'

ans = 1 
        
# Denominator is 2 digit.
10.upto(99) do |denominator|                           
    # To avoid division by zero.
    next if denominator % 10 == 0
    # Numerator is 2 digit and less than Denominator.
    10.upto(denominator-1) do |numerator|
        fraction1 = numerator / denominator
        fraction2 = 0     
        # When you require 'mathn', "Integer / Integer" is not Integer but Fraction.
        # If you want to Integer, you should be Fraction#to_i.
        if (denominator % 10) == (numerator / 10).to_i  
            fraction2 = (numerator % 10) / (denominator / 10).to_i
        elsif (denominator / 10).to_i == (numerator % 10)
            fraction2 = (numerator / 10).to_i / (denominator % 10)
        end                                             
        ans *= fraction1 if fraction1 == fraction2      
    end                                                 
end                                                     
                                                      
p ans.to_s