def scratch(): print(utils.fib(1)) print(utils.fib(10)) print(utils.fibs_ub(90)) print(utils.fibs(10)) print(utils.fibs_ub(4000000)) return
def avm_ls(self, inputs, index): x = inputs[index] fitness = self.get_f(inputs, index, x) # print(x) if self.get_f(inputs, index, x - 1) >= fitness and self.get_f( inputs, index, x + 1) >= fitness: # if self.satisfied_condition(x): return x, fitness k = -1 if self.get_f(inputs, index, x - 1) < self.get_f(inputs, index, x + 1) else 1 while self.get_f(inputs, index, x + k) < self.get_f(inputs, index, x): if fitness < 0: break fitness = self.get_f(inputs, index, x + k) x = x + k k = 2 * k l = min(x - k / 2, x + k) r = max(x - k / 2, x + k) n = min_n(r, l) while n > 3: if l + fib(n - 1) - 1 <= r and self.get_f( inputs, index, l + fib(n - 2) - 1) >= self.get_f( inputs, index, l + fib(n - 1) - 1): l = l + fib(n - 2) n -= 1 x = l return x, fitness
def main(): i = 1 result_sum = 0 while True: f = fib(i) i += 1 if f < 4000000: if is_even(f): result_sum += f else: break print result_sum
def next_level_xp(cls): """ xp needed == sum(fib(next_level))*10 """ next_level = cls.Level.value + 1 if next_level == 1: return 10 else: needed = 0 for f in fib(next_level): needed += f * 10 print(f"xp needed for lvl {next_level}: {needed}") return needed
def fibonacci(k: int): logging.info('') res, err = None, None if 0 <= k <= 12000: res = redis.get(str(k)) if res: logger.info(' GET DATA FROM REDIS') res = int(res) else: logger.info(' CALCULATING NEW VALUE') res = fib(k) redis.set(str(k), res) else: err = 'Wrong value. Must be 0<=k<=12000' logger.info(f' GOT WRONG ARGUMENT <{k}>') return {'res': res, 'error': err}
def problem2(): """ 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. """ seq = fib() x = seq.next() even_terms = [] total_sum = 0 while x < 4000000: # 4,000,000 if x % 2 == 0: total_sum += x x = seq.next() return total_sum
async def cpubounded(_): res = str(utils.fib()) return web.Response(text=res)
async def get(self): res = str(utils.fib()) self.set_header('Content-type', 'text/plain') self.write(res) await self.flush()
def main(): for i, f in enumerate(fib()): if len(str(f)) == 1000: return i+1
# Correct answer: 4613732 from utils import fib x = sum = 0 while True: x += 1 current = fib(x) if current >= 4000000: break if current % 2 == 0: sum += current print sum
from utils import fib import sys if __name__ == '__main__': print(sys.argv) n = int(sys.argv[1]) print(fib(n=n))
from utils import fib counter = 0 for val in fib(): if val >= len(str(val)) >= 1000: break counter += 1 print counter
def test_challenge4(self): print(fib(15)) print(numToString(fib(15)))
#!/usr/bin/env python from utils import fib def test_pandigital(num): s = set(num) return ((len(s) == 9) and '0' not in s) if __name__ == "__main__": gen = fib() num = gen.next() cond = True k = 1 while cond: if num > 1e18: tail = num % 1000000000 tt = test_pandigital(str(tail)) if tt: head = str(num)[:9] th = test_pandigital(head) print k print "> ", head, tail, th, tt if th: cond = False k += 1 num = gen.next()
from utils import fib end_case = set('%d' % i for i in range(1, 10)) def is_pan_digital(slug): if set(slug) == end_case: return True return False counter = 0 for val in fib(a=1, b=1): counter += 1 str_val = str(val) if is_pan_digital(str_val[:9]) and is_pan_digital(str_val[-9:]): break print counter # Too slow
""" Project Euler Problem 2 ======================= 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, ... Find the sum of all the even-valued terms in the sequence which do not exceed four million. """ from utils import fib print sum(i for i in fib(4000000) if i%2==0)
from utils import is_prime2 as isprime, fib def next_prime(n): n += 1 while not isprime(n): n += 1 return n a = 10**14 A = [] limit = 10**5 for _ in range(limit): x = next_prime(a) A.append(x) a = x mod = 1234567891011 print(sum(fib(n, mod) for n in A) % mod)
from utils import fib a = 0 b = 1 i = 0 limit = 10**9 digits = set("123456789") while True: i += 1 a, b = b, a + b a %= limit b %= limit if set(str(a)) == digits: if set(str(fib(i))[:9]) == digits: print i break
def first_fib_term(ndigits=1000): term = 1 while len(str(fib(term))) < ndigits: term += 1 return term
def on_get(self, req, resp, number): result = {'number': number, 'Fibonacy': fib(int(number))} resp.body = json.dumps(result)
from utils import fib total = 0 n = 1 thisfib = fib(n) while(thisfib <= 4000000): if (thisfib % 2 == 0): total = total + thisfib n = n + 1 thisfib = fib(n) print total
def main(): return sum(x for x in take_upto(4000000, fib()) if not x % 2)