#https://projecteuler.net/ --> source of this script #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. def mul_sum(n): result = 0 for i in range(1,n): if i % 3 == 0 or i % 5 == 0: result = result + i return result #print(mul_sum(1000)) #Problem 2 : 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. def max_fib(n, m): a = [1, 2] while a[-1] + a[-2] < n + 1: a.append(a[-1] + a[-2]) if m == 1: result = 0 for i in range(0, len(a)): if a[i] % 2 == 0: result = result + a[i] return result if m == 2: result = 0 for i in range(0, len(a)): if a[i] % 2 != 0: result = result + a[i] return result else: return "second value must be 1 or 2" #print(max_fib(4000000, 1)) #Problem 3: Largest prime factor #The prime factors of 13195 are 5, 7, 13 and 29. #What is the largest prime factor of the number 600851475143 ? def max_pf(n): a = list() b = set() for i in range(1, n + 1): if n % i == 0: a.append(i) print(a) for i in range(0, len(a)): chk = True for j in range(2, a[i]): if a[i] % j == 0: chk = False break if chk: b.add(a[i]) return b #print(max_pf(10)) def prime_factor(n): a = list() for i in range(2, n+1): chk = True for j in range(2, i): if i % j == 0: chk = False break if chk: a.append(i) return a print(prime_factor(10))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question