Exemplo n.º 1
0
int fib_rec(int n)
{
	int rez;
	if (n < 2)
		return 1;
	
	rez = fib_rec(n-1) + fib_rec(n-2);

	return rez;
}
Exemplo n.º 2
0
void
fib(void)
{
    msg_t msg_struct;
    msg_t *msg = &msg_struct;
    int n = 0;
    int i = 0;

    read_message_by_type(msg, MSG_TYPE_ARGUMENT, 100);
    if (msg_type_is_invalid(msg))
    {
        print_usage();
        return;
    }

    n = atoi(msg_data_get_string(msg));
    if (n <= 0)
    {
        print_usage();
        return;
    }

    for (i = 0; i <= n; ++i)
    {
        sleep(100);

        print_str("fib(");
        print_int(i);
        print_str(") = ");
        print_int(fib_rec(i));
        print_strln("");
    }
}
Exemplo n.º 3
0
/* Calculate the nth fibonacci number and print it to the kernel log */
static int fib_calculate(struct file *file, const char *user_buffer,
        unsigned long buf_count, void *data)
{
        int n, i, fib1=0, fib2=1, tmp;
        char kernel_buf[10];                    /* Expect max 10 chars */
        char *endp;

        if (buf_count > sizeof(kernel_buf))
                return -EINVAL;

        /* Copy data from user-space via proc file entry */
        if (copy_from_user(kernel_buf, user_buffer, buf_count))
        {
                printk ("Copy from user error\n");
                return -EFAULT;
        }

        n  = (int)(simple_strtoul(kernel_buf, &endp, 10));
        if (*endp != '\n')
        {
                return -EINVAL;
        }

        if(n<=0)
                printk("Invalid value entered\n");
        else
        {
                fib1=fib_rec(n);
                printk("The %dth Fibonacci number is: %d\n",n,fib1);

        }

return buf_count;
}
Exemplo n.º 4
0
int fib_rec(const int& n, std::vector<int>& memo,  long long& count) {
    assert(n >= 0);

    // base cases
    if (n <= 1)
        return n;

    // set the elements using memoization
    if (memo[n - 1] == -1)
        memo[n - 1] = fib_rec(n - 1, memo, ++count);

    if (memo[n - 2] == -1)
        memo[n - 2] = fib_rec(n - 2, memo, ++count);

    // return the two elements added together after they are computed
    return memo[n - 1] + memo[n - 2];
}
Exemplo n.º 5
0
int main(int argc, char* argv[]) {
    assert(argc == 2);
    long long count = 0;
    int target = atoi(argv[1]);

    std::vector<int> memo(target + 1, -1);

    for (int i = 0; i <= target; ++i)
        std::cout << "[" << fib_rec(i, memo, ++count) << "]";

    std::cout << std::endl << "Function calls: " << count << std::endl;
}
Exemplo n.º 6
0
static int
fib_rec(int n)
{
    return (n <= 1) ? 1 : fib_rec(n-1) + fib_rec(n-2);
}
Exemplo n.º 7
0
void print_fibs(int n)
{
	printf("fib(%d) rec=%d nerec=%d\n", n, fib_rec(n), fib_nerec(n));
}