Advertisement
Bartimaeus

Modular Arithmetic

Jan 7th, 2019
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 2.95 KB | None | 0 0
  1. let (%) (a,b) c = (a*b) mod c ;;
  2.  
  3. let print_list l = print_string "["; List.iteri (fun i x -> if i+1=List.length l then print_int x else
  4.     (print_int x; print_string "; ")) l; print_string "]";;
  5. let print_list_of_lists l = List.iter (fun lt -> print_list lt; print_string ";   \n") l ;;
  6.  
  7. let rec repeat n(*раз*) x = if n<=0 then [] else x :: (repeat (n-1) x) ;;
  8.  
  9. let classes n = List.mapi (fun i z1 -> (i,List.mapi (fun j z2 -> (i*j) mod (n)) (repeat n 0))) (repeat n 0) ;;
  10.  
  11. let rec find_i (i,l) = match l with
  12.     | [] -> []
  13.     | (a,lst) :: t -> if a=i then lst else find_i (i,t) ;;
  14.  
  15. let rec find x l = match l with
  16.     | [] -> false
  17.     | a :: b -> if a=x then true else find x b ;;
  18.  
  19. let rec liberty l = match l with
  20.     | [] -> []
  21.     | -1 :: t -> liberty t
  22.     | a :: b -> if find a b = true then liberty b else a :: (liberty b) ;;
  23.  
  24. let generator l = let help () = List.map (fun (i,x) -> List.map (fun y ->
  25.         if find i (find_i (y,l))=true then y else -1) x) l in (List.map (fun l -> liberty l) (help ())) ;;
  26.  
  27. let rec nlib lst =
  28.     match lst with
  29.     | [] -> []
  30.     | a :: b -> a@(nlib b) ;;
  31.  
  32. print_string "Введите n : " ;;
  33. let n = read_int () ;;
  34. let all_classes_by_n = (generator (classes n));;
  35.  
  36. let associativity l = List.for_all (fun (x,y,z) -> if (x,(y,z)%n)%n=((x,y)%n,z)%n then true else false)
  37.     (nlib (List.map (fun a -> nlib (List.map (fun b -> List.map (fun c -> (a,b,c)) l) l)) l)) ;;
  38.  
  39. let identity_element l = if (List.map (fun x -> if (List.exists (fun y -> if (x,y)%n=y then true else false) l) = true
  40.     then x else -1) l) <> [] then true else false ;;
  41.  
  42. let id_element l = let lst=liberty (List.map (fun x -> if (List.exists (fun y -> if (x,y)%n=y then true else false) l) = true
  43.     then x else -1) l) in if lst=[] then -1 else List.nth lst 0 ;;
  44.  
  45. let closure l = List.for_all (fun (x,y) -> find ((x,y)%n) l) (nlib (List.map (fun i -> List.map (fun j -> (i,j)) l) l)) ;;
  46.  
  47. let inverse_element l = if identity_element l then List.for_all (fun a -> List.exists
  48.     (fun b -> if (a,b)%n=(id_element l) then true else false) l) l
  49.     else false ;;
  50.  
  51. List.iter (fun classes_by_n -> print_list classes_by_n;
  52. let p1 = associativity classes_by_n in ( let p2 = identity_element classes_by_n in ( let p3 = closure classes_by_n in (
  53.     let p4 = inverse_element classes_by_n in (
  54.     if p1 then print_string "\t - Есть ассоциативность;  " else print_string "\t - Ассоциативности нет; ";
  55.     if p2 then print_string "\tЕсть нейтральный; " else print_string "\tНет нейтрального; ";
  56.     if p3 then print_string "\tЕсть замкнутось; " else print_string "\tНет замкнутости; ";
  57.     if p4 then print_string "\tЕсть обратимость; " else print_string "\tНет обратимости; ";
  58.     if p1 && p2 && p3 && p4 then print_string "\tЭто группа\n" else print_string "\tЭто не группа\n"
  59.     ) )))) all_classes_by_n ;;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement