Problem 1

66 comments on “Problem 1”


  • 5443 Shivakumar

    def fib(n):
    a,b=0,1
    s=0
    if n<=0:
    print("enter a postive number")
    elif n==1:
    print(a)
    else:
    while(s<=n):
    c=a+b
    a=b
    b=c
    s+=1
    print(a)
    def prime(n):
    count=0
    for i in range(2,9999):
    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
    n=int(input("enter n="))
    if n%2 == 1:
    fib((n//2)+1)
    else:
    prime(n//2)


  • Debasmita

    import java.util.*;
    public class NthPrimeFib {
    public static final int MAX = 1000005;
    public static int getPrime(int n) {
    int c = 0;
    int[] flag = new int[MAX];
    for(int i=2; i*i<MAX; i++) {
    if(flag[i] == 0) {
    for(int j = i*i; j<MAX; j+=i) {
    flag[j] = 1;
    }
    }
    }
    for(int i=2; i<MAX; i++) {
    if(flag[i] == 0) {
    c++;
    if(c == n) {
    return i;
    }
    }
    }
    return 0;
    }
    public static int getFib(int n) {
    if(n < 3) {
    return 1;
    }
    return getFib(n-1)+getFib(n-2);
    }
    public static int getN(int n) {
    if(n % 2 == 0) {
    return getPrime(n/2);
    }
    return getFib(n/2);
    }
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    in.close();
    System.out.println(getN(n));
    }
    }


  • anushkav220

    BY JAVA:
    import java.util.Scanner;
    public class practicee {
    public static void main(String args[]) {

    Scanner s = new Scanner(System.in);
    int n=s.nextInt();
    if(n%2==0) {
    int x=0;
    int ans=0;
    for(int i=2;x<(n/2)-1;i++) {
    for(int j=2;j<i;j++) {
    if(i%j==0) {
    break;
    }
    else if(i-1==j){
    x=x+1;
    ans=i;
    }
    }

    }
    System.out.print(ans);
    }
    else {
    int a=0;
    int b=1;
    int c;

    for(int i=1; i<(n+1)/2; i++) {
    c=a+b;
    a=b;
    b=c;

    }
    System.out.println(b);
    }

    }

    }


  • swagatapal1980

    import java.util.Scanner;
    public class Main {

    static void fibonacci(int n)
    {
    int a=0, b=1,next;
    for(int i=1; i<=n; i++)
    {
    next = a+b;
    a=b;
    b=next;
    }
    System.out.println(a);
    }

    static void prime(int n)
    {
    int i,j,flag, count =0;
    long max = 99999;
    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.println(i);
    break;
    }
    }
    }
    }
    public static void main(String[] args)
    {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();

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

    }
    }
    }


  • 156

    package workspace;

    import java.util.*;

    public class Tcs_Q1 {

    static int limit=1000;
    public static void main(String[] args) {

    Scanner scn=new Scanner(System.in);
    int n=scn.nextInt();
    int ans=0;
    if(n%2==0) {
    ans=calculate_prime(n/2);
    }else {
    ans=calculate_fib(n/2+1);
    }

    System.out.println(ans);

    }

    private static int calculate_fib(int n) {
    int a=0;
    int b=1;
    int sum=0;
    for(int i=1;i<=n;i++) {
    sum=a+b;
    a=b;
    b=sum;
    }
    return a;
    }

    private static int calculate_prime(int n) {

    int prime=0;
    for(int i=2;i<limit;i++){

    if(isprime(i)) {
    prime=i;
    n–;
    }

    if(n==0){
    break;
    }

    }

    return prime;

    }

    private static boolean isprime(int n) {

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

    if(n%i==0) {
    return false;
    }

    }

    return true;
    }

    }


  • sagarbiswasbunny

    def fibonacci(n):
    if n==1:
    return 1
    if n==2:
    return 1
    else:
    return(fibonacci(n-1)+fibonacci(n-2))
    def isprime(n):
    flag=0
    for i in range(2,n//2+1):
    if(n%i==0):
    flag=1
    break
    if flag==0:
    return True
    else:
    return False

    def prime(n):
    count=0
    check=2
    while(count!=n):
    if isprime(check):
    count=count+1
    check=check+1
    return(check-1)

    n=int(input())
    if n%2==0:
    n=n/2
    print(prime(n))
    else:
    n=(n+1)/2
    print(fibonacci(n))


  • Debangshu

    n=int(input(“enter a number: “))
    if n%2!=0:
    third,first,second,number=0,0,1,0
    list=[first,second]
    while (number!=(n//2+1)):
    third=first+second
    list.append(third)
    first=second
    second=third
    number+=1

    print(list[n//2+1])
    else:
    prime_list=[]

    for num in range(1, n*n):
    # all prime numbers are greater than 1
    for i in range(2, num):
    if (num % i) == 0:
    break
    else:
    prime_list.append(num)

    print(prime_list[n//2])


  • c

    a=c=1
    b=2
    n=int(input())
    def gen_prime(n):
    flag=0
    for i in range(2,n//2+1):
    if n%i==0:
    flag=1
    break

    else:
    return n
    if flag==1:
    return gen_prime(n+1)

    for i in range(n-3):
    if i%2==0:
    b=gen_prime(b+1)
    res=b
    else:
    a,c=c,a+c
    res=c

    else:
    print(res)


  • Subhradeep

    #include

    int fibonacci(int n){
    int a = 0,b = 1,res;

    for(int i = 1;i<=n;i++){
    res = a+b;
    a = b;
    b = res;
    }
    return a;
    }

    int prime(int n){
    int i,count = 0, num = 1;
    while(count<n){
    num = num + 1;
    for(i=2;i<=num;i++){
    if(num%i == 0){
    break;
    }
    }
    if(num == i){
    count++;
    }
    }
    return num;
    }

    int main()
    {
    int n,res;
    scanf("%d",&n);

    if(n%2 == 1){
    res = fibonacci((n/2)+1);
    printf("%d",res);
    }else{
    res = prime(n/2);
    printf("%d",res);
    }
    }


  • Akash

    Java Code:
    import java.util.Scanner;

    public class Code10 {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();

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

    }
    public static void fib(int n) {
    int n1=0, n2=1, sum=0;
    for(int i=1; i0; i++){
    flag=0;
    for(int j=2; j<i; j++){
    if(i%j==0){
    flag=1;
    break;
    }
    }
    if(flag==0){
    count++;
    }
    if(count==n){
    System.out.println(i);
    break;
    }
    }
    }

    }


  • Hemant

    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)