[Tutor] Fwd: Problem Euler 26
W W
srilyk at gmail.com
Tue Jul 1 11:48:56 CEST 2008
Just a tidbit:
A neat function my friend came up with last year to figure out the
length of a whole number (now converted to python for your viewing
pleasure):
from math import log10 as log
from math import floor
def findNumberLength(number):
number = float(number)
x = log(number)
x = floor(x)
x += 1
x = int(x)
return x
The only catch is that this only works for a whole (non-decimal) number:
>>>print findNumberLength(123)
3
>>>print findNumberLength(1234.5234)
4
Though you could easily split your number in two:
def splitNum(mynum):
mynum = str(mynum)
mynum = mynum.split('.')
for x in range(2):
mynum[x] = float(mynum[x])
return mynum
and then run the operation on both. A simple error check would
eliminate, say, working this on 123.0 (which would give you 4 digits,
and I suppose if you were looking for significant figures that would
work)
if (mynum % 1) == 0:
print "This number is X.0"
else:
splitNum(mynum)
Although, now that I think about it, if you were really lazy, you
could just convert the int to a str and run len on it. Heh... Can't do
that (easily) in c++.
*Shrugs* oh well.
HTH,
Wayne
On Tue, Jul 1, 2008 at 1:12 AM, Andre Engels <andreengels at gmail.com> wrote:
> On Sun, Jun 29, 2008 at 10:34 PM, kinuthiA muchanE <muchanek at gmail.com> wrote:
>
>> 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.
>
> Yes, it seems you have... I will try to rephrase:
>
> For each fraction x/y (y>x to get it below 1), its representation as a
> decimal fraction consists of two parts: first some arbitrary series of
> numbers, then a series of numbers that is repeated over and over
> again. For example, 1/55 = 0.018181818181818..., or in short 0.0(18) -
> the arbitrary series here is 0, the repeating part 18. You are asked
> to get the largest repeating part.
>
> Your solution finds the longest time before a digit is repeated
> instead, which is incorrect because the same digit can be used more
> than once in the repeating part (and also because it might occur once
> or more in the non-repeating part) - for example, the 1/38 that you
> mentioned equals:
> 0.0(263157894736842105)
>
> A hint for solving this problem: use methods similar to the once you
> use for a long division with pen and paper.
>
> --
> Andre Engels, andreengels at gmail.com
> ICQ: 6260644 -- Skype: a_engels
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
--
To be considered stupid and to be told so is more painful than being
called gluttonous, mendacious, violent, lascivious, lazy, cowardly:
every weakness, every vice, has found its defenders, its rhetoric, its
ennoblement and exaltation, but stupidity hasn't. - Primo Levi
More information about the Tutor
mailing list