Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #define TRUE (1==1)
- #define FALSE (1==0)
- /* Factorion : nombre qui est égal à la somme des factorielles de ses chiffres
- * Il en n'existe que 4 : 1, 2, 145 et 40 585.
- * 145 -> 1! + 4! + 5! = 1 + 24 + 120 = 145
- */
- int factorielle (int n)
- {
- if (n == 0)
- {
- return 1 ;
- }
- else
- {
- return (n*factorielle(n-1)) ;
- }
- }
- int nombre_chiffres (int n)
- {
- int compteur = 0 ;
- while (n != 0)
- {
- n /= 10 ;
- compteur++ ;
- }
- return compteur ;
- }
- // string_to_int : renvoie n sous forme d'une chaîne de caractères
- char* string_to_int (int n)
- {
- int nb_chiffres = nombre_chiffres(n) ;
- int i ;
- char* s = (char*) malloc ((nb_chiffres)*sizeof(char)) ;
- s[nb_chiffres] = '\0' ;
- for (i = nb_chiffres-1 ; i >= 0 ; i--)
- {
- s[i] = (n % 10) + '0' ;
- n /= 10 ;
- }
- return s ;
- }
- // char_to_int : renvoie le nombre associé à c
- int char_to_int (char c)
- {
- int d = 0 ;
- switch (c)
- {
- case '0' : d = 0 ; break ;
- case '1' : d = 1 ; break ;
- case '2' : d = 2 ; break ;
- case '3' : d = 3 ; break ;
- case '4' : d = 4 ; break ;
- case '5' : d = 5 ; break ;
- case '6' : d = 6 ; break ;
- case '7' : d = 7 ; break ;
- case '8' : d = 8 ; break ;
- case '9' : d = 9 ; break ;
- default : break ;
- }
- return d ;
- }
- //est_un_factorion : vérifie si n est un factorion
- int est_un_factorion(int n)
- {
- char* s = string_to_int(n) ;
- int nb_chiffres = nombre_chiffres(n) ;
- int b = FALSE ;
- int c, i ;
- unsigned int somme = 0 ;
- for (i = 0 ; i < nb_chiffres ; i++)
- {
- c = char_to_int(s[i]) ;
- somme += factorielle(c) ;
- /* On décompose n chiffre par chiffre
- * et pour chaque chiffre, somme = somme + factorielle(chiffre)
- */
- }
- if (somme == n)
- {
- b = TRUE ;
- }
- return b ;
- }
- void affichage_factorion(int n)
- {
- if (est_un_factorion(n) == TRUE)
- {
- printf("%d est un factorion.\n",n);
- }
- return ;
- }
- int main (int argc, char** argv)
- {
- int n ;
- if (argc == 2)
- {
- n = atoi(argv[1]) ;
- }
- else
- {
- printf("Nombre : ") ;
- scanf("%d",&n) ;
- }
- int i ;
- for (i = 1 ; i <= n ; i++)
- {
- affichage_factorion(i) ;
- }
- return 0 ;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement