본문 바로가기

데이터 - 기본 코드 및 알고리즘 연습/Codewar

[Codewars] Product of consecutive Fib numbers

▶  문제 : 

▶  내 답안 : 

def F(n) :
    a, b = 0, 1 
    for i in range(n): 
        a, b = b, a+b 
    return a

def productFib(prod):
    answer = []
    n = 0 
    while True :
        r_1 = F(n)
        r_2 = F(n+1)
        if r_1*r_2 > prod :
            return [r_1, r_2, False]
        if r_1*r_2 == prod :            
            return [r_1, r_2, True]
        n += 1

▶  모범답안 : 

def productFib(prod):
  a, b = 0, 1
  while prod > a * b:
    a, b = b, a + b
  return [a, b, prod == a * b]

▶  배워야할 부분 : while 문

 

'데이터 - 기본 코드 및 알고리즘 연습 > Codewar' 카테고리의 다른 글

[Codewars] Square Every Digit  (0) 2021.06.30
[Codewars] Connect Four  (0) 2021.06.28
[Codewars] Kebabize  (0) 2021.06.23
[Codewars] Integers: Recreation One  (0) 2021.06.22
[Codewars] Build Tower  (0) 2021.06.21