1
\$\begingroup\$

I am starting to learn Julia. For getting fluent in the syntax, I am solving some of the easier Project Euler challenges. Problem 33 is about so-called curious fractions, which keep their value after canceling out common digits in numerator an denominator (example: removing digit 9 from \$\frac{49}{98}\$ to get \$\frac{4}{8}\$ reduces to the correct result \$\frac{1}{2}\$.

I wanted to ask especially whether I am using non-idiomatic conversions (like num_str = "$num" to convert Integer to string). I am using Julia 0.4.5 right now, in case that matters.

Other than that, recommendations on better structuring the whole program.

"""
Check whether fraction Rational(a, b), when "curiously" shortened by removing
a common digit in numerator and denominator, keeps its value.

# Reference
Definition via Project Euler, Problem 33 
https://projecteuler.net/problem=33

# Examples
```
julia> is_curious_fraction(19, 95)
true

julia> is_curious_fraction(24, 27)
false
```
"""
function is_curious_fraction(num, den)
    original_fraction = Rational(num, den)
    num_str = dec(num)
    den_str = dec(den)

    for d in 1:9  # ignore zeros as trivial
        d_char = Char(d + 48)  # convert digit to ASCII character

        # check whether both num and den contain same digit
        if d_char ∈ num_str && d_char ∈ den_str
            # if so, create "curiously" shortend fraction
            # by removing at most one occurence of the digit
            new_numerator = replace(num_str, d_char, "", 1)
            new_denominator = replace(den_str, d_char, "", 1)

            # convert this pair of one-digit strings to Int and
            # create new Rational
            new_fraction = Rational(parse(Int, new_numerator), 
                                    parse(Int, new_denominator))

            # for a curious fraction, the value of original and
            # "curiously" shortened fraction are equal
            if new_fraction == original_fraction
                return true
            end
        end
    end
    # in all other cases, it is no curious fraction
    false
end

curious_fractions = []
# loop over all two-digit numinator and denominator
# fractions with value less than one
for num in 10:99
    for den in (num + 1):99
        if is_curious_fraction(num, den)
            push!(curious_fractions, Rational(num, den))
            println("$num / $den")
        end
    end
end
# denominator of product of fractions 
# (after reduction to lowest common terms)
println(prod(curious_fractions).den)
\$\endgroup\$
  • \$\begingroup\$ the digits function is probably of interest to you \$\endgroup\$ – Lyndon White Aug 24 '16 at 3:01
  • \$\begingroup\$ And, note to self: I found out about the dec function, which is probably better than my using string evaluation "$a" to convert an Integer to String: a = 1234; assert("$a" == dec(a)) \$\endgroup\$ – ojdo Aug 24 '16 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.