Origami reaches the height of Mount Everest (for&while loop, closed function loop)

How many times do you fold a paper with a thickness of 0.1mm in half to reach a height of 8848180mm, the height of Mount Everest.

(This note is suitable for coders who are familiar with loops and lists)


[The details of learning are a joyful process]

  • Python official website: python cutting edge. Unfortunately it’s the original English version. So, I want to practice English reading. “>https://www.python.org/

  • Free: Free for big names “Bible” tutorial “ python complete self-study tutorial” is not just as simple as the basics…
    Address: https://lqpybook.readthedocs.io/

Self-study is not a mysterious thing, a person The time spent studying by oneself in a lifetime is always longer than the time spent studying in school, and the time without a teacher is always longer than the time with a teacher.
Hua Luogeng

  • My CSDN homepage, My HOT blog, My Python learning personal memo
  • Highly recommended for good articles, Lao Qi Classroom


It is better to chase the wind than to wait for it...


How many times to fold 0.1mm thick paper in half


Origami reaches the height of Mount Everest


(Height can reach 8848180mm, the height of Mount Everest)

Quality score of this article:


97
This article address:
https://blog.csdn.net/m0_57158496/article/details/134168298

CSDN quality score query entrance: http://www.csdn.net/qc


Directory

  • ◆?Origami reaches the height of Mount Everest
    • 1. Topic description
      • 1.1 Screenshot of the problem
      • 1.2 Screenshot “Defects”
    • 2. Algorithm analysis
      • 2.1 Question Analysis
      • 2.2 Python code (for & amp;power of 2)
      • 2.3 Loop×2
      • 2.4 Python bit operations<<1 instead of ×2
      • 2.5 Simple while
      • 2.6 Function self-calling (closure) loop

◆?Origami reaches the height of Mount Everest

1. Title description

1.1 Screenshot of the problem

  • Screenshot of question description

    How many times can a paper with a thickness of 0.1mm be folded in half to reach a height of 8848180mm, the height of Mount Everest

1.2 Screenshot “Defect”

[The question comes from the CSDN Q&A community question “Origami reaches the height of Mount Everest”]

  • The questioner is not familiar with loops yet, so he needs to read more books.

    ?
    The number of loops in this question is “uncertain”. For loops with an uncertain number of times, it is most suitable to use the while keyword to construct a loop body.


Back to table of contents

2. Algorithm analysis

  • Screenshot of code running effect

    This topic is a loop × 2, and there is no algorithm discussion. I tried to talk about using for and while to solve it through various code forms.

2.1 Question Analysis

  • Analyze the question and “dig” the meaning of the question. For programming problem solving, after “understanding” the problem, you have to carry out data design (structured data) and code expression according to the determined algorithm, and finally organize the statements to output the expected conclusion.
    ?
    1. When the paper in this question is folded in half, each increase in height is an increase in the power of 2.
    ?
    2. Another point is, pay attention to the description in parentheses “fold it in half again and it will exceed”. If the exit loop condition we set is h >= 8848180, the return value will be one less than the current number of loops, that is, print(k-1 ).
    ?
    3. Another point is that the initial value of paper thickness is 0.1 instead of 0. Why do you have to be so serious? Because it is possible that 0.1 may affect the result by folding in half.


Back to table of contents

2.2 Python code (for & amp;power of 2)

Python code (for)

mountQomolangma = 8848180

for i in range(1, int(1e8)): # For an uncertain number of loops, if you have to use for, just get a large value that you can think of. It is just a decoration anyway. I set 1 million here.
    paperHigh = 0.1*2**i + 0.1
    
    if paperHigh >= mountQomolangma:
        break #

print(f"\
Paper with a thickness of 0.1mm, folded in half {<!-- -->i} times, height {<!-- -->paperHigh}mm, folded in half {<!-- -->i-1 }can reach the height of Mount Everest {<!-- -->mountQomolangma}mm.")


The efficiency of writing code in this way will be low. Each loop is calculating the power of 2. You can use loop × 2 instead, which will be more efficient.


Back to table of contents

2.3 Loop×2

Loop×2 code

mountQomolangma = 8848180
paperHigh = 0.1

for i in range(1, int(1e8)): # For an uncertain number of loops, if you have to use for, just get a large value that you can think of. It is just a decoration anyway. I set 1 million here.
    paperHigh *= 2
    
    if paperHigh >= mountQomolangma:
        break #Exit the loop.

print(f"\
Paper with a thickness of 0.1mm, folded in half {<!-- -->i} times, height {<!-- -->paperHigh}mm, folded in half {<!-- -->i-1 }can reach the height of Mount Everest {<!-- -->mountQomolangma}mm.")


