Problem 1

66 comments on “Problem 1”


  • Hritik

    #include
    int prime(int);
    int fibo(int);
    int main(){
    int n;
    scanf(“%d”,&n);
    if(n%2==0)
    printf(“%d”,prime(n/2));
    else
    printf(“%d”,fibo((n/2)+1));
    return 0;
    }
    int prime(int num){
    int i,j,flag=0,count;
    for(i=2;i<=100;i++){
    count=0;
    for(j=2;j<=i;j++){
    if(i%j==0){
    count++;
    }
    }
    if(count==1){
    flag++;
    }
    if(flag==num){
    return i;
    }
    }
    }
    int fibo(int n){

    if(n==1){
    return 1;
    }else if(n==0){
    return 0;
    }
    return fibo(n-1)+fibo(n-2);
    }


  • naveen k sharma

    def prime(n):
    i=1
    j=1
    count=0
    flag=0
    for i in range(2,int(n+1)):
    flag = 0
    for j in range(2,i/2 +1):
    if(i%j==0):
    flag+=1
    if(flag==0):
    count+=1
    if(count==n):
    print(i)
    break

    def fib(m):
    a = 0
    b = 1
    for i in range(1,int(m+1)):
    c = a+b
    a=b
    b = c
    if(i==m):
    print(a)

    if __name__==’__main__’:
    n = int(input())
    e = 0
    if(n%2==0):
    e = float(n/2)
    prime(e)
    else:
    e = float((n/2) +1)
    fib(e)


  • Jai Singh

    def fib(n):
    frst=1
    scnd=1
    list1=[frst,scnd]
    for i in range(n-2):
    res=frst+scnd
    list1.append(res)

    frst,scnd=scnd,res
    return list1

    def prime(n):
    count=n-1
    list2=[2]
    for i in range(3,100):
    flag=0
    if count==0:break
    for j in range(2,i):
    if i%j==0:
    flag=1
    break

    if flag==0:
    count=count-1
    list2.append(i)
    return list2

    n=int(input())
    e=n//2
    if n%2==0:
    p=prime(e)
    f=fib(e)
    else :
    p=prime(e)
    f=fib(e+1)
    if(n%2==0):
    print(p[-1])
    else:
    print(f[-1])


  • Priyanka namdev

    #Python code 3.6
    def prime(num):
    count = 0

    for i in range(2, 100):
    flag = 0 # after loop finish flag =0 bcz next iteration will be check
    for j in range(2, i):
    if i%j==0:
    flag = 1
    if (flag == 0):
    count += 1
    if count == num:
    return i # return i value

    def fib(num):
    if num==1 or num==2:
    return 1
    else:
    return fib(num-1) + fib(num-2) #1,1,2,3 == fib(n-1)+fib(n-2) == 3+2=5

    num = int(input())
    if num%2==0:
    n = num//2 # 3/2==1.5 or 3//2==1
    print(prime(n))
    else:
    n =(num+1)//2 # 2//2==1 or 2+1//2==1
    print(fib(n))


  • Abhishek

    I found some mistake in the c code
    If(i%j==0)
    {flag=1;
    break; }
    If(flag==0)
    If(++count==n)
    {printf(%d, I);
    break; }


  • akash saraf

    Hey guys,
    This is the python code,hope it is helpful :
    def Prime(n):
    result=[]
    x=2
    while len(result) < n:
    flag=0
    for i in range(2,x):
    if x%i == 0:
    flag+=1
    break
    if flag == 0:
    result.append(x)
    x+=1
    return result
    def Fib(n):
    if n<=1:
    return n
    else:
    return(Fib(n-1)+Fib(n-2))
    n=int(input())
    c=n
    n=int(n/2)
    p=Prime(n)
    fib_list=[]
    for i in range(n+1):
    f=Fib(i+1)
    fib_list.append(f)
    actual=[]
    p1=0
    p2=0
    for i in range(1,c+1):
    if(i%2==0):
    actual.append(p[p2])
    p2+=1
    else:
    actual.append(fib_list[p1])
    p1+=1
    print(actual[c-1])


  • Ravisankar ka

    import java.util.Scanner;

    public class NewClass4 {
    static void fibonacci(int n)
    {
    int i, t1 = 0, t2 = 1, nextTerm;
    for (i = 1; i<=n; i++)
    {
    nextTerm = t1 + t2;
    t1 = t2;
    t2 = nextTerm;
    }
    System.out.print(t1);
    }
    static void prime(int n)
    {
    int MAX = 1000;
    int i, j, flag, count =0;
    for (i=2; i<=MAX; i++)
    {
    flag = 0;
    for (j=2; j<i; j++)
    {
    if(i%j == 0)
    {
    flag = 1;
    break;
    }
    }
    if (flag == 0)
    if(++count == n)
    {
    System.out.print(i);
    break;
    }
    }
    }
    public static void main(String[] args)
    {
    int n;
    Scanner sc = new Scanner(System.in);
    n = sc.nextInt();
    if(n%2 == 1)
    fibonacci (n/2 + 1);
    else
    prime(n/2);
    }
    }


  • SAURABH

    //the FLAG values were by mistakely given wrong values by me in one of the explanation. This is the correct one

    /* the “i” acts as a variable that will eventually contain the prime no. and will be printed. “j” loop is used to check if “i”(which is our prime no. ) has any factors if “i” has a factor, the control breaks out of “j” loop and flag is set to 1. And if “i” has no factor then the count is increased by 1 and checked if (count == n) and if turns out to be true then “i” value is printed. For e.g. if argument passed to function prime is 1 i.e. void prime(1) then for(i=2;i<MAX;i++) is executed and flag is set to 0, then for(j=2;j<i;j+) is executed which results in "FALSE" and hence we do not enter the loop and thus flag remains 0 and then the condition(++count==n) gets "TRUE"( as ++count changes count to 1 and the value of n is also 1). Therefore, we print "i" which is 2 (and the 1st prime is 2)… */
    #include
    #define MAX 1000
    void fibonacci(int n)
    {
    int i, t1 = 0, t2 = 1, nextTerm;
    for (i = 1; i<=n; i++)
    {
    nextTerm = t1 + t2;
    t1 = t2;
    t2 = nextTerm;
    }
    printf("%d", t1);
    }
    void prime(int n)
    {
    int i, j, flag, count =0;
    for (i=2; i<=MAX; i++)
    {
    flag = 0;
    for (j=2; j<i; j++)
    {
    if(i%j == 0)
    {
    flag = 1;
    break;
    }
    }
    if (flag == 0)
    {
    if(++count == n)
    {
    printf("%d", i);
    printf("%d",count);
    break;
    }
    }
    }
    }
    int main()
    {
    int n;
    scanf("%d", &n);
    if(n%2 == 1)
    {
    fibonacci (n/2 + 1);
    }
    else
    {
    prime(n/2);
    }
    return 0;
    }


  • SAURABH

    /* the “i” acts as a variable that will eventually contain the prime no. and will be printed. “j” loop is used to check if “i”(which is our prime no. ) has any factors if “i” has a factor, the control breaks out of “j” loop and flag is set to 1. And if “i” has no factor then the count is increased by 1 and checked if (count == n) and if turns out to be true then “i” value is printed. For e.g. if argument passed to function prime is 1 i.e. void prime(1) then for(i=2;i<MAX;i++) is executed and flag is set to 1, then for(j=2;j<i;j+) is executed which results in "FALSE" and hence we do not enter the loop and thus flag remains 1 and then the condition(++count==n) gets "TRUE"( as ++count changes count to 1 and the value of n is also 1). Therefore, we print "i" which is 2 (and the 1st prime is 2)… 😛

    #include
    #define MAX 1000
    void fibonacci(int n)
    {
    int i, t1 = 0, t2 = 1, nextTerm;
    for (i = 1; i<=n; i++)
    {
    nextTerm = t1 + t2;
    t1 = t2;
    t2 = nextTerm;
    }
    printf("%d", t1);
    }

    void prime(int n)
    {
    int i, j, flag, count =0;
    for (i=2; i<=MAX; i++)
    {
    flag = 0;
    for (j=2; j<i; j++)
    {
    if(i%j == 0)
    {
    flag = 1;
    break;
    }
    }

    if (flag == 0)
    {
    if(++count == n)
    {
    printf("%d", i);
    printf("%d",count);
    break;
    }
    }
    }
    }

    int main()
    {
    int n;
    scanf("%d", &n);
    if(n%2 == 1)
    {
    fibonacci (n/2 + 1);
    }
    else
    {
    prime(n/2);
    }
    return 0;
    }


  • rashmi mazumder

    the java code of the above question is—

    /*package whatever //do not write package name here */

    import java.io.*;
    import java.util.Scanner;

    class Main {
    public static void main (String[] args) {
    int n,e;
    Scanner sc=new Scanner(System.in);
    n=sc.nextInt();
    e=n/2;
    Main obj=new Main();
    if(n%2==0)
    obj.prime(e);
    else
    obj.fibo(e+1);
    }
    public void prime(int e){
    int i,j,no,flag=0,count=0;
    for(i=1;i<=100;i++){
    flag=0;
    for(j=2;j<=i/2;j++){
    if(i%j==0)
    flag=0;
    else
    flag=1;
    }
    if(flag==1)
    count++;
    if(count==e){
    System.out.println(i);
    break;
    }
    }
    }
    public void fibo(int e){
    int n0=0,n1=1,n2=0;
    for(int i=3;i<=e;i++)
    {
    n2=n0+n1;
    n0=n1;
    n1=n2;
    }
    System.out.println(n2);
    }
    }


  • Somshubhra

    I have a better solution…

    gjg

    #include
    #define MAX 1000
    void fibonacci(int n)
    {
    int i, t1 = 0, t2 = 1, nextTerm;
    for (i = 1; i<=n; i++)
    {
    nextTerm = t1 + t2;
    t1 = t2;
    t2 = nextTerm;
    }
    printf("%d", t1);
    }
    void prime(int n)
    {
    int i, j, flag, count =0;
    for (i=2; i<=MAX; i++)
    {
    flag = 0;
    for (j=2; j<i; j++)
    {
    if(i%j == 0)
    {
    flag = 1;
    break;
    }
    }
    if (flag == 0)
    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;
    }


  • Tanaya

    this code seems to be giving the wrong output …. i have a better solution…
    #include

    int main ()
    {
    int n, flag;

    int i, t = 1, j;

    int a, b, c, r, g;

    a = 0;
    b = 1;
    c = 0;
    r = 1;

    scanf (“%d”, &n);

    if (n % 2 != 0)

    {

    while (r <= (n / 2 + 1))

    {

    g = b;
    c = a;
    a = b;
    b = a + c;

    r++;

    }

    printf ("%d ", g);

    }

    else

    { //for prime no

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

    {

    flag = 0;

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

    {

    if (i % j == 0)

    {
    flag = 1;

    break;

    }

    }

    if (flag == 0)

    {
    t++;

    }

    }

    printf ("%d ", i – 1);

    }

    return 0;

    }