### Random Post

### Recent Posts

### Recent Comments

### Archives

### Categories

- article (11)
- enigma (1,609)
- enigma-book-1982 (70)
- misc (5)
- project euler (2)
- puzzle (90)
- puzzle# (173)
- site news (76)
- tantalizer (189)
- teaser (7)
- today (1)

### Site Stats

- 310,314 hits

Programming Enigma Puzzles

20 March 2015

Posted by on **From New Scientist #2403, 12th July 2003** [link]

George has written down a proper fraction whose numerator and denominator each have four digits, and has calculated the corresponding decimal fraction, thus:

All the digits of the fraction and its decimal equivalent have been replaced by # marks. Moreover, the bar over the decimal expression indicates that its value comprises one decimal digit that is followed by a recurring decimal with a seven-digit cycle.

The numerator, the denominator and the non-recurring decimal digit include nine different digits.

If the numerator is a prime, what are the seven digits under the bar?

[enigma1247]

%d bloggers like this:

I thought I’d write a generic routine for calculating recurring decimals (although I may have gone a bit over the top with the “base” argument – for example 5/17 = 0.(4B)… as a recurring hexadecimal).

This Python program runs in 303ms.

Solution:The seven digits in the recurring part of the decimal are 2217573.The full expression for the solution is (with the repeating section in brackets):

1487 / 2390 = 0.6(2217573)…

If we allow non-canonical recurring decimals we could claim that the following are also solutions:

1609 / 4827 = 0.3(3333333)…, but we would normally write this as 0.(3)…

3079 / 6158 = 0.4(9999999)…, but we would normally write this as 0.5.

Hi Jim,

It seems there is no answer for this question,

because, the repeated part repeats only 2 times.

The division 1487/2390 gives 0.6_2217573_2217573_251858766525401733815670013427734375

You’ll never get an exact representation for an infinitely recurring decimal expansion using the Python

`decimal`

module (or`float`

). Although you can increase the precision of`Decimal`

(default is 28 decimal places) to get a more accurate approximation.MMa Code

3079 / 6158 = 1/2 exactly and so shouldn’t get on the list as it already has a 5 in the denominator, rounding errors again. to 50 DP.

1487/2390 = 62217573221757322175732217573221757322175732217573221757322175732217573221757

One of the things I like about MMa is you don’t have to worry about any accuracy. I knew I needed to work to 16 dp for this, so set the 16 in this part of code

`d=ToString[N[FromDigits[a[[q]]]/c[[w]],16]];`

it could have easily been 100 or 1000, at 100 dp the timing was 1.82 and at 1000 it went up to 4.2Paul.

while we are talking timings, I tend to forget one other great aspect of MMA, it’s scalability. I can wrap round the whole code one word ‘Parallelize’ and the above program ran in

{0.046800, Null}. My licence is only for 4 cores, but there is no limit to how many cores it can use.

Paul.

This one runs in 51ms using shell based timing (9ms using profile).

I have used the general division technique. This is not so fast, its execution time on my machine is almost 450 miliseconds. If I want faster one, I would rather use C++.

Here is an alternative version of my code, that uses the observation that

ydivides99999990x, andxis prime. Soyis either a 4-digit multiple ofx(and the only 1-digit multipliers that are also divisors of 99999990 are 2, 3, 5, 6, 9), oryis a 4-digit divisor of 99999990 (and it must consist of 4-different digits, so the only candidates are 2390 and 4302).Note that if

yis a multiple ofx, then the fraction is not in its lowest terms, but the puzzle doesn’t explicitly state that it is. It says it is a “proper” fraction, which technically just means thatx<y.It also doesn't need the full power of the generic [[

`recurring()`

]] function from my original program (which is a shame, because it was a fun bit of code to write). Once we have the 7-digit recurring part of the decimal, as 7 is prime we know that if the recurring part includes multiple whole repeats then it must be 7 repeats of a 1-digit cycle, so we can just test for that.This code runs in 42ms.

Like my previous program all the computation takes place using integers, so there is no dependence on any fixed or floating point implementation.

If you remove the final check (line 46) you see that the candidate solutions are the same as my first program finds:

It doesn’t produce the additional candidates that Paul’s Mathematica program finds:

because in the convention I am using 0.5(0)… is not the non-terminating decimal representation of 1/2. Rather it is 1/2 = 0.4(9)…, and these three solutions already use the digit 4 in the denominator and so are disallowed.

If you want to allow 0.5(0)… as the valid representation of 1/2 you can remove the [[

`- 1`

]] in line 39.If you want to see more decimal places in the decimal representation of the solution you can use the Python [[

`Decimal()`

]] class, and set [[`f = Decimal(x) / Decimal(y)`

]] in line 48.