Prime numbers in range.

Lucaci Andrew 0 Tallied Votes 585 Views Share

Ok, so, this code snippet is a reply to this thread: http://www.daniweb.com/software-development/cpp/threads/425821/prime-number-c
From what I understood form the OPs request, is that, he wanted an algorihm which would check for prime numbers in a given range, applying these conditions:
a number is to be considered valid if:
a. the number itself is a prime number.
b. the digits composing the number are prime, like this:

37397 is a prime number:
3 is a prime number
37 is a prime number
373 is a prime number
3739 is a prime number

I would not recommend this algorithm thou because it's slow, and is based on multiplications and divisions, and it has a lot of conditions to check.
But, it's a "hard" approach to a given problem.

/*
 * prime.cpp
 * prime.cpp is licensed under GNU GENERAL PUBLIC LICENSE
 *  Created on: Jun 18, 2012
 *      Author: sin
 */

#include <iostream>
#include <assert.h>
using namespace std;

bool isprime(int s){
	if (s==0 or s==1) return (false);
	for (int i=2;i<=s/2;i++)
		if (s%i==0) return (false);
	return (true);
}

bool isDigitPrime(int s){
	if (!isprime(s/10000)) return (false);
	else{
		if (!isprime(s/1000)) return (false);
		else{
			if (!isprime(s/100)) return (false);
			else{
				if (!isprime(s/10)) return (false);
				else{
					return (true);
				}
			}
		}
	}
}

void test(){
	assert(isprime(37397)==true);
	assert(isprime(3)==true);
	assert(isprime(37)==true);
	assert(isprime(373)==true);
	assert(isprime(3739)==true);
}

int main(){
	test();
	int a, b;
	cout<<"Insert the range(min. 10k|max. 99999).\n";
	cout<<"Min: ";
	cin>>a;
	cout<<"Max: ";
	cin>>b;
	if (a>=10000 or b<100000){
		for (int i=a;i<b;i++){
			if (isprime(i)){
				if (isDigitPrime(i)){
					cout<<i<<endl;
				}
			}
		}
	}
	else{
		cout<<"Invalid range.\n";
	}
	return (0);
}
Lucaci Andrew 140 Za s|n

And if you fancy strings, here's a modification to the current code:

int toInt(string s){
    stringstream to;
    to<<s;
    int a;
    to>>a;
    return (a);
}

bool isDigitPrime(int s){
    stringstream prime;
    string is;
    prime<<s;
    is.push_back(prime.str()[0]);
    if (!isprime(toInt(is))) return (false);
    else{
        is.push_back(prime.str()[1]);
        if (!isprime(toInt(is))) return (false);
        else{
            is.push_back(prime.str()[2]);
            if (!isprime(toInt(is))) return (false);
            else{
                is.push_back(prime.str()[3]);
                if (!isprime(toInt(is))) return (false);
                else return (true);
            }
        }
    }
}
LatSo 0 Newbie Poster

sin I really appreciate it but is this snippet do the following?

a.) What range of prime he wants ( but with a default of 10k-100k ; so the user can only give the range he want if it is between 10k-50k
b.) then the program will generate all prime numbes in that given range , but the twist is the prime number that will be generated is not the normal prime number but instead it will generate prime number that is every component digit is aslo prime

Lucaci Andrew 140 Za s|n

I thought this was the rule of your required application.
Indeed, I have extended the range, letting you to modify my code snippet if you fancy to use it.
You can put whatever range you like, just modify this range condition: if (a>=10000 or b<100000)

a.) What range of prime he wants ( but with a default of 10k-100k ; so the user can only give the range he want if it is between 10k-50k

Yes, by the very own starting point of the program.

    int a, b;
    cout<<"Insert the range(min. 10k|max. 99999).\n";
    cout<<"Min: ";
    cin>>a;
    cout<<"Max: ";
    cin>>b;

b.) then the program will generate all prime numbes in that given range , but the twist is the prime number that will be generated is not the normal prime number but instead it will generate prime number that is every component digit is aslo prime

if

b. the digits composing the number are prime, like this:

37397 is a prime number:
3 is a prime number
37 is a prime number
373 is a prime number
3739 is a prime number

this is the rule, than it does the required thing.

LatSo 0 Newbie Poster

will this code snippet generate all possible prime numbers in the given range?

Lucaci Andrew 140 Za s|n

