This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def largest_prime_under(n): | |
while n >= 2: #exit loop when all iterations have been exhausted | |
# If a number is not prime, it can be factored into two factors (let's call them x and y). | |
# If both x and y are greater than the square root of n, then x*y > n, | |
# so at least one of those factors must be less than or equal to the square root of n, | |
# and to check if n is prime, we only need to test for factors less than or equal to the square root: | |
if all(n % x for x in range(2, int(n ** 0.5 + 1))): | |
print (n) #print and break if the test passes | |
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fibonacci = [1,1] #start fibonacci with the first two elements | |
i = 1 #start iterator at second number in the series | |
while (fibonacci[i] + fibonacci[i-1]) < 10000: #while the number to be appeneded is less than 10 grand | |
fibonacci.append(fibonacci[i] + fibonacci[i-1]) #append the number | |
i += 1 #increment the iterator | |
print(fibonacci) #print the list |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fibonacci = [1,1] | |
i = 1 | |
while (fibonacci[i] + fibonacci[i-1]) < 10000: | |
fibonacci.append(fibonacci[i] + fibonacci[i-1]) | |
i += 1 | |
print(fibonacci) |