using System; namespace ChiffresEtNombres { class MainClass { /************************************************/ public static int unites(int n) { return n%10; } /************************************************/ public static int dizaines(int n) { return unites(n/10); } /************************************************/ public static int extrait(int n, int p) { for (int i = 1 ; i <= p-1 ; i++) n /= 10; return unites(n); } /* version récursive : */ public static int extraitRec(int n, int p) { if (p==1) return unites(n); else return extraitRec(n / 10, p - 1); } /************************************************/ public static int nbChiffres(int n) { if (n == 0) return 1; int nb = 0; while(n != 0) { nb++; n/=10; } return nb; } /* Version récursive : */ public static int nbChiffresRec(int n) { if (n >= -9 && n <= 9) return 1; return 1 + (nbChiffresRec(n/10)); } /************************************************/ public static int sommeChiffres(int n) { int somme = 0; while(n != 0) { somme += n%10; n /= 10; } return somme; } /* version récursive : */ public static int sommeChiffresRec(int n) { if (n == 0) return 0; return sommeChiffresRec(n/10) + n%10; } /************************************************/ public static int sommeDiviseursStricts(int n) { int somme = 0; for (int diviseur = 1; diviseur * 2 <= n ; diviseur ++) if (n%diviseur==0) somme += diviseur; return somme; } /************************************************/ public static bool sontAmis(int a, int b) { return sommeDiviseursStricts(a) == b && sommeDiviseursStricts(b) == a;; } /************************************************/ public static bool estParfait(int n) { return sommeDiviseursStricts(n) == n; } /************************************************/ public static int sommeParties(int n, int p) { int gauche = n; int droite = 0; for (int i = p ; i >= 1 ; i--) { gauche /= 10; droite = 10*droite + extrait(n, i); } return gauche + droite; } /************************************************/ public static bool estKaprekar(int n) { int carre = n*n; int nb = nbChiffres(carre); for(int i = 1 ; i < nb ; i++) if (sommeParties(carre, i) == n) return true; return false; } /************************************************/ public static void Main (string[] args) { int n = 456238765; Console.WriteLine("n = " + n); Console.WriteLine("unités = " + unites(n)); Console.WriteLine("dizaines = " + dizaines(n)); Console.WriteLine("milliers = " + extrait(n, 3) + "(="+ extraitRec(n, 3) +")"); Console.WriteLine("nb de chiffres = " + nbChiffres(n) + "(="+ nbChiffresRec(n) +")"); Console.WriteLine("somme des chiffres = " + sommeChiffres(n) + "(="+ sommeChiffresRec(n) +")"); int i = 220, j=284; Console.WriteLine("Somme des diviseurs de " + i + " = "+ sommeDiviseursStricts(i)); Console.WriteLine("Somme des diviseurs de " + j + " = "+ sommeDiviseursStricts(j)); int k = 12540, p = 2; Console.WriteLine("sommeParties(" + k + ", " + p + ") = " + sommeParties(k, p)); Console.WriteLine("estKaprekar(" + k + ") = " + estKaprekar(k)); int l = 4879; Console.WriteLine("estKaprekar(" + l + ") = " + estKaprekar(l)); } } }