LoginSignup
0
0

More than 5 years have passed since last update.

フィボナッチ数と素数の実装(python)

Last updated at Posted at 2016-12-03

復習。。。

from numpy import *

してます。

フィボナッチ数

再帰なし

def Fibonacci(x):
    if x == 0:
        return array([0])
    prepre, pre = 0, 1
    lst = [0, 1]
    for i in range(1, x):
        pre, prepre = pre + prepre, pre
        lst += [pre]
    return array(lst)

再帰あり

def Fibonacci_recursive(x):
    lst = []
    def f(x, lst, is_add=False):
        if x > 1:
            if is_add:
                lst += [f(x-1, lst, True) + f(x-2, lst)]           
            return f(x-1, lst) + f(x-2, lst)
        elif x == 1:
            if is_add:
                lst += [0, 1]
            return 1
        else:
            if is_add:
                lst += [0]
            return 0
    f(x, lst, True)
    return array(lst)

素数

def prime(N):
    a = arange(2, N+1)
    for i in range(2, N+1): 
        a = a[where((a <= i) + (a% i != 0))]
    return a

for文を使わず実装する方法を思いつきたいです。

0
0
4

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0