Closed Yaya0312 closed 4 years ago
Il suffit de faire une fonction de conversion d'un grand nombre en polynôme. (non fonctionnelle)
let f b = match b with (a,b) -> (a, float_of_int b);;
let g b = match b with (a,b) -> (a, int_of_float b);;
let poly_of_gdnb (a:gdnb) =
let p = List.map f a.abs in
if (!a.signe) then
multcoef (-1.) p
else
p
;;
let gdnb_of_poly (p:poly) =
let p = List.map g p in
let t = in
if (!a.signe) then
{ signe= ; abs=p}
else
{ signe= ; abs=p}
;;
let karatsuba_gndb a b = karatsuba (poly_of_gdnb a) (poly_gdnb b) ;;
(*-----------Définition type grand nombre-----------*)
type gdnb = {signe : bool; abs : (int*float) list};;
type poly = (int*float) list;;
let gdnb1 = {signe = false; abs = [(0, 2.); (1, 4569.)]};;
let gdnb2 = {signe = false; abs = [(0,2.); (1, 4569.)]};;
(*----------Fonction transition string-abs----------*)
let rec abs_of_string_aux s cpt acc =
let len = String.length s in
if len <= 4 then List.rev (((cpt-1,(float_of_string s)) :: []) @ acc)
else
(abs_of_string_aux (String.sub s 0 (len-4)) (cpt+1) (((cpt-1,(float_of_string (String.sub s (len-4) 4))) :: []) @ acc));;
let abs_of_string s =
if(String.contains s '-') then
let chaine = String.sub s 1 ((String.length s)-1) in
abs_of_string_aux chaine 1 []
else
abs_of_string_aux s 1 [];;
abs_of_string "-12";;
mon code ci-dessus renvoie bien ce que l'on veut reste plus cas faire en sorte que la fonction renvoie sous cette forme: type gdnb : {signe = ??; abs =???}
Code Discord
let rec abs_of_string_aux s cpt acc =
let len = String.length s in
if len <= 4 then List.rev (((cpt-1,(float_of_string s)) :: []) @ acc)
else
(abs_of_string_aux (String.sub s 0 (len-4)) (cpt+1) (((cpt-1,(float_of_string (String.sub s (len-4) 4))) :: []) @ acc))
;;
let abs_of_string s =
if(String.contains s '-') then
let chaine = String.sub s 1 ((String.length s)-1) in
abs_of_string_aux chaine 1 []
else
abs_of_string_aux s 1 [];;
let signe (a:string) =
let b = float_of_string a in
if(b < 0.) then
true
else
false
;;
let gdnb (l:poly) =
let rec aux l acc =
match l with
| [] -> acc
| (coef,nb)::c -> aux c (acc +. (nb *.((10.)**4.)**float_of_int(coef)))
in aux l 0.
;;
let fonction (a:string) (b:string) (x:float) =
let a0 = abs_of_string a in
let b0 = abs_of_string b in
let t0 = signe a in
let t1 = signe b in
match t0,t1 with
| true,true -> let c0 = karatsuba a0 b0 x in
gdnb c0
| true,false-> let c0 = karatsuba a0 b0 x in
let c1 = multCoeff c0 (-1.)in
gdnb c1
| false,true-> let c0 = karatsuba a0 b0 x in
let c1 = multCoeff c0 (-1.)in
gdnb c1
|false,false-> let c0 = karatsuba a0 b0 x in
gdnb c0
;;
J'ai mis à jour le dépot git. J'ai ajouté mon travail sur la partie 4 (non fini) ainsi que ce que j'avais réalisé sur les grands nombres.
Adaptez cette méthode pour la multiplication de grands entiers. Indication : Vous devrez choisir un paramètre α de type entier et expliquer pourquoi (P 0 + αP 1 )(Q 0 + αQ 1 ) − R 0 − α 2 R 2 est bien divisible par α.