최소공배수 1부터 20까지 숫자 모두로 나누어도 몫이 0인 가장 작은 숫자를 구하라. """ Smallest multiple 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? """ if __name__ == '__main__' : num = 20 while True : for d in range ( 1 , 21 ): if num % d != 0 : break else : print (num) break num += 20 https://projecteuler.net/problem=5 연산 횟수를 줄이고자, 시작값을 20으로 하고, 증분도 20으로 하였다.
회문 구하기 두 개의 3자리 숫자의 곱으로 만들어지는 가장 큰 회문을 구하라. """ Largest palindrome product A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two 3-digit numbers. """ def is_palindrome (n): s = str (n) s2 = str () i = len (s) - 1 while i >= 0 : s2 += s[i] i -= 1 return s == s2 if __name__ == '__main__' : maxi = 0 for a in range ( 100 , 1000 ): for b in range ( 100 , 1000 ): num = a * b if is_palindrome(num): print (num) if num > maxi: maxi = num print ( "max = " , maxi) https://projecteuler.net/problem=4 회문 여부를 판별하기 위한 함수를 정의하였다. 함수가 입력 받은 숫자를 문자열로 변환 후 순서를 뒤집는다.
a = True print (type(a), a) # <class ' bool '> True a = False print (type(a), a) # <class ' bool '> False True는 참이고 False는 거짓이다. a = [ 1 , 2 ] b = [ 1 , 2 ] print (a == b) # True print (a is b) # False 동등 비교 연산자 == 는 값을 비교하는 것이고, is 연산자는 변수의 ID값을 비교하는 것이다.
댓글
댓글 쓰기