Problem 1

66 comments on “Problem 1”


  • Swetha

    import java.util.*;
    public class Sample {
    public static int even(int num) {
    //odd term
    int i=0;
    int start=2;
    while(i<num) {
    int count=0;
    int j=1;
    while(j<=start) {
    if(start%j==0) {
    count++;
    }
    j++;
    }
    start++;
    if(count==2) {
    i++;
    }
    }
    return (start-1);
    }
    public static int odd(int num) {
    //fibo
    int a=1;
    int b=1;
    if(num==1) {
    return 1;
    }
    if(num==2) {
    return 1;
    }
    int count=2;
    int c=0;
    while(count<num) {
    c=a+b;
    a=b;
    b=c;
    count++;
    }
    return c;
    }
    public static void main(String args[]) {
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    if(n%2!=0) {
    int num=Math.round((float)n/2);
    System.out.println(odd(num));
    }
    else {
    int num=n/2;
    System.out.println(even(num));
    }
    }
    }


  • Dadaso

    #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;
    }


  • Aditi

    package pkg;

    import java.util.Scanner;

    public class MixtureSeries1 {

    public static void main(String[] args) {
    // TODO Auto-generated method stub
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();

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

    public static void fibonacci(int n) {
    int t1=0, t2 = 1, temp = 1;
    for(int i = 1; i <= n; i++) {
    t1=t2;
    t2= temp;
    temp= t1+t2;
    }
    System.out.println(t1);
    }

    public static void prime(int n) {
    int t = 1;
    boolean flag = false;

    for(int i = 1; i <= n; i++) {
    for(int j = ++t; j < 100; j++) {
    flag = false;
    for(int k = 2; k <= j/2; k++) {

    if(j%k==0) {
    flag = true;
    break;

    }
    }

    if(flag==false) {
    t = j;
    break;
    }
    }
    }
    System.out.println(t);

    }
    }


  • Deepanshu

    from math import sqrt

    MAX=1000

    def nthprime(n) :
    count=0
    for i in range(2,MAX+1) :
    check=0
    for j in range(2,int(sqrt(i))+1) :
    if i%j==0 :
    check=1
    break

    if check==0 :
    count=count+1

    if count==n :
    return i
    break

    def nthFibonacci(n) :
    f = [0] * (n+2)
    f[0],f[1]=1,1

    for i in range(2,n+1) :
    f[i]=f[i-1]+f[i-2]
    return f[n]
    if __name__ == “__main__” :

    n=int(input())
    if n%2 ==0 :
    n=n//2
    n=nthprime(n)
    print(n)

    else :
    n=(n//2) +1
    n=nthFibonacci(n-1)
    print(n)


  • Pallipattu

    def fibbo(n):
    n1, n2 = 0, 1
    count = 0
    if n <= 0:
    print("Please enter a positive integer")
    elif n == 1:
    print(n1)
    else:
    while count < n:
    nth = n1 + n2
    n1 = n2
    n2 = nth
    count += 1
    print(n1)

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

    # main
    n = int(input())
    if n%2 == 1:
    fibbo(n//2+1)
    else:
    prime(n//2)


  • Ankita

    package PrepInsta;
    import java.util.Scanner;

    public class Series2 {

    public static void isPrime(int n)
    {
    int count,m=0;
    if(n==1)
    System.out.println(2);
    for(int i=2;i>1;i++)
    {
    count=0;
    for(int j=2;j<i; j++)
    {
    if(i%j==0)
    count++;
    }
    if(count==0)
    m++;
    if(m==n)
    {
    System.out.println(i);
    break;
    }
    }
    }

    public static void isFibo(int n)
    {
    int a=1;
    int b=1;
    int c=0;

    if(n==1 || n==2)
    System.out.println(1);
    else
    {
    for(int i=3;i<=n;i++)
    {
    c=a+b;
    a=b;
    b=c;
    }
    System.out.println(c);
    }
    }

    public static void main(String[] args) {
    Scanner sc =new Scanner(System.in);

    System.out.println("Enter term: ");
    int n=sc.nextInt();

    if(n%2==0)
    isPrime(n/2);
    if(n%2==1)
    isFibo((n/2)+1);

    }

    }


  • neeraj

    n=int(input(“enter the n:”))
    lst=[]
    lst1=[]
    for i in range(1,9999):
    if(i>1):
    for j in range(2,i):
    if(i%j==0):
    break;
    else:
    lst.append(i)
    first=1
    second=1
    temp=0
    for i in range(0,n):
    lst1.append(first)
    temp=first+second
    first=second
    second=temp
    for i in range(0,n):
    print(lst1[i],end=” “)
    print(lst[i],end=” “)


  • Satakchi

    public class Question1
    {
    void Prime(int n)
    {
    int count=0,prime_num=0;
    for(int i=2;i<=100;i++)
    {
    int c=0;
    for(int j=1;j<=i;j++)
    {
    if(i%j==0)
    {
    c++;
    }
    }
    if(c==2)
    count++;
    if(count==n)
    {
    prime_num=i;
    System.out.println(prime_num);
    break;
    }
    }
    }
    void Fibonacci(int n)
    {
    int a=0,b=1,c;
    for(int i=1;i<=n;i++)
    {
    c=a+b;
    a=b;
    b=c;
    }
    System.out.println(a);
    }
    public static void main(String args[])
    {
    Question1 ob=new Question1();
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    if(n%2==0)
    ob.Prime(n/2);
    else
    ob.Fibonacci(n/2+1);
    }
    }


  • prasad

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

    def prime(num):

    for num in range(2, 9999):
    if num > 1:
    for i in range(2, num):
    if num % i == 0:
    break
    else:
    print(num)

    num = int(input())
    if num%2 == 1:
    fib(num//2+1)
    else:
    prime(num//2)


  • Rohan

    #include
    #include

    void fibonacci(int n)
    {
    int a=0, b=1,next, i ;
    for(i= 1; i<=n; i++)
    {
    next = a+b;
    a=b;
    b= next;
    }
    printf("%d",a);
    }

    void prime(int n)
    {
    int count=0,i;
    for(i=1;i<= n;i++)
    {
    if(n%i==0)
    count++;
    }
    if(count==2)
    printf("It is a prime number");
    else
    printf("It is not a prime number");
    }

    int main()
    {
    int num;
    printf("Enter the term ")
    scanf("%d", &num);
    if(num%2 ==1)
    fibonacci(num/2 + 1);
    else
    prime(num/2);

    return 0;
    }


  • ankit

    ls = [1,1]

    for i in range(2,100):
    ls.append(ls[i-1]+ls[i-2])

    # print(ls)

    ls1 = []

    while len(ls1)1:
    for j in range(2,i):
    if i%j==0:
    break
    else:
    ls1.append(i)

    ls2 = []
    for i in range(0,100):
    ls2.append(ls1[i])

    # print(ls1)
    # print(ls2)

    ls3 = []
    count = 0
    count1 = 0
    for i in range(0,100):
    if i%2==0:
    ls3.append(ls[count])
    count += 1
    else:
    ls3.append(ls2[count1])
    count1 += 1

    print(ls3[int(input())-1])


  • shushank

    def fibbo(n):
    n1, n2 = 0, 1
    count = 0
    if n <= 0:
    print("Please enter a positive integer")
    elif n == 1:
    print(n1)
    else:
    while count < n:
    nth = n1 + n2
    n1 = n2
    n2 = nth
    count += 1
    print(n1)

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

    # main
    n = int(input())
    if n%2 == 1:
    fibbo(n//2+1)
    else:
    prime(n//2)