Problem: Double And Add One - Data Structure using C

Translate

Friday, December 11, 2020

Problem: Double And Add One

Problem: Double And Add One

If you like numbers, you may have been fascinated by prime numbers.

Here's a problem related to prime numbers: Accept input numbers N and i. Identify all prime numbers P up to N with the

following property:

P1=2*P+1 is also prime

P2=2*P1+1 is also prime

...

Pi=2*P(i-1)+1 is also prime

Sample Input 

100

3

Sample Input Sample Output

2 5 89

code👇👇👇👇👇👇👇👇👇👇👇

#include<stdio.h>

int isPrime(int x){

    if(x<=1) return 0;

    for(int i=2;i*i<=x;i++)

    if(x%i==0) return 0;

    return 1;

}

int main(){

    int N,i,a,b;

    scanf("%d",&N);

    scanf("%d",&i);

    for(int j=2;j<=N;j++){

        

        if(isPrime(j)==1){

            a=j;

            b=0;

            for(int k=0;k<i;k++){

                a=2*a+1;

                if(isPrime(a)==0) b=1;

            }

            if(b==0) printf("%d ",j);

        }

    }

}



No comments:

Post a Comment

Introduction to Arrays

  Introduction to Arrays An array is a data structure that allows you to store a collection of elements of the same type. Each element in th...