Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C++ by carado ( 16 years ago )
#include <stdlib.h>
#include <iostream>
#include <vector>
#include <math.h>

bool isPrimary(long a);

int main(int argc, char *argv[])
{
 if (argc != 2)
 {
  std::cerr<<"Usage : "<<argv[0]<<" <number> \n";
  return (EXIT_FAILURE);
 }
 std::cout<<strtol(argv[1], NULL, 10);
 if(isPrimary(strtol(argv[1], NULL, 10)))
  std::cout<<" is primary.\n";
 else
  std::cout<<" is not primary.\n";
 return (EXIT_SUCCESS);
}

bool isPrimary(long a)
{
 std::vector<long> primarys(1,2);
 for (long i = 3; pow(i, 2) <= a; i++)
 {
  for (long j = 0; pow(primarys[j], 2) < primarys[i] && j < primarys.size(); j++)
  {
   if (i % primarys[j] == 0)
    break;
  }
  if (a % i == 0)
   return (false);
  primarys.push_back(i);
 }
 if(a <= 3)
  return (true);
 if(a % 2 == 0)
  return (false);
 return (true);
}

 

Revise this Paste

Parent: 16706
Your Name: Code Language: