SPOJ Number theory Problem PRIME1 - Prime Generator Solution





Solve in C++:
///La ilaha illellahu muhammadur rasulullah
///******Bismillahir-Rahmanir-Rahim******///
///Abul Hasnat  Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
///**********ALLAH IS ALMIGHTY************///
#include<iostream>
#include<math.h>
#include<stdio.h>
using namespace std;

bool checkprime (long int num)
{
    if (num <=1)
        return false;
    else if (num == 2)
        return true;
    else if (num % 2 == 0)
        return false;
    else
    {
        bool prime = true;
        int divisor = 3;

        int upperLimit =sqrt(num) +1;

        while (divisor <= upperLimit)
        {
            if (num % divisor == 0)
                return false;
            divisor +=2;
        }
        return prime;
    }
}

int main()
{
    int a,b,i,n,j;
    scanf("%d",&n);

    for(i=0; i<n; i++)
    {
        scanf(" %d %d",&a,&b);
        printf("\n");
        for(j=a; j<=b; j++)
        {
            if(checkprime(j))
                printf("%d\n",j);

        }
    }
    return 0;
}




 


No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h &g...

Powered by Blogger.