Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* file: superprimes.c */
- /* author: Anastase Luca-George (email: l.anastase@student.rug.nl) */
- /* date: 9/23/2024 */
- /* version: 2.0 */
- /* Description: This program tells how many numbers between a and b are superprimes */
- #include <stdio.h>
- /// A function to check whether or not a number is prime
- int is_prime(int x)
- {
- if(x == 1 || x == 0)
- return 0;
- if(x == 2 || x == 3)
- return 1;
- if(x % 2 == 0 || x % 3 == 0)
- return 0;
- for(int i = 5; i * i <= x; i+=6) // skipping multiples and 2 and 3
- if(x % i == 0 || x % (i + 2) == 0)
- return 0;
- return 1;
- }
- int main(){
- int a, b, cnt = 0;
- scanf("%d %d", &a, &b);
- /// Iterating through all numbers between a and b
- for(int i = a; i < b; ++ i){
- int f1 = 1;
- int x = i;
- // Checking if a number is superprime
- if(is_prime(x)){
- while(x > 9){
- int sum = 0, c = x;
- while(c){
- sum+= (c % 10);
- c /= 10;
- }
- if(!is_prime(sum)){
- f1 = 0;
- break;
- }
- x = sum;
- }
- if(f1 == 1){
- cnt++;
- printf("%d\n", cnt);
- }
- }
- }
- printf("%d\n", cnt);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement