Emperor-Chinohito

isTheNumberAcceptable but improved

Sep 6th, 2024 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.55 KB | Source Code | 0 0
  1. /*
  2. This program finds if I like a number or not.
  3. As a rule, I only like multiples of 2, 6, or 10.
  4. There might be exceptions like 76, but that's rare.
  5.  
  6. Please note: I only really know Python, so this code might be rough
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <stdbool.h>
  11.  
  12. bool isPrime(int num) {
  13.    
  14.     // If a number is even, but not 2, return false
  15.     if (num == 2) {
  16.         return true;
  17.     }
  18.  
  19.     if (num <= 1 || num % 2 == 0) {
  20.         return false;
  21.     }
  22.  
  23.     // Check every odd factor of a number, since we already got rid of all the even numbers
  24.     for (int i = 3; i < num/2; i += 2) {
  25.         if (num % i == 0) {
  26.             return false;
  27.         }
  28.     }
  29.  
  30.     return true;
  31. }
  32.  
  33. bool isGood(int num) {
  34.    
  35.     // Upon second thought, I kinda like 76 but that's the only exception
  36.     if (num == 76) {
  37.         return true;
  38.     }
  39.  
  40.     // Get rid of 54 manually
  41.     if (num == 54) {
  42.        return false;
  43.     }
  44.  
  45.     if (num % 2 == 0) {
  46.  
  47.         // Get rid of even numbers with prime factors other than 3 and 5
  48.         for (int i = 2; i < num; i++) {
  49.             if (isPrime(i) && i != 2 && i != 3 && i != 5 && num % i == 0) {
  50.                 return false;
  51.             }
  52.         }
  53.  
  54.     } else {
  55.         return false; // Get rid of all odd numbers
  56.     }
  57.  
  58.     return true;
  59. }
  60.  
  61. int main() {
  62.     int number;
  63.     printf("Enter a number: ");
  64.     scanf("%d", &number);
  65.  
  66.     if (isGood(number)) {
  67.         printf("%d is acceptable\n", number);
  68.     } else {
  69.         printf("%d is NOT acceptable\n", number);
  70.     }
  71.  
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment