Problem 1

66 comments on “Problem 1”


  • Ritesh

    // In Java

    import java.util.Scanner;
    public class Prime{
    public static void main(String[] args){

    Scanner sc = new Scanner(System.in);
    int a,b,c;
    int num =2;
    int temp=0;
    System.out.println(“Enter term”);

    int n = sc.nextInt();

    if(n%2!=0){

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


  • Debanjan

    import math
    r=int(input(“Enter range”))
    def prime(c):
    i=2
    k=0
    while i<99999:
    # Just checking prime or not
    c1=0
    for j in range (2,i+1):
    if i%j==0:
    c1+=1
    if c1==1:#means the number is prime
    k+=1
    if k == c:# checking for the nth prime number
    print(i)
    break
    i+=1

    def fibo(n):
    n1, n2 = 1, 1
    count = 1
    while count < n:
    nth = n1 + n2
    n1 = n2
    n2 = nth
    count += 1
    op = n1
    return op

    r1=math.ceil(r/2)
    if(r%2==1):
    print(fibo(r1))

    else:
    prime(r1)


  • Debanjan

    import math
    r=int(input(“Enter range”))
    count1=3
    n11, n21 = 1, 1
    count2=3
    n12, n22 = 2, 3
    for i in range (1,r+1):
    if i%2==0:
    nterms = (i / 2)

    if nterms == 1 or nterms == 2:
    op=n11
    else:
    while count1 < nterms:
    nth1 = n11 + n21
    # update values
    n11 = n21
    n21 = nth1
    count1 += 1
    op=n11
    elif i%2==1:
    nterms =math.ceil(i / 2)
    if nterms == 1 :
    op=n12
    elif nterms==2:
    op=n22
    else:
    while count2 < nterms:
    nth2 = n12 + n22
    # update values
    n12 = n22
    n22 = nth2
    count2 += 1
    op=n12
    print(op)


  • Gourab

    #include
    int main(){
    int i,j,n,f=0;
    scanf(“%d”,&n);
    int a=0,b=1,m=a+b,p=2;
    for(i=1;i<=n;i++){
    if(i%2==0){
    //prime
    for(int j=p;j<1000;j++){
    for(int k=2;k<=j/2;k++){
    if(j%k==0){
    f=1;
    break;
    }
    }
    if(f==0){
    printf("%d ",j);
    p=j+1;
    f=0;
    break;
    }
    f=0;
    }
    }
    else if(i%2!=0){
    if(i==1){
    printf("%d ",i);
    continue;
    }

    //fib
    m = a+b;
    a=b;
    b=m;
    printf("%d ",m);

    }
    }
    return 0;
    }


  • harshad_1996

    n=int(input())
    j=1

    def prime(i):
    no=[]

    for k in range(2,9999):
    for m in range(2,k):
    if(k%m==0):
    break
    else:
    no.append(k)

    return no[int(i-1)]

    def fib(j):
    a=1
    b=1
    c=0

    if(j==1 or j==2):
    c=1
    else:
    for l in range(2,j):
    c=a+b
    a=b
    b=c

    return(c)

    for i in range(1,n+1):
    if(i%2==0):
    num=prime(int(i/2))
    #print(num)
    else:
    num= fib(j)
    j+=1
    #print(num)

    print(num)


  • Asharaf

    import java.util.Scanner;
    import java.util.List;
    import java.util.ArrayList;
    class TCS9
    {
    static int pri = 2;
    public static void main(String[] args)
    {
    Scanner scan = new Scanner(System.in);
    List fibo = new ArrayList();
    List prime = new ArrayList();
    int n = scan.nextInt();
    for(int i = 1;i<=n;i++)
    {
    if(i%2==1)
    {
    if(fibo.size()<2)
    fibo.add(1);
    else
    {
    int a = (Integer)fibo.get(fibo.size()-1);
    int b = (Integer)fibo.get(fibo.size()-2);
    fibo.add(a+b);
    }
    }
    else
    {
    boolean flag = false;
    while(flag!=true){
    if(Prime(pri)){
    prime.add(pri);
    flag = true;
    }
    pri++;
    }
    }
    }
    List l3 = new ArrayList();
    int cnt1=0,cnt2=0;
    for(int i = 1;i<=n;i++)
    {
    if(i%2==1)
    l3.add(fibo.get(cnt1++));
    else
    l3.add(prime.get(cnt2++));
    }
    System.out.println(l3.get(n-1));

    }
    static boolean Prime(int n)
    {
    for(int i = 2;i*i<=n;i++)
    if(n%i==0)
    return false;
    return true;
    }
    }


  • Vineet

    Java solution ===>

    import java.util.Scanner;

    public class pblm1 {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    int[] P_series = Pr(n);
    int[] F_series = fibo(n);

    for (int i = 0; i < n / 2; i++) {
    System.out.print(F_series[i] + " " + P_series[i] + " ");
    }

    sc.close();

    }

    public static int[] Pr(int m) {
    int[] prime = new int[m / 2];
    int pos = 0;
    for (int i = 2; i <= m + 3; i++) {
    int count = 0;
    for (int j = 2; j < i; j++) {
    if (i % j == 0) {
    count++;
    }
    }
    if (count == 0) {
    prime[pos++] = i;
    }
    }

    return prime;
    }

    public static int[] fibo(int x) {
    int[] fb = new int[x / 2];
    int first = 0, second = 1, next = 0;
    for (int i = 0; i < fb.length; i++) {
    if (i == 0) {
    fb[i] = 1;
    } else {
    next = first + second;
    first = second;
    second = next;

    fb[i] = second;
    }

    }
    return fb;
    }
    }


  • Sachin

    #include

    int main ()

    {

    int n,max=80,j=0;
    scanf(“%d”,&n);
    if(n%2==0){
    n=n/2;
    for(int i=2;i<=max;i++){
    if(i==2 || i==3)
    j++;
    else if (i%2==0 || i%3==0)
    continue;
    else
    j++;

    if(j==n){
    printf("%d",i);
    return 0;
    }
    }
    }
    else{
    n=(n/2)+1;
    int a=1,b=1,c;
    if(n==1 || n==2){
    printf("1");
    return 0;
    }
    for(int i=3;i<=n;i++){
    c=a+b;
    a=b;
    b=c;
    }
    printf("%d",c);

    }
    return 0;

    }


  • swapnil

    def nextPrime(p):
    while True:
    p=p+1
    if(checkPrime(p)):
    return p
    return p

    def checkPrime(p):
    for i in range(2,p):
    if p%i==0:
    return False
    return True
    a=1
    b=1
    z=0
    p=0
    res=0
    n=int(input())
    for i in range(n+1):
    if i%2==0:
    p=nextPrime(p)
    else:
    if i==1:
    res=1
    elif i==3:
    res=1
    else:
    z=a+b
    res=z
    a=b
    b=z

    if n%2==0:
    print(p)
    else:
    print(res)


  • swapnil

    package tara;
    import java.util.*;
    public class tcs1 {
    static int nextPrime(int p) {
    boolean is_prime=false;
    while(true) {
    p=p+1;
    is_prime=checkPrime(p);
    if(is_prime==true) {
    break;
    }
    }
    return p;
    }

    static boolean checkPrime(int p) {
    if(p==2) {
    return true;
    }
    for(int i=2;i<p;i++) {
    if(p%i==0) {
    return false;
    }

    }
    return true;
    }
    public static void main(String[] args) {

    Scanner sc=new Scanner(System.in);
    int N=sc.nextInt();
    int odd=0;
    int p=1;
    int a=1;
    int b=1;
    int z=0;
    for(int i=1;i<N+1;i++) {
    if(i%2==0) {
    p=nextPrime(p);

    }else {
    if(i==1) {
    odd=1;
    }else {
    if(i==3) {
    odd=1;
    }else {
    z=a+b;
    odd=z;
    a=b;
    b=z;

    }
    }
    }
    }
    if(N%2!=0) {
    System.out.println(odd);
    }else {
    System.out.println(p);
    }
    }

    }


  • Sampada

    #include
    #include
    #include

    using namespace std;

    void prime(int a)
    {
    int i,count;
    count=0;
    for(i=0;i<=a;i++)
    { if(a%i==0)
    count=count+1;
    }
    if(count==2)
    cout<<a;
    }
    void fab(int a)
    {
    int f=0,b=1,next;
    for(int i=2;i<=a;i++)
    {
    next=f+b;
    f=b;
    b=next;
    }
    cout<<a;
    }

    int main()
    {
    int n;
    cout<>n;

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

    }
    return 0;

    }


  • subhojit

    #code in Python 3
    def prime(n):
    a=2
    ct=1
    flag=0
    if n==1:
    return a
    else:
    while ct!=n:
    a+=1
    for i in range(2,a,1):
    if a%i==0:
    flag=2
    break
    flag=1
    if flag==1:
    ct+=1
    return a

    def fibonacci(n):
    a=1
    b=1
    if n==1:
    return a
    elif n==2:
    return b
    else:
    for i in range(3,n+1,1):
    a,b=b,a+b
    return b
    if __name__==”__main__”:
    num=int(input())
    if num%2==0:
    print(prime(num/2))
    else:
    print(fibonacci((num//2)+1))