Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 0.74 KB | None | 0 0
  1.  
  2. let chiffres = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";;
  3. let chiffre v =
  4.   if 0 <= v && v < String.length chiffres then
  5.     String.sub chiffres v 1
  6.   else
  7.     "?(" ^ string_of_int v ^ ")"
  8. ;;
  9.  
  10. let rec ecriture b n =
  11.   if n = 0 then
  12.     ""
  13.   else
  14.     let alpha = n mod b in
  15.     let dernier = chiffre alpha in
  16.     let n' = n / b in
  17.     let reste = ecriture b n' in
  18.     reste ^ dernier
  19. ;;
  20.  
  21. let rec ecriture' b n =
  22.   if n = 0 then
  23.     ""
  24.   else
  25.     (ecriture' b (n / b)) ^ (chiffre (n mod b))
  26. ;;
  27.  
  28. let ecrit b n =
  29.   if n < 0 then
  30.     failwith "Pas de nombre negatif"
  31.   else if n = 0 then
  32.     print_endline "0"
  33.   else
  34.     print_endline (ecriture b n)
  35. ;;
  36.  
  37. let ecrit_16 = ecrit 16;;
  38.  
  39. ecrit_16 65535;;
  40. ecrit 37 36;;
  41. ecrit 37 65535;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement