Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*********************************************************
- Program: EMIRP (Program 1 Part A)
- Author: --------------
- Date: 01/29/2014
- Time Spent:
- Purpose: To find the first 100 EMIRP numbers:
- that is, numbers that satisfy the criteria
- that the number is prime, and when the number
- is reversed, it is also prime; however,
- palindromic primes should not be counted.
- **********************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- typedef int bool;
- #define true 1
- #define false 0
- bool isPalindrome(int n); // Determines if N is a palindromic number.
- bool isEMIRP(int n); // Determines if N is an EMIRP.
- bool isPrime(int n); // Determines if N is prime.
- char * getString(int n); // Gets a c-string representation of N.
- char * reverse(char * s); // Reverses the characters in a c-string.
- int main(int argc, char ** argv) {
- int e, n, i; // The number of EMIRP characters discovered, the current number, and an iterative variable.
- int * emirps = (int*)malloc(100); // Stores the EMIRP numbers for future iteration.
- // Continue iterating until 100 EMIRP numbers are found.
- e = 0;
- n = 11;
- while(e < 100) {
- // If the number is an EMIRP, add it to the list and increment the number of EMIRPs found.
- if(isEMIRP(n)) {
- *emirps = n;
- emirps++;
- e++;
- }
- n++;
- }
- printf("Done.\n\n");
- // Print the discovered EMIRP numbers.
- for(i = 0; i < e; i++) {
- printf("%s%d%s", ((((i + 1) % 10) == 0)?"\n":""), emirps[i], (((i % 10) < 9)?", ":""));
- }
- return 0;
- }
- bool isPalindrome(int n) {
- char *pCN, *pCNR, cN[20];
- pCN = getString(n);
- pCNR = reverse(pCN); // Reverse the characters in pCN and assign them to pCNR
- return (atoi(pCN) == atoi(pCNR)); // Return whether or not the two c-strings are equal.
- }
- bool isEMIRP(int n) {
- if(isPalindrome(n)) return false; // If N is a palindrome, return false.
- else {
- // If both N and its reverse are prime, return true.
- if(isPrime(n) && isPrime(atoi(reverse(getString(n))))) return true;
- }
- return false;
- }
- bool isPrime(int n) {
- int i;
- int primeFactors[4] = {2, 3, 5, 7}; // The prime factors used to determine if N is prime.
- // Iterate through the prime factors, checking to see if N is evenly divisible by any of them, or N itself is a prime factor.
- for(i = 0; i < 4; i++) {
- if(n == primeFactors[i]) return true;
- else if((n % primeFactors[i]) == 0) return false;
- }
- return true; // If N is not a prime factor, and is not evenly divisible by any of them, N is prime.
- }
- char * getString(int n) {
- char *r, *c, s[20];
- int digits, i;
- sprintf(s, "%d", n); // Create a textual representation of N.
- // Count the number of digits in N.
- digits = 0;
- while(n > 0) {
- n /= 10;
- digits++;
- }
- r = (char*)malloc(digits + 1);
- c = r;
- // Copy the contents of cN to pCN
- for(i = 0; i < digits; i++) {
- *c = s[i];
- c++;
- }
- *c = '\0';
- return r;
- }
- char * reverse(char * s) {
- int i, j;
- int len = strlen(s);
- char * r = (char*)malloc(len + 1);
- /* Iterate backwards through S and forwards through R, adding
- characters to R from S to get the reverse c-string. */
- for(i = (len - 1), j = 0; i >= 0, j < len; i--, j++) {
- r[j] = s[i];
- }
- r[len] = '\0';
- return r;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement