0

ユーザー入力「n」があり、平方根を見つけています。私が知っているのはmath.sqrt(n)ですが、プログラムが2未満になるまで平方根を見つけ続けるようにする必要があります。カウンター。私はパイソンを使用しています。

ここのところ:

import math
root = 0
n = input('Enter a number greater than 2 for its root: ')

square_root = math.sqrt(n)
print 'The square root of', n, 'is', square_root

keep_going = 'y'

while keep_going == 'y':
    while math.sqrt(n) > 2:
        root = root + 1
4

4 に答える 4

1
import math
user_in = input()
num = int(user_in)

num = math.sqrt(num)
count = 1
while(num > 2):
  num = math.sqrt(num)
  count += 1

print count
于 2011-03-29T02:46:43.750 に答える
0

2.x を想定

count = 0
user_input = int(raw_input("enter:"))
while true:    
    num = math.sqrt( user_input )
    if num < 2: break
    print num
    count+=1
print count    

ユーザー入力のエラー チェックが存在しません。

于 2011-03-29T02:56:35.530 に答える
0

明白な方法:

def sqrtloop(n):
    x = 0
    while n >= 2:
        n **= 0.5
        x += 1
    return x

それほどではありません:

def sqrtloop2(n):
    if n < 2:
        return 0
    return math.floor(math.log(math.log(n, 2), 2)) + 1
于 2011-03-29T06:23:32.263 に答える