pythoncode-snippetscode-golf

Python Code Shortening


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

Solution

  • 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.