Advertisement
LA77

Untitled

Sep 23rd, 2024
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. /* file: superprimes.c */
  2. /* author: Anastase Luca-George (email: l.anastase@student.rug.nl) */
  3. /* date: 9/23/2024 */
  4. /* version: 2.0 */
  5. /* Description: This program tells how many numbers between a and b are superprimes */
  6.  
  7. #include <stdio.h>
  8.  
  9. /// A function to check whether or not a number is prime
  10. int is_prime(int x)
  11. {
  12. if(x == 1 || x == 0)
  13. return 0;
  14. if(x == 2 || x == 3)
  15. return 1;
  16. if(x % 2 == 0 || x % 3 == 0)
  17. return 0;
  18. for(int i = 5; i * i <= x; i+=6) // skipping multiples and 2 and 3
  19. if(x % i == 0 || x % (i + 2) == 0)
  20. return 0;
  21. return 1;
  22. }
  23.  
  24. int main(){
  25.  
  26. int a, b, cnt = 0;
  27. scanf("%d %d", &a, &b);
  28.  
  29. /// Iterating through all numbers between a and b
  30. for(int i = a; i < b; ++ i){
  31.  
  32. int f1 = 1;
  33. int x = i;
  34.  
  35. // Checking if a number is superprime
  36. if(is_prime(x)){
  37. while(x > 9){
  38. int sum = 0, c = x;
  39.  
  40. while(c){
  41. sum+= (c % 10);
  42. c /= 10;
  43. }
  44.  
  45. if(!is_prime(sum)){
  46. f1 = 0;
  47. break;
  48. }
  49.  
  50. x = sum;
  51. }
  52.  
  53. if(f1 == 1){
  54. cnt++;
  55. printf("%d\n", cnt);
  56. }
  57. }
  58. }
  59.  
  60. printf("%d\n", cnt);
  61.  
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement