Created
May 9, 2011 14:19
-
-
Save bradmontgomery/962589 to your computer and use it in GitHub Desktop.
Short python script to show that if you could fold a piece of paper in half 50 times, its thickness will be 3/4 the distance from the Earth to the Sun (71 million miles). Discovered on a very interesting Quora question.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Folding paper in half 50 times is 3/4 of the distance from earth to sun | |
From: | |
http://www.quora.com/What-are-some-of-the-most-mind-blowing-facts#answer_526501 | |
Thickness of a sheet of paper: 0.1 mm (~0.004 inches) | |
""" | |
def fold(num_folds=50, thickness=0.1): | |
"""Simulate folding a sheet of paper in half. | |
``num_folds`` is the number of times we'll fold the paper in half. | |
``thickness`` is the the thickness of a sheet of paper (in millimeters). | |
""" | |
for i in range(1, num_folds + 1): | |
thickness = thickness * 2 | |
if thickness >= 10000: | |
# convert to kilometers | |
t = thickness / 10000.0 | |
units = 'km' | |
elif thickness >= 1000: | |
# convert to meters | |
t = thickness / 1000.0 | |
units = 'm' | |
elif thickness >= 100: | |
# convert to centimeters | |
t = thickness / 100.0 | |
units = 'cm' | |
else: | |
# keep in millimeters | |
t = thickness | |
units = 'mm' | |
fmt_string = '{n} folds, thickness = {thickness:G} {units}' | |
print(fmt_string.format(n=i, thickness=t, units=units)) | |
if __name__ == "__main__": | |
n = input("\n\nHow many folds? ") | |
fold(n) |
Please correct your code. It must be:
if thickness >= 1000000:
# convert to kilometers
t = thickness / 1000000.0
units = 'km'
elif thickness >= 1000:
# convert to meters
t = thickness / 1000.0
units = 'm'
elif thickness >= 10:
# convert to centimeters
t = thickness / 10.0
units = 'cm'
else:
# keep in millimeters
t = thickness
units = 'mm'
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Updates:
ndigits
parameter; using the:G
formatting option to round/format outputpython paper.py