テーマノート:推測-検証シリーズ1

2348 ワード

コンピュータの様々なアルゴリズムには「反復」(Iteration)というアルゴリズムがある.このアルゴリズムは,コンピュータの「1つのボールが着地したときに10億回計算できる」という特性を利用して,問題を絶えず循環的に検証している.これはまた「推測−検証」(Guess and Check)法として抽象化される.
About Guess and Check Method:
  • Guess and check methods can work on problems with a finite number of possibilities

  • さらに,推測−検証手法の本質は窮挙法(Exhaustive enumeration)である.人間の脳の特性は計算の面で私たちが窮挙法を使う代価が非常に大きいことを決定したが、コンピュータは簡単に実現できる.
    About Exhaustive Enumeration:
  • Exhaustive enumeration is a good way to generate guesses in an organized manner
  • literally walk through all possible values of some parameter, some elements of the computation
  • test everything until find the right answer

  • では、これらの思想はコンピュータのプログラミングに反映されてどのような様子を体現しているのでしょうか.また、教育言語はPythonであり、より正確にはPython言語に反映されているのはどのようなものなのでしょうか.答え:ループ(loop)
    about loop characteristics:
  • Need a loop variable
  • - Initialized outside loop 
    - Change within loop 
    - Test for termination depends on variable 
    
  • Useful to think about a decrementing function
  • - Maps set of program variables into an interger
    - When loop is entered, value is non-negative (use absolute )
    - When value is <= 0, loop terminates 
    - Value is decreased every time through loop
    

    Looping structure compares to branching structure:
  • Branching structure (conditionals) let us jump to different pieces of code based on a test
  • - program are constant time
    
  • Looping structure (e.g. while) let us repeat pieces of code until a condition is satisfied (be false)
  • programs now take time depends on values of varibles as well as length of program

  • Instances of while loop:
    num = 10
    while True:
        if num < 7:
            print ('Breaking out of loop')
            break
        print (num)
        num -= 1
    print ('Outside of loop')
    
    num = 0
    while num <= 5:
        print (num)
        num += 1
    
    print ("Outside of loop")
    print (num)
    
    num = 2
    while num < 12:
        print (num)
        num += 2
    print ('Goodbye!')
    
    num = 10
    print ('Hello!')
    while num >0:
        print (num)
        num -= 2
    
    total = 0
    current =1
    end = int(input('enter your postive num:'))
    while current <= end:
        total += current
        current += 1
    print(total)