Euler Problem 26
Andrew Mathenge
mathenge at gmail.com
Mon Jun 30 23:33:36 BST 2008
Actually, I think that understanding the question is the easiest part.
Here's the question as copied from the website:
Find the value of d < 1000 for which 1/d contains the longest
recurring cycle in its decimal fraction part.
Let's take the example that's given where d <= 10.
When d = 7, the value of 1/d (1/7) is:
0.142857142857142857142857142857142857142857....
As you can see, (142857) repeats indefinitely. It has a sequence of 6
digits that repeat. This is longer than any of the others where d <=
10.
Therefore, the question asks, for d = 1 to 1000, which value of d has
the longest sequence of repeating digits in the fractional part?
Quite rightfully so, you can't limit the number of digits to 9 as in
your example since there will be a number with a series of repeating
digits greater than 9.
For example, 1/17 is:
0.0588235294117647058823529411764705882352941176470588235294117647.....
or
0.(0588235294117647)
The number of repeating digits in this sequence is 16.
Hope that helps. I'm not a python programmer so I really can't help
debug your code.
Andrew.
On Mon, Jun 30, 2008 at 4:03 PM, kinuthiA muchanE <muchanek at gmail.com> wrote:
> This email is from a ubuntu-ke subscriber to other subscribers inluding you:)
>
> On Mon, 2008-06-30 at 22:04 +0300, Tony White wrote:
>> Hi
>>
>> My guess, your pattern is limiting you to uniquely the digits 0-9, so
>
> ...and that is the whole idea IMHO, actually "input-ting" nothing would
> match my regular expression. There are only ten non-recurring digits in
> the world ie zero to nine, :-).
>> that's where it stops. Consider - (1011011101234101111109) could also
>> be a valid sequence. ie, digits may be used more than once.
>
> My dear Tony, so what is the question? I thought they wanted the
> reciprocal of a number, that will give you the digits "123456789" in a
> non-repeating order. And 1011011101234101111109 would not match the
> regular expression I had! 1's are and 0's occur more than once!
>
>
>
> Mugumo, I think there is always a mathematical angle to everything! And
> I am okay...
>
> Asante kwa kuweza kupata wasaa wa kunijibu, natumai tutaendelea
> kuwasiliana.
>
> Kinuthia...
>
>>
>> Tony
>>
>>
>> 2008/6/30 kinuthiA muchanE <muchanek at gmail.com>:
>> > This email is from a ubuntu-ke subscriber to other subscribers inluding you:)
>> > Habari wote,
>> >
>> >
>> > I am trying to solve Problem Number 26
>> > (http://projecteuler.net/index.php?section=problems&id=26) on Project
>> > Euler but apparently the answer I am submitting is wrong.
>> >
>> > Here is the problem:
>> >
>> >
>> > A unit fraction contains 1 in the numerator. The decimal representation
>> > of the unit fractions with denominators 2 to 10 are given:
>> >
>> > 1/2
>> > =
>> > 0.5
>> > 1/3
>> > =
>> > 0.(3)
>> > 1/4
>> > =
>> > 0.25
>> > 1/5
>> > =
>> > 0.2
>> > 1/6
>> > =
>> > 0.1(6)
>> > 1/7
>> > =
>> > 0.(142857)
>> > 1/8
>> > =
>> > 0.125
>> > 1/9
>> > =
>> > 0.(1)
>> > 1/10
>> > =
>> > 0.1
>> >
>> > Where 0.1(6) means 0.166666..., and has a 1-digit recurring cycle. It
>> > can be seen that 1/7 has a 6-digit recurring cycle.
>> >
>> > Find the value of d < 1000 for which 1/d contains the longest recurring
>> > cycle in its decimal fraction part.
>> >
>> > I am giving the answer 38, because 1/38 = 0.0263157894. It seems I have
>> > misunderstood the question or I cant solve it! Here is the code(in
>> > Python) that I
>> > came up with:
>> >
>> > def aux(num):
>> > import re
>> > pattern = re.compile(r"^0?1?2?3?4?5?6?7?8?9?$")
>> >
>> > frac ="%.9f"%(1.0/num)
>> > fracSlice = frac[2:] # get the decimal
>> > fractional part, ie remove
>> > '0.'
>> >
>> > fracList = list(fracSlice) #convert string to a
>> > list
>> > fracList.sort() # I need to
>> > sort , because I will be searching by
>> > increasing order
>> >
>> > testFrac = "".join(fracList) # convert list back to a string,
>> > phew!
>> > if re.match(pattern,testFrac): # if the pattern matches, the
>> > number is
>> > our candidate
>> > print (num,fracSlice)
>> >
>> >
>> > for b in xrange(1,1000):
>> > aux(b)
>> >
>> > Er... er, that does not exactly work as expected but it narrows the
>> > search to only 3 candidates because of the inclusion of the zero:
>> >
>> > (28, '035714286')
>> > (38, '026315789')
>> > (81, '012345679')
>> >
>> > For 28, the digit, in the fractional part, after 8 is 5, so 5 is
>> > repeated and as for, 81 the next digit after 7 is 0, so again 0 occurs
>> > twice. But for 38, the next digit after 9 is 4, and because it has NOT
>> > occurred before, I assume 38 is the correct answer... and I am wrong!
>> >
>> > I suspect I have completely misunderstood the question.
>> >
>> > Any ideas?
>> > Thanks!
>> >
>> > Kinuthia...
>> >
>> >
>> > --
>> > Ubuntu-ke mailing list
>> > Ubuntu-ke at lists.ubuntu.com
>> > https://lists.ubuntu.com/mailman/listinfo/ubuntu-ke
>> >
>>
>>
>>
>
>
> --
> Ubuntu-ke mailing list
> Ubuntu-ke at lists.ubuntu.com
> https://lists.ubuntu.com/mailman/listinfo/ubuntu-ke
>
More information about the Ubuntu-ke
mailing list