Python Code Shortening
Asked Answered
F

4

6

I was trying to solve this problem here :- https://www.spoj.pl/problems/PHIVAL/

The questions asks you to output as many decimal digits of the golden ratio (1+sqrt(5))/2 as possible and also try to minimise the code length.

This is what I have right now. Can this code be made any shorter ?

from decimal import *
getcontext().prec=7050
print(1+Decimal(5).sqrt())/2
Flyaway answered 18/4, 2011 at 15:41 Comment(5)
the fastest way to do it would be to have a webpage that spits out 1 million digits of phi and have your program just be echo curl mysite.com\phi :-)Serpasil
Why set precision to 7050? Why not 9999? Just as short code-wise, but many more digits.Magnification
@delnan It would exceed the timelimit.Flyaway
@glowcoder .. very clever :) .. But that wouldn't work on the spoj.Flyaway
For reference, codegolf.stackexchange.com has a whole bunch of these spoj.pl problems on themDobb
B
3

You can take out the space before the asterisk.

Update:

You added the part about insignificant whitespace, so I started thinking about a different approach. If whitespace isn't counted, you may be able to do something like this

print"1."+`map(len,"""      








       """.split("\n"))`[1::3]

It encodes each digit as a number of spaces on a line in a multi-line string constant. Obviously, you could add more lines to get more digits. It should run pretty fast, as there is very little calculation done. It uses 50 (update 2: 45) non-whitespace characters to produce any number of digits output.

Buddybuderus answered 18/4, 2011 at 15:46 Comment(3)
Thanks. But the source length is counted as the number of non-white space characters.Flyaway
@jack_carver: With the new information about white space, I came up with another approach that may give better results.Buddybuderus
You can make any python code to just 44 characters (or 42 in python2) if white space is not counted pastebin.com/uHR9TwQTRadiotransparent
D
1

Taking recursive's approach to an extreme, this uses just 19 non-whitespace characters:

print '1.%d'%len('                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          ')

Granted, the code required to generate the first 1000000 digits would be over 10^1000000 characters in length!

Deodar answered 18/4, 2011 at 23:39 Comment(0)
B
0

Due to high score for short code, i think that best approach could be just

print 1
Bona answered 18/4, 2011 at 16:14 Comment(1)
score(ndigits=1, codelen=6) -> 2500001, score(7050, 68) -> 227638 There must be "at least 1000 [digits] after the decimal point."Vinosity
V
0

Well I tried javascript-ish approach, and it apparently doesn't work in Python:

import decimal
decimal.__dict__.values()[17]().prec = 7050
...

Looks like your code is pretty close to the shortest possible solution.

Villenage answered 18/4, 2011 at 16:31 Comment(1)
Yes. But due to the varied nature of the formula I cannot exactly figure out the minimal source length. The best python solution there has a score of "307962" Where as mine gives "212528".Flyaway

© 2022 - 2024 — McMap. All rights reserved.