Recursion 3

Let a 0 = 0 a_{0}=0 , a 1 = 1 a_{1}=1 and a n = n a n 1 a_{n}= n^{a_{n-1}} where n 2 n\ge2 be a recursive function. Write a program that calculates the image value of any non-negative integer n.

Can you find a 4 a_{4} mod 2015 2015 mod 20 20 mod 15 + 2000 15+2000 ?


this problem is a part of the set Fundamental Programming
2013 2015 2016 2014

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

3 solutions

Arulx Z
Dec 24, 2015

I used a pretty straightforward algorithm (again) -

1
2
3
4
5
6
7
8
>>> def a(n):
        if n == 0:
            return 0
        if n == 1:
            return 1
        return n ** a(n - 1)
>>> a(4) % 2015 % 20 % 15 + 2000
2014

Zeeshan Ali
Dec 21, 2015

Here is an algorithm that may help you understand the recursive functions;

pow(n, p) starts
    if p is 1
        return n;
    else
        return n*pow(n, p-1);
pow(n, p) ends

func(n) starts
    if n is 0 or n is 1
        return n;
    else
        return pow(n, func(n-1));
func(n) ends
  • The first recursive function calculates the p t h p^{th} power a number n.

  • The second one is the actual required function that recursively determines the n t h n^{th} term of the sequence given.

Md Zuhair
Feb 22, 2019
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
#include<math.h>
using namespace std;
int recursion(int);
int recursion(int n)
{
    if(n==0)
    {
        return 0;
    }
    else if(n==1)
    {
        return 1;
    }
    else if(n>=2)
    {
        int z=pow(n,recursion(n-1));
        return z;
    }
}
int main()
{
    int n;
    cout<<"Enter n";
    cin>>n;
    cout<<"\n nth term"<<recursion(n);
    int k;
    k=((recursion(n)%2015)%20)%15+2000;
    cout<<endl<<k;
    return 0;
}

According to this code, k is the answer.

OUTPUT:-

Enter n4

nth term262144 2014


Process exited after 2.355 seconds with return value 0 Press any key to continue . . .

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...