Project Euler - 5

최소공배수 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으로 하였다.

Project Euler - 4

회문 구하기 두 개의 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 회문 여부를 판별하기 위한 함수를 정의하였다. 함수가 입력 받은 숫자를 문자열로 변환 후 순서를 뒤집는다.

Project Euler - 3

최대 소인수 600851475143의 소인수 중에서 최대 값을 구하라. """ Largest prime factor The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? """ class PrimeNumber: def __init__ ( self ): self .current = 1 self .num_set = set () def __iter__ ( self ): return self def __next__ ( self ): self .current += 1 while True : for i in self .num_set: if self .current % i == 0 : self .current += 1 break # Break current for loop else : break # Break the while loop and return self .num_set.add( self .current) return self .current if __name__ == '__main__' : number = 600851475143 prime_number = PrimeNumber() for n in prime_number: if n > number: break if number % n == 0 : print (n) https://projecteuler.net/proble

Project Euler - 2

피보나치 수 4백만 미만의 피보나치 수 중에서 짝수인 것들의 합을 구하라. """ Even Fibonacci numbers Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ... By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. """ class Fibonacci: def __init__ ( self ): self .a = 0 self .b = 1 def __iter__ ( self ): return self def __next__ ( self ): c = self .a + self .b if self .a > self .b: self .b = c else : self .a = c return c if __name__ == '__main__' : s = 0 fibonacci = Fibonacci() for n in fibonacci: if n >= 4000000 : break else : if n % 2 == 0 : s += n print (s) https://projecteuler.net/problem=2 피보나치 수열을 구하기 위해서 Iterator

Project Euler - 1

3과 5의 배수 1000 미만의 자연수 중, 3과 5의 배수의 합을 구하라. https://projecteuler.net/problem=1 """ Multiples of 3 and 5 If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. """ if __name__ == '__main__' : s = 0 for i in range ( 1 , 1000 ): if i % 3 == 0 or i % 5 == 0 : s += i print (s)

정규표현식

#!/ usr /bin/python3 # 정규표현식 # by Elex (http://python.elex.pe.kr/) import re def main ():         # 패턴 컴파일     pattern = re.compile( "([\.0-9a-z_-]+)@([0-9a-z_-]+)(\.[0-9a-z_-]+){1,2}" , re.IGNORECASE)         txt = "someone's email address is email @email.com "     # 찾기     match = re.search(pattern, txt)     # 또는     match = re.search( "([\.0-9a-zA-Z_-]+)@([0-9a-zA-Z_-]+)(\.[0-9a-zA-Z_-]+){1,2}" , txt)             if match:            print ( '{} is valid email address.' .format(match.group()))     # 바꾸기     print (re.sub( "([\.0-9a-zA-Z_-]+)@([0-9a-zA-Z_-]+)(\.[0-9a-zA-Z_-]+){1,2}" , "*****" , txt))     # 또는     if match:         print (txt.replace(match.group(), "*****" ))                if __name__ == "__main__" : main() 파이썬에서 정규표현식 관련 기능은 re라는 모듈을 통해서 제공된다. 패턴 문자열을 컴파일한 후에 search() 메서드를 사용할 수도 있고, search() 메서드에 패턴 문자열을 직접 지정해서 사용할 수도 있다. search() 메서드는 일치 결과를 반환하는데 일치하

문자열

#!/ usr /bin/python3 # 문자열 # by Elex (http://python.elex.pe.kr/) def main ():         print ( "This is a string." .upper()) # THIS IS A STRING.     print ( "This is a string." .lower()) # this is a string.     print ( "this is a string." .capitalize()) # This is a string.     print ( "This Is A String." .swapcase()) # tHIS iS a sTRING.         print ( "This is a string." .find( "is" )) # 2     print ( "This is a string." .replace( "This" , "That" )) # That is a string.             print ( "  This is a string.  " .strip()) # This is a string.     print ( "  This is a string.  " .strip( "\n" )) #   This is a string.      print ( "  This is a string.  " .rstrip()) #   This is a string.     print ( "  This is a string.  " .lstrip()) # This is a string.        print ( "This is a string." .isa