This code snippet will generate all posible prime numbers in a given range (which is also included in the range (10k, 100k-1) and also whom's digits obey the following rule:

b. the digits composing the number are prime, like this:

37397 is a prime number:
3 is a prime number
37 is a prime number
373 is a prime number
3739 is a prime number

is this too hard to understand?

LatSo 0 Newbie Poster

im just being sure , tnx btw ^_^

so the whole code will be like this

#include <iostream>
    #include <assert.h>
    using namespace std;

    int toInt(string s){
        stringstream to;
        to<<s;
        int a;
        to>>a;
        return (a);
        }
        bool isDigitPrime(int s){
        stringstream prime;
        string is;
        prime<<s;
        is.push_back(prime.str()[0]);
        if (!isprime(toInt(is))) return (false);
        else{
        is.push_back(prime.str()[1]);
        if (!isprime(toInt(is))) return (false);
        else{
        is.push_back(prime.str()[2]);
        if (!isprime(toInt(is))) return (false);
        else{
        is.push_back(prime.str()[3]);
        if (!isprime(toInt(is))) return (false);
        else return (true);
        }
        }
        }
        }

        int main(){
    test();
    int a, b;
    cout<<"Insert the range(min. 10k|max. 99999).\n";
    cout<<"Min: ";
    cin>>a;
    cout<<"Max: ";
    cin>>b;
    if (a>=10000 or b<100000){
    for (int i=a;i<b;i++){
    if (isprime(i)){
    if (isDigitPrime(i)){
    cout<<i<<endl;
    }
    }
    }
    }
    else{
    cout<<"Invalid range.\n";
    }
    return (0);
    }
Lucaci Andrew 140 Za s|n

I'm not telling you to copy my code, and if you want to just chat, there's the personal message option avaible from my profile. This thread is a code snippet, and so, by taking the code, you can do whatever you like with it. Further on, if you have questions related to this topic, PM me.

RainbowMatrix -4 Newbie Poster
Code 1:
1. C program to determine prime number
2. Determining if a number is prime in c
3. C program to find given number is prime or not

#include<stdio.h>

int main(){

    int num,i,count=0;
    printf("Enter a number: ");
    scanf("%d",&num);
    for(i=2;i<=num/2;i++){
        if(num%i==0){
         count++;
            break;
        }
    }
   if(count==0 && num!= 1)
        printf("%d is a prime number",num);
   else
      printf("%d is not a prime number",num);
   return 0;
}

Sample output:
Enter a number: 5
5 is a prime number

Code 2:
1. C program for prime numbers between 1 to 100
2. How to find prime numbers from 1 to 100 in c
3. How to print prime numbers from 1 to 100 in c

#include<stdio.h>

int main(){
    int num,i,count;

    for(num = 1;num<=100;num++){
         count = 0;

         for(i=2;i<=num/2;i++){
             if(num%i==0){
                 count++;
                 break;
             }
        }

         if(count==0 && num!= 1)
             printf("%d ",num);
    }

   return 0;
}

Output:
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

Code 3:
1. C program for prime numbers between 1 to n
2. C program to find prime numbers up to n
3. C program to list prime numbers
4. Write a c program to generate n prime numbers
5. C program to find n prime numbers

#include<stdio.h>

int main(){

    int num,i,count,n;
    printf("Enter max range: ");
    scanf("%d",&n);

    for(num = 1;num<=n;num++){

         count = 0;

         for(i=2;i<=num/2;i++){
             if(num%i==0){
                 count++;
                 break;
             }
        }

         if(count==0 && num!= 1)
             printf("%d ",num);
    }

   return 0;
}

Sample output:
Enter max range: 50
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

Code 4:
1. C program to find prime numbers using while loop
2. Wap to find prime numbers in c
3. Write a c program to generate prime number
4. How to get prime numbers in c

#include<stdio.h>

int main(){

   int num,i,count,min,max;

printf("Enter min range: ");
    scanf("%d",&min);

    printf("Enter max range: ");
    scanf("%d",&max);

    num = min;
    while(num<=max){

         count = 0;
         i=2;

         while(i<=num/2){
             if(num%i==0){
                 count++;
                 break;
             }
             i++;
        }

         if(count==0 && num!= 1)
             printf("%d ",num);

         num++;
    }

   return 0;
}

Sample output:
Enter min range: 50
Enter max range: 100
53 59 61 67 71 73 79 83 89 97

Code 5:
1. How to find out prime numbers in c programming
2. Display prime numbers in c
3. C program to find prime numbers between two numbers
4. C code to display prime numbers within a range

#include<stdio.h>

int main(){

    int num,i,count,min,max;

     printf("Enter min range: ");
     scanf("%d",&min);

    printf("Enter max range: ");
    scanf("%d",&max);

    for(num = min;num<=max;num++){

         count = 0;

         for(i=2;i<=num/2;i++){
             if(num%i==0){
                 count++;
                 break;
             }
        }

         if(count==0 && num!= 1)
             printf("%d ",num);
    }

   return 0;
}

Sample output:
Enter min range: 10
Enter max range: 50
11 13 17 19 23 29 31 37 41 43 47

Code 6:
1. Sum of prime numbers from 1 to 100 in c

#include<stdio.h>

int main(){

    int num,i,count,sum=0;

    for(num = 1;num<=100;num++){

         count = 0;

         for(i=2;i<=num/2;i++){
             if(num%i==0){
                 count++;
                 break;
             }
        }

         if(count==0 && num!= 1)
             sum = sum + num;
    }

    printf("Sum of prime numbers is: %d ",sum);

   return 0;
}

Output:
Sum of prime numbers is: 1060

Code 7:
1. C program to find sum of prime numbers

#include<stdio.h>

int main(){

    int num,i,count,min,max,sum=0;

     printf("Enter min range: ");
     scanf("%d",&min);

    printf("Enter max range: ");
    scanf("%d",&max);

    for(num = min;num<=max;num++){

         count = 0;

         for(i=2;i<=num/2;i++){
             if(num%i==0){
                 count++;
                 break;
             }
        }

         if(count==0 && num!= 1)
             sum = sum + num;
    }

    printf("Sum of prime numbers is: %d ",sum);

   return 0;
}

Sample output:
Enter min range: 50
Enter max range: 100
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.