Пример #1
0
def fibSequence(start, maximum):
  i   = start
  cur = euler.fib(i)
  while cur < maximum:
    yield cur
    i += 1
    cur = euler.fib(i)
Пример #2
0
def main():
    """
    >>> main()
    4782
    """
    fibs = izip(fib(), count(1))
    print(next(dropwhile(lambda f: int(log10(f[0])) + 1 < 1000, fibs))[1])
Пример #3
0
def get_fib_nums(end):
    ''' Return a list of all Fibonacci numbers up to 'end' '''
    nums = []
    for n in fib():
        if n > end:
            break
        nums += [n]
    return nums
Пример #4
0
def main():
    ''' Driver function '''
    index = 1
    for f in fib():
        if len(str(f)) == 1000:
            break
        index += 1
    print(index)
Пример #5
0
def euler2():
    result = 0
    i = 0
    current = 0
    while current < 4e6:
        i += 1
        current = fib(i)
        if current % 2 == 0: result += current
    print(result)
Пример #6
0
def main():
    ''' Driver function '''
    total = 0
    end = 4 * 10**6
    for f in fib():
        if not f % 2:
            total += f
        if f > end:
            break
    print(total)
Пример #7
0
def sumEvenFibsLessThan(num):
    fibs = []
    i = 1
    while True:
        f = euler.fib(i)
        i += 1
        if f < num:
            fibs.append(f)
        else:
            break

    return sum([n for n in fibs if n % 2 == 0])
Пример #8
0
def main():
    """
    >>> main()
    4613732
    """
    limit = 4000000

    print(
        sum(
            (x for x in takewhile(lambda x: x < limit, fib()) if x % 2 == 0)
        )
    )
Пример #9
0
def main():
    """
    >>> main()
    2252639041804718029
    """
    limit = 10 ** 17

    fibs = list(takewhile(lambda x: x < limit, fib()))
    zrts = {1: 0}

    def sum_z(number):
        """
        sum_z computes the sum of the zeckendorf representations terms up
        to the given number.
        """
        if number not in zrts:
            f = max((f for f in fibs if f < number))
            zrts[number] = number - f + sum_z(number - f) + sum_z(f)
        return zrts[number]

    print(sum_z(limit))
Пример #10
0
def euler25():
    print(next(i for i in count() if len(str(fib(i))) >= 1000))
Пример #11
0
from itertools import count, izip
from euler import fib, findfirst

# The Fibonacci sequence is defined by the recurrence relation:
#
#     Fn = Fn-1 + Fn-2, where F1 = 1 and F2 = 1.
#
# Hence the first 12 terms will be:
#
#     F1 = 1
#     F2 = 1
#     F3 = 2
#     F4 = 3
#     F5 = 5
#     F6 = 8
#     F7 = 13
#     F8 = 21
#     F9 = 34
#     F10 = 55
#     F11 = 89
#     F12 = 144
#
# The 12th term, F12, is the first term to contain three digits.
#
# What is the first term in the Fibonacci sequence to contain 1000 digits?

# we need to increase with two because fib() starts with 1 and 2, not 1 and 1
LEN = 1000
print findfirst(izip(count(), fib()), lambda x: len(str(x[1])) >= LEN)[0] + 1
Пример #12
0
def fib_generator():
    i = 1
    while True:
        yield fib(i)
        i += 1
Пример #13
0
import euler

consider = [euler.fib(n) for n in range(70) if euler.fib(n) < 4000000 and euler.fib(n)%2 ==0]
print sum(consider)
Пример #14
0
import euler

consider = [
    euler.fib(n) for n in range(70)
    if euler.fib(n) < 4000000 and euler.fib(n) % 2 == 0
]
print sum(consider)
Пример #15
0
import sys
sys.path.append("../projecteuler")
from euler import fib

f = str(fib(int(sys.argv[1])))
ans = ""
for i in range(0, len(f), 20000):
	ans += f[i]
print(ans)
Пример #16
0
import euler

n = 1

while len(str(euler.fib(n))) < 1000:
    n += 1

print n + 1
Пример #17
0
#!/usr/bin/env python

# Copyright (c) 2008 by Steingrim Dovland <*****@*****.**>

from euler import fib

# 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.

terms = [ x for x in fib(4*1000*1000) if x % 2 == 0 ]
print sum(terms)
        
Пример #18
0
def fib_gen2():
    i = 0
    while fib(i) < 4 * 10**6:
        if fib(i) % 2 == 0:
            yield fib(i)
        i += 1
Пример #19
0
#!/usr/bin/python
# coding: UTF-8
"""
@author: CaiKnife

Even Fibonacci numbers
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, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
"""

from euler import fib

MAX = 4000000
i = 1
results = []
while fib(i) < MAX:
    if not fib(i) % 2:
        results.append(fib(i))
    i += 1

print sum(results)
Пример #20
0
#!/opt/local/bin/python
from euler import fib
from itertools import dropwhile

print dropwhile(lambda (a, b): len(str(b)) < 1000, enumerate(fib())).next()[0]