Problem 1

66 comments on “Problem 1”


  • Jaswanth Maddila

    def prime(num):
    if num==1:
    return False
    if num==2 or num==3:
    return True
    if num%2==0 or num%3==0:
    return False
    i=5
    while(i*i<=num):
    if(num%i==0 or num%(i+2)==0):
    return False
    i+=6
    return True

    def func(num):
    i=1
    while(num!=0):
    if prime(i):
    num=num-1
    i=i+1
    return i-1

    def function(num):
    if num%2==0:
    return func(num//2)
    else:
    return func(num//2+1)

    num = int(input())
    print(function(num))


  • Sayani

    #Java code

    import java.util.Scanner;

    public class TCS_NQT1 {

    static void fibo(int n)
    {
    int a = 0;
    int b = 1;
    int store;

    for(int i = 1; i<=n; i++ )
    {
    store = a+b;
    a=b;
    b=store;
    }
    System.out.println(a+ " ");
    }

    static void prime(int n)
    {
    int max = 500;
    int i ;
    int j;
    int flag;
    int 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)
    fibo(n/2+1);
    else
    prime(n/2);

    }

    }


  • Kanchi

    def fibbo(n):
    a,b=0,1
    if n==0:
    print(“0”)
    else:
    temp=0
    for i in range(n-1):
    temp=a+b
    a,b=b,temp
    print(temp)
    def odd(n):
    x=2
    while n>1:
    prime=0
    for i in range(1,x+2):
    if (x+1)%i==0:
    prime+=1
    if prime==2:
    n=n-1
    x=x+1
    print(x)

    ip=int(input())
    if ip%2==0:
    fibbo(ip)
    else:
    odd(ip)


  • NAVEENKUMAR

    #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 is odd
    nth number in main series will be found at (n/2 + 1) position
    in fibonacci sub series
    else
    if n is even then it will be found in (n/2) position in prime sub series */

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

    return 0;
    }