Every time it is ×2, it will be faster to use bit operations. If the binary number is shifted left by one bit <<1, the value it represents is doubled, thus completing the ×2 operation.


Back to table of contents

2.4 Python bit operations<<1 instead of ×2

Bit operation<<1 replaces ×2 code

mountQomolangma = 8848180
paperHigh = 1

for i in range(1, int(1e8)): # For an uncertain number of loops, if you have to use for, just get a large value that you can think of. It is just a decoration anyway. I set 1 million here.
    paperHigh <<= 1
    
    if paperHigh/10 >= mountQomolangma:
        break #Exit the loop.

print(f"\
Paper with a thickness of 0.1mm, folded in half {<!-- -->i} times, height {<!-- -->paperHigh}mm, folded in half {<!-- -->i-1 }can reach the height of Mount Everest {<!-- -->mountQomolangma}mm.")



Back to table of contents

2.5 Concise while

Or while loop, the code should be simpler, just put the while keyword in the if condition and then make the switch.

Concise while code

mountQomolangma = 8848180
paperHigh = 1
k = 0

while paperHigh/10 < mountQomolangma:
    k+=1
    paperHigh <<= 1

print(f"\
Paper with a thickness of 0.1mm, folded {<!-- -->k} times in height {<!-- -->paperHigh/10}mm, folded in half {<!-- -->k} -1} can reach the height of Mount Everest {<!-- -->mountQomolangma}mm.")


Back to table of contents

2.6 Function self-calling (closure) loop

Can a loop be implemented without using the for and while keywords? The answer is yes. In python, there is a "package" called "closure", which can implement data transfer like recursion. "Closure" is actually an encapsulated cyclic space structure formed by the self-invocation of a function, that is, a layer of "scopes", and data "penetrates" between each layer of scope through function parameters. Just like the cycle, the focus is on reasonable switch settings so that the "closure" can be "broken", narcissistic and "reversed" in a timely manner. Otherwise, we will really "enter the reincarnation of eternal destruction."

For the understanding of closures, I feel a little uncomfortable when looking at for and while. Regarding fundamental weaknesses in programming, coders with smaller coding capabilities are slightly less readable. But the code structure is still rigorous and concise.

Closure function implements loop

def fold(h, paper, k):
    
    if (paper + 1)/10 < h:
        return fold(h, paper<<1, k + 1)
    else:
        print(f"\
A paper with a thickness of 0.1mm, folded in half {<!-- -->k} times, the height is {<!-- -->(paper + 1)/10}mm, folded in half {<!-- -->k-1} can reach the height of Mount Everest {<!-- -->mountQomolangma}mm.")


if __name__ == '__main__':
    mountQomolangma = 8848180
    fold(mountQomolangma, 1, 0)


Back to top


