Advertisement
Guest User

EMIRP Finder

a guest
Jan 29th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.26 KB | None | 0 0
  1. /*********************************************************
  2.     Program: EMIRP (Program 1 Part A)
  3.     Author: --------------
  4.     Date: 01/29/2014
  5.     Time Spent:
  6.     Purpose: To find the first 100 EMIRP numbers:
  7.              that is, numbers that satisfy the criteria
  8.              that the number is prime, and when the number
  9.              is reversed, it is also prime; however,
  10.              palindromic primes should not be counted.
  11. **********************************************************/
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16.  
  17. typedef int bool;
  18. #define true 1
  19. #define false 0
  20.  
  21. bool isPalindrome(int n); // Determines if N is a palindromic number.
  22. bool isEMIRP(int n); // Determines if N is an EMIRP.
  23. bool isPrime(int n); // Determines if N is prime.
  24. char * getString(int n); // Gets a c-string representation of N.
  25. char * reverse(char * s); // Reverses the characters in a c-string.
  26.  
  27. int main(int argc, char ** argv) {
  28.     int e, n, i; // The number of EMIRP characters discovered, the current number, and an iterative variable.
  29.     int * emirps = (int*)malloc(100); // Stores the EMIRP numbers for future iteration.
  30.  
  31.     // Continue iterating until 100 EMIRP numbers are found.
  32.     e = 0;
  33.     n = 11;
  34.     while(e < 100) {
  35.         // If the number is an EMIRP, add it to the list and increment the number of EMIRPs found.
  36.         if(isEMIRP(n)) {
  37.             *emirps = n;
  38.             emirps++;
  39.             e++;
  40.         }
  41.         n++;
  42.     }
  43.  
  44.     printf("Done.\n\n");
  45.  
  46.     // Print the discovered EMIRP numbers.
  47.     for(i = 0; i < e; i++) {
  48.         printf("%s%d%s", ((((i + 1) % 10) == 0)?"\n":""), emirps[i], (((i % 10) < 9)?", ":""));
  49.     }
  50.  
  51.     return 0;
  52. }
  53.  
  54. bool isPalindrome(int n) {
  55.     char *pCN, *pCNR, cN[20];
  56.    
  57.     pCN = getString(n);
  58.     pCNR = reverse(pCN); // Reverse the characters in pCN and assign them to pCNR
  59.  
  60.     return (atoi(pCN) == atoi(pCNR)); // Return whether or not the two c-strings are equal.
  61. }
  62.  
  63. bool isEMIRP(int n) {
  64.     if(isPalindrome(n)) return false; // If N is a palindrome, return false.
  65.     else {
  66.         // If both N and its reverse are prime, return true.
  67.         if(isPrime(n) && isPrime(atoi(reverse(getString(n))))) return true;
  68.     }
  69.     return false;
  70. }
  71.  
  72. bool isPrime(int n) {
  73.     int i;
  74.     int primeFactors[4] = {2, 3, 5, 7}; // The prime factors used to determine if N is prime.
  75.  
  76.     // Iterate through the prime factors, checking to see if N is evenly divisible by any of them, or N itself is a prime factor.
  77.     for(i = 0; i < 4; i++) {
  78.         if(n == primeFactors[i]) return true;
  79.         else if((n % primeFactors[i]) == 0) return false;
  80.     }
  81.  
  82.     return true; // If N is not a prime factor, and is not evenly divisible by any of them, N is prime.
  83. }
  84.  
  85. char * getString(int n) {
  86.     char *r, *c, s[20];
  87.     int digits, i;
  88.  
  89.     sprintf(s, "%d", n); // Create a textual representation of N.
  90.  
  91.     // Count the number of digits in N.
  92.     digits = 0;
  93.     while(n > 0) {
  94.         n /= 10;
  95.         digits++;
  96.     }
  97.  
  98.     r = (char*)malloc(digits + 1);
  99.     c = r;
  100.     // Copy the contents of cN to pCN
  101.     for(i = 0; i < digits; i++) {
  102.         *c = s[i];
  103.         c++;
  104.     }
  105.     *c = '\0';
  106.  
  107.     return r;
  108. }
  109.  
  110. char * reverse(char * s) {
  111.     int i, j;
  112.     int len = strlen(s);
  113.     char * r = (char*)malloc(len + 1);
  114.  
  115.     /* Iterate backwards through S and forwards through R, adding
  116.     characters to R from S to get the reverse c-string. */
  117.     for(i = (len - 1), j = 0; i >= 0, j < len; i--, j++) {
  118.         r[j] = s[i];
  119.     }
  120.     r[len] = '\0';
  121.  
  122.     return r;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement