Print Highly Prime Numbers in an Input Interval | C Program

In summary, the program is designed to find and print all highly prime numbers within a given interval. Prime numbers are considered highly prime if the deletion of every digit from right to left results in a prime number. The program uses functions to check if a number is prime and if it is highly prime. It also includes a print function and a main function to prompt the user for input and output the results. However, there may be logic errors in the isPrime function that could cause incorrect results in some cases.
  • #1
gruba
206
1

Homework Statement


Write a program that will print all highly prime numbers from the input interval <a,b>. Prime number is highly prime if deletion of every digit from right is a prime.

Example:
239 is highly prime because 239,23,2 are primes.

2. The attempt at a solution
Could someone point out what are logic errors in the following program:
Code:
#include <stdio.h>

int isPrime(int p);
int isHighlyPrime(int n);
void printHighlyPrimeNumbers(int a,int b);

// check if p is a prime
int isPrime(int p)
{
  int nd=2;//number of divisors
  int d;//current divisor

  for(d=2; d<p/2; d++)
  {
  if(p % d == 0)
  nd++;
  }
  if(nd == 2)
  return 1;
  return 0;
}

// check if n is highly prime
int isHighlyPrime(int n)
{
  int temp=n;
  int i;
  int digitCount=0;//number of digits in n

  while(temp != 0)
  {
  temp=temp/10;
  digitCount++;
  }
  temp=n;

  //if number of digits is 1, only check if prime
  if(digitCount == 1)
  {
  if(isPrime(temp))
  return 1;
  }

  //else check if n,n/10,(n/10)/10,... are primes
  else
  {
  for(i=1; i<=digitCount-1 && isPrime(temp); i++)
  {
  if(isPrime(temp))
  temp=temp/10;
  }
  return 1;
  }
  return 0;
}

//print highly primes from a to b
void printHighlyPrimeNumbers(int a,int b)
{
  int i;
  for(i=a; i<=b; i++)
  {
  if(isHighlyPrime(i))
  printf("%d ",i);
  }
}

int main()
{
  int a,b;
  do
  {
  printf("a = ");
  scanf("%d",&a);
  }while(a<1);
  do
  {
  printf("b = ");
  scanf("%d",&b);
  }while(b<1);
 
  //swap if a>b
  if(a > b)
  {
  int temp=a;
  a=b;
  b=temp;
  }

  printHighlyPrimeNumbers(a,b);

  return 0;
}
 
Last edited:
Physics news on Phys.org
  • #2
gruba said:

Homework Statement


Write a program that will print all highly prime numbers from the input interval <a,b>. Prime number is highly prime if deletion of every digit from right is a prime.

Example:
239 is highly prime because 239,23,2 are primes.

2. The attempt at a solution
Could someone point out what are logic errors in the following program:
Code:
#include <stdio.h>

int isPrime(int p);
int isHighlyPrime(int n);
void printHighlyPrimeNumbers(int a,int b);

// check if p is a prime
int isPrime(int p)
{
  int nd=2;//number of divisors
  int d;//current divisor

  for(d=2; d<p/2; d++)
  {
  if(p % d == 0)
  nd++;
  }
  if(nd == 2)
  return 1;
  return 0;
}

// check if n is highly prime
int isHighlyPrime(int n)
{
  int temp=n;
  int i;
  int digitCount=0;//number of digits in n

  while(temp != 0)
  {
  temp=temp/10;
  digitCount++;
  }
  temp=n;

  //if number of digits is 1, only check if prime
  if(digitCount == 1)
  {
  if(isPrime(temp))
  return 1;
  }

  //else check if n,n/10,(n/10)/10,... are primes
  else
  {
  for(i=1; i<=digitCount-1 && isPrime(temp); i++)
  {
  if(isPrime(temp))
  temp=temp/10;
  }
  return 1;
  }
  return 0;
}

//print highly primes from a to b
void printHighlyPrimeNumbers(int a,int b)
{
  int i;
  for(i=a; i<=b; i++)
  {
  if(isHighlyPrime(i))
  printf("%d ",i);
  }
}

int main()
{
  int a,b;
  do
  {
  printf("a = ");
  scanf("%d",&a);
  }while(a<1);
  do
  {
  printf("b = ");
  scanf("%d",&b);
  }while(b<1);

  //swap if a>b
  if(a > b)
  {
  int temp=a;
  a=b;
  b=temp;
  }

  printHighlyPrimeNumbers(a,b);

  return 0;
}
This part I don't get:
Code:
for(i=1; i<=digitCount-1 && isPrime(temp); i++)
  {
  if(isPrime(temp))
  temp=temp/10;
  }
I would keep the for loop simple:
Code:
for(i=1; i<=digitCount-1; i++)
Then, if isPrime(temp) returns 0, isHighlyPrime should immediately return 0.

(Also, note that your isPrime function considers 4 a prime number.)
 
  • #3
gruba said:
Code:
else
  {
  for(i=1; i<=digitCount-1 && isPrime(temp); i++)
  {
  if(isPrime(temp))
  temp=temp/10;
  }
  return 1;
  }
  return 0;
}
Seems flawed: if it's prime you don't have to IF again. If it's not prime, you should not return 1 .

[edit] quick work Samy !
 
  • #4
[EDIT]I have figured out the problem, here is the working program:
Code:
#include <stdio.h>

int isPrime(int p);
int isHighlyPrime(int n);
void printHighlyPrimeNumbers(int a,int b);

// check if p is a prime
int isPrime(int p)
{
  int i = 2;
 if(p == 1 || p == 7)
    return 0;
  while(i<p){
  if(!(p % i)) return 0;
  i = 2*i-1;
  }
  return 1;
}

// check if n is highly prime
int isHighlyPrime(int n)
{
  int temp=n;
  int i;
  int digitCount=0;//number of digits in n

  while(temp != 0)
  {
  temp=temp/10;
  digitCount++;
  }
  temp=n;

  //if number of digits is 1, only check if prime
  if(digitCount == 1)
  {
  if(isPrime(temp))
  return 1;
  }

  //else check if n,n/10,(n/10)/10,... are primes
  else
  {
  while(temp>0)
  {
  if(isPrime(temp) == 0)
  {
  return 0;
  }
  temp =temp/10;
  }
  return 1;
  }
  return 0;
}

//print highly primes from a to b
void printHighlyPrimeNumbers(int a,int b)
{
  int i;
  for(i=a; i<=b; i++)
  {
  if(isHighlyPrime(i))
  printf("%d ",i);
  }
}

int main()
{
  int a,b;
  do
  {
  printf("a = ");
  scanf("%d",&a);
  }while(a<1);
  do
  {
  printf("b = ");
  scanf("%d",&b);
  }while(b<1);

  //swap if a>b
  if(a > b)
  {
  int temp=a;
  a=b;
  b=temp;
  }

  printHighlyPrimeNumbers(a,b);

  return 0;
}
 
Last edited:
  • #5
Some examples: the program treats numbers as 113, 131, 137 ... as highly prime.
Also 77, 371, 377 and 1133, which are not prime.

Something still wrong with the isPrime function.
 
Last edited:
  • #6
Samy_A said:
Something still wrong with the isPrime function.
I have edit the previous post, but not sure if it will correct possible error.
 
  • #7
gruba said:
I have edit the previous post, but not sure if it will correct possible error.
gruba said:
[EDIT]I have figured out the problem, here is the working program:
Code:
int isPrime(int p)
{
  int i = 2;
if(p == 1 || p == 7)
    return 0;
  while(i<p){
  if(!(p % i)) return 0;
  i = 2*i-1;
  }
  return 1;
}
Still gives 371 and 373 as primes (as examples).
As 7 is prime, returning 0 when p==7 doesn't make much sense.

The key issue is this: in isPrime, you check if p is divisible by i=2, and then by i=2*2-1=3, i=2*3-1=5, i=2*5-1=9, ...
See the problem?
 
Last edited:
  • #8
Samy_A said:
Still gives 371 and 373 as primes (as examples).
As 7 is prime, returning 0 when p==7 doesn't make much sense.
In isPrime, you check if p is divisible by i=2, and then by i=2*2-1=3, i=2*3-1=5, i=2*5-1=9.
See the problem?

The following isPrime() function seems to work for interval 50 - 1200:
Code:
int isPrime(int p)
{
  int i;
  if(p == 1)
  return 0;
  for(i=2; i<=p/2; i++)
  if(p % i == 0)
  return 0;
  return 1;
}
 
  • #9
gruba said:
The following isPrime() function seems to work for interval 50 - 1200:
Code:
int isPrime(int p)
{
  int i;
  if(p == 1)
  return 0;
  for(i=2; i<=p/2; i++)
  if(p % i == 0)
  return 0;
  return 1;
}
Yes, this one looks ok (I'm tired, so I may miss something). There is room for improvement as far as performance is concerned, but probably that is not important at this stage.
 

1. How does this program print highly prime numbers in an input interval?

The program uses a loop to iterate through each number in the input interval. Within the loop, it checks if the number is prime by dividing it with all numbers from 2 to its square root. If it is found to be prime, it is printed on the screen.

2. What is the time complexity of this program?

The time complexity of this program is O(n*sqrt(n)), where n is the length of the input interval. This is because for each number in the interval, the program has to perform n/2 operations to check if it is prime.

3. Can this program handle large input intervals?

Yes, this program can handle large input intervals. It uses a long data type to store the numbers and the square root function to improve efficiency for larger numbers.

4. How does the program handle invalid input?

If the input interval is invalid, for example, if the starting number is greater than the ending number, the program will display an error message and terminate.

5. Are there any limitations to this program?

One limitation of this program is that it can only print prime numbers in the given input interval. It does not have the capability to check if a number is prime outside of the specified interval. Additionally, the program may take longer to run for larger input intervals.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
673
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
893
  • Engineering and Comp Sci Homework Help
Replies
4
Views
929
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
3
Replies
80
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
19
Views
2K
Back
Top