Skip to content

Instantly share code, notes, and snippets.

@bradmontgomery
Created May 9, 2011 14:19
Show Gist options
  • Save bradmontgomery/962589 to your computer and use it in GitHub Desktop.
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.
"""
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)
@bradmontgomery
Copy link
Author

bradmontgomery commented May 11, 2011 via email

@bjarkeeck
Copy link

1 fold = 0.01
2 fold = 0.02
Then 3 fold = 0.04 and not 0.03 right?

@bradmontgomery
Copy link
Author

@BjarkeCK I just noticed your comment. Yes, you're correct, but the default number of significant digits (the ndigits parameter) causes a rounding error, there. A more precise example might be:

>>> fold(5, ndigits=3)
1 folds, thickness = 0.008 inches
2 folds, thickness = 0.016 inches
3 folds, thickness = 0.032 inches
4 folds, thickness = 0.064 inches
5 folds, thickness = 0.128 inches

@bradmontgomery
Copy link
Author

Better yet, we should use millimeters, centimeters, kilometers instead an antiquated system of measurements ;)

@bradmontgomery
Copy link
Author

Updates:

  • Switched to metric units.
  • Removed the ndigits parameter; using the :G formatting option to round/format output
  • Can now be run interactively; e.g. with python paper.py

@rohithill
Copy link

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