Thoughts on Perl and Emacs, technology and writing

Project Euler and Calc Mode

What makes a real programmer1. Well, apart from the ability to write a blackjack program that always wins, I think it is probably someone who actually programs for fun outside of work (novel concept, I know), not necessarily someone who is good at it. I’m not a real programmer, but I do play one occasionally on various blogs.

One of the places I look for fun problems to solve is Project Euler. Actually that isn’t true. I’ve only solved two problems there which gives me a smartness rating of 1%. But what is Project Euler? Fortunately, that is one of the FAQs.

Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.

Now all I need to do is find a way to measure how long the recurring cycle is. Having solved it prior to writing the article, I can say a couple of things which I discovered by experimentation at the REPL.

There is no 1/d for d < 1000 where the cycle is longer than d – 1.

The long cycles are all where d is prime number

The second of these means that I only need to process the prime numbers which will save a lot of calculation effort. The first, leads to a strategy to calculate the cycle.

If I set the precision to, say, 1050, there will always be a cycle. Therefore, all I have to do is take digits 1020 to 1040 or so and figure out where they occurred previously.

If r contains a string representation of the floating-point ratio, then diff will contain the length of the cycle. This assumes the cycle only occurs once in the string which isn’t necessarily the case. I therefore add a check that an arbitrary 20 digit sequence in the middle of the string occurs at that point for the first time2.

Putting it all together along with a method to get a list of prime numbers gets me this entirely adequate solution. And d is therefore… actually, no I’m not going to tell you. You’ll just have to run the code for yourself.

Share this:

Like this:

Related

7 Responses

Did you only try the primes between 900 and 1000? I notice the largest prime d < 1000 which generates a d-1 length cyclic (is a full reptend prime) is the 166th prime, 983 – so it must be the right answer – but not all primes do so. All you can say of any prime d is that the length of its reciprocal decimal digit cycle divides d-1. Would your program have been certain to find the right answer if there had been no full reptend primes in that interval?

No, it wouldn’t have found it if the answer hadn’t been in that range. However, I originally ran it on all the numbers (not just prime) between 2 and 999. It took a few seconds to run and determined the correct result. It wasn’t really necessary to restrict the search to just primes.

I actually solved the problem a few months ago so I forget why I chose a range of 900 to 1000. Could it be because recursion is extremely limited in emacs and it was unable to generate primes from 2 to 999?