Problem 1

66 comments on “Problem 1”


  • Prakhar

    #python easy and simple
    #1,2,1,3,2,5,3,7,5,11,8,13,13,17
    #alternate fibo and prime
    n=int(input())
    def fib(n):
    if(n==1 or n==2):
    return 1
    else:
    return fib(n-1)+fib(n-2)
    def prime(n):
    m=99999
    l=[]
    for i in range(2,m):
    c=0
    for j in range(2,i):
    if(i%j==0):
    c=1
    break
    if(c==0):
    l.append(i)
    if(len(l)==n):
    break
    return l[-1]
    if(n%2==0):
    print(prime(n//2))
    else:
    print(fib(n//2+1))


  • SK.ISHAN

    #include
    using namespace std;
    #define int long long
    int fib(int n){
    int i,c,a=1,b=1;
    if(n==1 || n==2)return 1;
    else if(n>2){
    for(i=3;i<=n;i++){
    c=a+b;
    a=b;
    b=c;
    }
    return b;
    }
    }
    int prime(int n){
    int i,j,c=0,f;
    for(i=2;i<=INT_MAX;i++){
    f=0;
    for(j=2;j>n;
    if(n%2==1)cout<<fib(n/2+1);
    else cout<<prime(n/2);
    }


  • SK.ISHAN

    For C++:-
    #include
    using namespace std;
    #define int long long
    int fib(int n){
    int i,c,a=1,b=1;
    if(n==1 || n==2)return 1;
    else if(n>2){
    for(i=3;i<=n;i++){
    c=a+b;
    a=b;
    b=c;
    }
    return b;
    }
    }
    int prime(int n){
    int i,j,c=0,f;
    for(i=2;i<=INT_MAX;i++){
    f=0;
    for(j=2;j>n;
    if(n%2==1)cout<<fib(n/2+1);
    else cout<<prime(n/2);
    }


  • Raja

    #include
    using namespace std;
    void fibonacci(int );
    void prime(int );
    int main()
    {
    int n;
    cin>>n;
    if(n%2!=0){
    fibonacci(n/2+1);
    }
    else prime(n);
    return 0;
    }
    void fibonacci(int c){
    int a=0;
    int b=1;
    int next;
    for(int i=0;i<c;i++){
    next=a+b;
    a=b;
    b=next;
    }
    cout<<a;
    }
    void prime(int d){
    int flag=0;
    int e=d/2;
    int count=0;
    int i;
    for(i=2;i<9999;i++){
    flag=0;
    for(int j=2;j<i;j++){
    if(i%j==0){
    flag=1;
    break;
    }
    }
    if(flag==0){
    if(++count==e){
    cout<<i;
    break;
    }
    }
    }

    }


  • Dipayan

    #include
    #define MAX 99999

    void fibonacci(int n)
    {
    /* Variable initialization */
    int a = 0, b = 1, next;
    //the below code is for fibonacci series till nth position
    for (int i = 1; i<=n; i++)
    {
    next = a + b;
    a = b;
    b = next;
    }

    //will print a not b or next as they are stored to calculate next and next to next term
    printf("%d", a);
    }

    void prime(int n)
    {
    int i, j, flag, count =0;
    //as prime numbers in given question start from 2
    for (i=2; i<=MAX; i++)
    {
    flag = 0;
    //to check if divisible apart from 1 & itself
    //loop starts from 2 to ignore divisibilty by 1 & ends before the number itself
    for (j=2; j<i; j++)
    {
    if(i%j == 0)
    {
    //number is not prime
    flag = 1;
    break;
    }
    }
    //is prime
    if (flag == 0){
    //if found the nth prime number
    if(++count == n)
    {
    printf("%d", i);
    break;
    }
    }
    }
    }
    int main(){
    int n;
    scanf("%d", &n);

    if(n%2 == 1)
    fibonacci (n/2 + 1);
    else
    prime(n/2);

    return 0;
    }


  • Daksha

    #code in python 3
    n=int (input ())
    if n%2==0:
    x=n//2
    i=1
    c=0
    while c <x:
    k=0
    for j in range (1,i+1):
    if i %j ==0:
    k+=1
    if k ==2:
    c+=1
    i+=1
    print (i-1)
    else:
    c=0
    k=1
    ×=1
    for i in range (1,n//2+1):
    x=c+k
    c=k
    k=x
    print (x)


  • simran

    this question is only valid for 14 term otherwise it produces wrong output
    like if we want 13 term the expected output is 13 but by using this code output is 21

    write code is:

    #include

    void fibo(int);

    void prime(int);

    main()

    {

    int n,e;

    scanf(“%d”,&n);

    e=n/2;

    if(n%2==0)
    prime(e);
    else
    fibo(e+1);
    }

    void prime(int n){
    int i,j,no,flag=0,count=0;
    for(i=2;i<=100;i++){
    flag=0;

    for(j=2;j<i;j++){
    if(i%j==0)
    {flag++;
    break;} }

    if(flag==0)
    {count++;}

    if(count==n)

    {

    printf("%d",i);

    break;

    }

    }

    }

    void fibo(int n)

    {

    int n0=0,n1=1,n2,i;

    for(i=2;i<=n;i++)

    {

    n2=n0+n1;

    n0=n1;

    n1=n2;

    }

    printf("%d",n2);

    }


  • Rayed Ali

    #include

    void fibo(int);

    void prime(int);

    int main()

    {

    int n,e;

    scanf(“%d”,&n);

    e=n/2;

    if(n%2==0)

    prime(e);

    else

    fibo(e+1);

    }

    void prime(int n)

    {

    int i,j,no,flag=0,count=0;

    for(i=2;i<=100;i++)

    {

    flag=1;

    for(j=2;j<=i/2;j++)

    {

    if(i%j==0)

    flag=0;

    }

    if(flag==1)

    count++;

    if (count==n)

    {

    printf("%d\n",i);

    break;

    }

    }

    }

    void fibo(int n)

    {

    int n0=0,n1=1,n2,i;
    if (n==1)
    printf ("1");
    else
    {
    for(i=1;i<=n-1;i++)
    {

    n2=n0+n1;

    n0=n1;

    n1=n2;

    }
    printf("%d",n2);
    }
    }


  • avi gehlot

    n=int(input())
    #calculated Fibonacci series
    a=0
    b=1
    for i in range(1,n+1): #number is input 14 (1,14) loop
    if(i%2!=0): #14%2==0 means Fibonacci series Not as output
    if (i==1):
    c=1
    else:
    c=a+b
    a=b #calculated Fibonacci series
    b=c
    else: # we are go to else part
    for k in range(1,n+1): # range for k 1,14
    if k>1: # means 1 is not a prime
    for j in range(2,k): # range 2,14
    if(k%j==0): # this is calculated prime number
    k=k+1
    else:
    x=k
    if(i%2!=0):
    print(‘{}’.format(c))
    else:
    print(‘{}’.format(x))


  • shruty suman

    Correction to the given code:
    void fibo(int n)

    {

    int n0=0,n1=1,n2,i;

    if(n==1)
    printf(“1”);
    else{
    for(i=2;i<=n;i++)

    {

    n2=n0+n1;

    n0=n1;

    n1=n2;

    }

    printf("%d",n2);
    }
    }


  • Mohd Saif

    n=int(input())
    a=0
    b=1
    for i in range(1,n+1):
    if(i%2!=0):
    if(i==1):
    c=1
    else:
    c=a+b
    a=b
    b=c
    else:
    for k in range(1,n+1):
    if k>1:
    for j in range(2,k):
    if(k%j==0):
    k=k+1
    else:
    x=k

    if(i%2!=0):
    print(f”The {n} term is:{c}”)
    else:
    print(f”The {n} term is:{x}”)