Thursday, May 11, 2017

URI Fast Prime Number Solution in C, C++ | URI 1221 Solution

URI Fast Prime Number Solution in C, C++ | URI 1221 Solution


URI Online Judge Solution Fast Prime Number  | Mathematics
URI Main Problem Link - https://www.urionlinejudge.com.br/judge/en/problems/view/1221

Problem Name: URI Problem Fast Prime Number
Problem Number : URI Problem Fast Prime Number 1221 Solution
Online Judge : URI Online Judge Solution
Level: Mathematics
Solution Language : C, C plus plus

URI Fast Prime Number Solution in C, C++ | URI 1221 Solution

URI Solution 1221 Solution/Code in C:

#include <stdio.h>
#include <math.h>

int main(){
    int n, teste, i, j;
    long p;
    
    scanf("%d", &n);
    
    for(i = 0; i <n; i++){
            teste = 0;
            scanf("%ld", &p);
            
            if(p == 0){
                 printf("%s\n", "Not Prime");
                 continue;
            }
            
            if(p == 1){
                 printf("%s\n", "Not Prime");
                 continue;
            }
            
            if(p == 2){
                 printf("%s\n", "Prime");
                 continue;
            }
            
            for(j = 2; j < sqrt(p)+1; j++){
                    if(p%j == 0) teste++;
                    if(teste == 2) break;
            }
            
            if(teste >= 1) printf("%s\n", "Not Prime");
            else printf("%s\n", "Prime");
    }
    return 0;
}


URI Solution 1221 Solution/Code in C++:

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
 int n, x;
 bool ver = false;

 cin >> n;

 for (int i = 0; i < n; ++i)
 {
  cin >> x;

  if(x == 2){
   cout << "Prime" << endl;
  }else{

   if(x % 2 == 0){
    cout << "Not Prime" << endl;
   }else{

    for (int j = 3; j < sqrt(x); j += 2)
    {
     if(x % j == 0)
      ver = true;
    }

    if(ver == true){
     cout << "Not Prime" << endl;  
    }else{
     cout << "Prime" << endl; 
    }
   }
  }

  ver = false;
 }

 return 0;
}


Tags: Uri solve , Uri solution, URI oj Solve, URI Online Judge Solution list, URI Fast Prime Number Solution in C, C++ | URI 1221 Solution, URI 1221 Solution in c, URI 1221 code in C

No comments:
Write comments

To know more about the problem, give us your valuable commment. We'll try to help you. Thanks

All rights reserved ©2016 -URI ONLINE JUDGE SOLUTION | Developed by Maniruzzaman Akash

© 2016 URI ONLINE JUDGE SOLUTION. Developed by Maniruzzaman Akash | Distributed By Gooyaabi Templates
Powered by Blogger.