Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include <math.h>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. int isPrimitiveNumber (unsigned long long n);
  8. void findPrimitiveNumbers(int N);
  9. unsigned long long combineTwoNumber(unsigned long long a, unsigned long long b);
  10. long findPowerOf10(unsigned long long n);
  11.  
  12. int main() {
  13.     int N = 200;
  14.    
  15.     findPrimitiveNumbers(N);
  16.  
  17.     return 0;
  18. }
  19.  
  20. int isPrimitiveNumber (unsigned long long n) {
  21.     if(n == 2)
  22.         return 1;
  23.     //check if n is a multiple of 2
  24.     if (n%2 == 0)
  25.         return 0;
  26.     //if not, then just check the odds
  27.     for(int i=3;i*i<=n;i+=2) {
  28.         if(n%i==0)
  29.             return 0;
  30.     }
  31.     return 1;
  32. }
  33.  
  34. void findPrimitiveNumbers(int N) {
  35.     int indexOfResult = 0; 
  36.    
  37.     //Generate the primitive numbers
  38.     unsigned long long currentNumber = 3;
  39.     unsigned long long firstElement = 2;
  40.     unsigned long long secondElement = 0;
  41.    
  42.     while(indexOfResult != N) {
  43.         if(isPrimitiveNumber(currentNumber)) {
  44.             if(secondElement == 0) {
  45.                 secondElement = currentNumber;
  46.             }
  47.             else {
  48.                 unsigned long long numberOfCombine = combineTwoNumber(firstElement, secondElement);
  49.                 if(isPrimitiveNumber(numberOfCombine)) {
  50.                     //numberOfCombine is a result. you should write numberOfCombine to the result file from here
  51.                     //Or save it into a array and write to the file later, it depends on you!
  52.                     cout << numberOfCombine << "\n";
  53.                     indexOfResult++;
  54.                 }
  55.                 firstElement = currentNumber;
  56.                 secondElement = 0;
  57.             }  
  58.         }
  59.         currentNumber++;
  60.     }
  61. }
  62.  
  63. unsigned long long combineTwoNumber(unsigned long long a, unsigned long long b) {
  64.     unsigned long long pow = 10;
  65.     while(b >= pow)
  66.         pow *= 10;
  67.     return a * pow + b;  
  68.    
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement