• Home
  • About
    • JOOS photo

      JOOS

      Joos's blog

    • Learn More
    • Email
    • Github
  • Posts
    • All Posts
    • All Tags
  • Projects

[hacker_rank] Extra Long Factorials with python3

15 Nov 2018

Reading time ~1 minute

문제 : https://www.hackerrank.com/challenges/extra-long-factorials/problem

solution 1

def extraLongFactorials(n):
    return reduce(lambda x, y: x*y, range(1, n+1))

solution 2

import sys
sys.setrecursionlimit(10**6)
read = lambda : sys.stdin.readline().strip()


def get_factorial2(n):
    old = [1]
    current = []
    for i in range(2, n+1):
        carry = 0
        print(old)
        for digit in old:
            prod = digit * i + carry
            current.append(prod % 10)
            carry = prod // 10
            # 몫

        if carry != 0:
            for j in str(carry)[::-1]:
                # carry 거꾸로 뒤집기
                current.append(int(j))  # add j as a str
        old = current
        current = []

    return "".join(map(str,old[::-1])) # convert everything to str before concatenation and returning the results

print(get_factorial2(int(read())))



algorithmhacker_rankpython Share Tweet +1