Previous article:? Classic circular proposition: One hundred coins and one hundred chickens (One cub costs five coins, a female costs three coins, and three chicks cost one coin; One hundred coins and one hundred chickens (one hundred chickens and one hundred coins)
Next article:?

My HOT blog:

A total of 246 blog post note information were collected this time, with a total reading volume of 40.46w and an average reading volume of 1644. 16 blog post note index links with a reading volume of no less than 4,000 have been generated. Data collection was completed at 2023-10-12 05:41:03, taking 4 minutes and 41.10 seconds.

  1. First experience of ChatGPT domestic mirror site: chat, Python code generation, etc.
    ( 59262 reading)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/129035387
    Like: 126 Dislike: 0 Collection: 798 Reward: 0 Comment: 71
    This blog post was first published on 2023-02-14 23:46:33 and modified at the latest on 2023-07-03 05:50:55.
  2. The magic code that changes the nickname of QQ group
    ( 58086 reading)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/122566500
    Like: 24 Dislike: 0 Collection: 83 Reward: 0 Comment: 17
    This blog post was first published at 2022-01-18 19:15:08 and was modified at the latest at 2022-01-20 07:56:47.
  3. pandas data type DataFrame
    ( 9173 Reading)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/124525814
    Like: 6 Dislike: 0 Collection: 31 Reward: 0 Comment: 0
    This blog post was first published on 2022-05-01 13:20:17 and modified at the latest on 2022-05-08 08:46:13.
  4. Personal information extraction (string)
    ( 7215 Reading)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/124244618
    Like: 1 Dislike: 0 Collection: 13 Reward: 0 Comment: 0
    This blog post was first published on 2022-04-18 11:07:12 and modified at the latest on 2022-04-20 13:17:54.
  5. 7 ways to implement reverse order (descending order) of Python lists
    ( 7161 Reading)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/128271700
    Like: 5 Dislike: 0 Collection: 22 Reward: 0 Comment: 8
    This blog post was first published at 2022-12-11 23:54:15 and was modified at the latest at 2023-03-20 18:13:55.
  6. Roman Numeral Converter | Roman Numeral Generator
    ( 7035 Reading)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/122592047
    Like: 0 Dislike: 0 Collection: 1 Reward: 0 Comment: 0
    This blog post was first published at 2022-01-19 23:26:42 and was modified at the latest at 2022-01-21 18:37:46.
  7. Python string displayed in the center
    ( 6966 Reading)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/122163023
    Like: 1 Dislike: 0 Collection: 7 Reward: 0 Comment: 1
    Notes for this blog
  8. Recursive implementation and for implementation of Fibonacci sequence
    ( 5523 reads)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/122355295
    Like: 4 Dislike: 0 Collection: 2 Reward: 0 Comment: 8
    Notes for this blog
  9. python clear screen
    ( 5108 reads)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/120762101
    Like: 0 Dislike: 0 Collection: 8 Reward: 0 Comment: 0
    Notes for this blog
  10. Exercise: String statistics (pit: fstring error report)
    ( 5103 reads)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/121723096
    Like: 0 Dislike: 0 Collection: 1 Reward: 0 Comment: 0
    Notes for this blog
  11. Carriage return, line feed, and carriage return and line feed
    ( 5093 reads)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/123109488
    Like: 1 Dislike: 0 Collection: 2 Reward: 0 Comment: 0
    This blog post was first published at 2022-02-24 13:10:02 and modified at the latest at 2022-02-25 20:07:40.
  12. Exercise: Nim game (smart version/fool style? Man-machine battle)
    ( 4943 reads)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/121645399
    Like: 14 Dislike: 0 Collection: 42 Reward: 0 Comment: 0
    Notes for this blog
  13. Password strength detector
    ( 4323 reads)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/121739694
    Like: 1 Dislike: 0 Collection: 4 Reward: 0 Comment: 0
    This blog post was first published at 2021-12-06 09:08:25 and modified at the latest at 2022-11-27 09:39:39.
  14. Exercise: Generate 100 random positive integers
    ( 4274 reads)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/122558220
    Like: 1 Dislike: 0 Collection: 6 Reward: 0 Comment: 0
    This blog post was first published at 2022-01-18 13:31:36 and was modified at the latest at 2022-01-20 07:58:12.
  15. My Python.color() (Python color printing control)
    ( 4159 reads)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/123194259
    Like: 2 Dislike: 0 Collection: 8 Reward: 0 Comment: 0
    This blog post was first published on 2022-02-28 22:46:21 and modified at the latest on 2022-03-03 10:30:03.
  16. Roman numeral converter (implemented using the modulus of the value of the Roman numeral construction element)
    ( 4149 reads)
    Blog address: https://blog.csdn.net/m0_57158496/article/details/122608526
    Like: 0 Dislike: 0 Collection: 0 Reward: 0 Comment: 0
    This blog post was first published at 2022-01-20 19:38:12 and modified at the latest at 2022-01-21 18:32:02.


Recommendation conditions
Reads exceeded 3,000


(For more hot blogs, please click on the blue text to jump to read)


Back to top



Lao Qi comic avatar

Excellent articles:

  • Recommended articles: Qi Wei Shu Manuscript "Python Complete Self-Study Tutorial" FreeSerial(has been completed and compiled into a book, and there is also a PDF version for permanent sharing on Baidu Netdisk, Click Jump for free download.)
  • Three major features of OPP: property in encapsulation
  • Understanding python' through built-in objects
  • regular expression
  • The role of "*" in python
  • Python complete self-study manual
  • walrus operator
  • `!=` in Python is different from `is not`
  • The right way to learn programming

Source: Lao Qi Classroom


Back to top

◆ Python Getting Started Guide[Python 3.6.3]

Highly recommended for good articles:

  • A high-quality creator in the full-stack field - [Hanlao](still a student in a domestic university) blog post "Non-technical article - about English and how to speak correctly "Asking questions", "English" and "Being able to ask questions" are two powerful tools for learning programming.
  • [Applicable fields of 8 major programming languages] Don’t rush to choose linguistic programming first, first look at what they can do
  • Good habits of reliable programmers
  • The three major elements of "Function function, end condition, and function equivalence" in the high-quality and good article by the boss Shuaidi will help you understand recursion.

CSDN Practical Tips Blog:

  • 8 Python practical tips that are extremely useful
  • python ignore warning
  • Python code writing specifications
  • Python's docstring specification (standard way of writing documentation)