Commits

Antoine Pietri  committed b8b908b

trigonometry in degrees ; added factors.bl

  • Participants
  • Parent commits d4f5811

Comments (0)

Files changed (2)

File examples/factors.bl

+factors(n)
+{
+    vars lim, i;
+    lim = sqrt(n) + 1;
+    if (n == 1)
+    {
+        write(1);
+    }
+    i = 2;
+    while (i <= lim)
+    {
+        while (mod(n, i) == 0)
+        {
+            write(i);
+            n = n / i;
+        }
+        i = i + 1;
+    }
+    if (n > 1) {
+        write(n);
+    }
+}
+
+main()
+{
+    factors(read());
+}
+let pi = 3.14159265
+let to_degrees = function rad -> (rad *. 180.) /. pi
+let to_radians = function deg -> (deg *. pi) /. 180.
+let rd f x = truncate (f (to_radians (float x)))
+let ra f x = truncate (to_degrees (f (float x)))
+let ra2 f x y = truncate (to_degrees (f (float x) (float y)))
+
 let call fname args = match (fname, args) with
-  | ("write", _) -> List.iter (fun x -> print_int x; print_newline ()) args;
-                    (Some 0)
-  | ("read", []) -> Some (read_int ())
+  | ("write", _)    -> List.iter (fun x -> print_int x; print_newline ()) args;
+                    (Some 1)
+  | ("read", [])    -> print_string "> "; Some (read_int ())
   | ("random", [a]) -> Some (Random.int a)
   | ("max", [a; b]) -> Some (max a b)
   | ("min", [a; b]) -> Some (min a b)
   | ("mod", [a; b]) -> Some (a mod b)
   | ("xor", [a; b]) -> Some (a lxor b)
-  | ("succ", [a]) -> Some (succ a)
-  | ("pred", [a]) -> Some (pred a)
-  | ("abs", [a]) -> Some (abs a)
-  | ("pow", [a;n]) -> Some (truncate ((float a) ** (float n)))
-  | ("sqrt", [a]) -> Some (truncate (sqrt (float a)))
-  | ("exp", [a]) -> Some (truncate (exp (float a)))
-  | ("log", [a]) -> Some (truncate (log (float a)))
-  | ("log10", [a]) -> Some (truncate (log10 (float a)))
-  | ("cos", [a]) -> Some (truncate (cos (float a)))
-  | ("sin", [a]) -> Some (truncate (sin (float a)))
-  | ("tan", [a]) -> Some (truncate (tan (float a)))
-  | ("acos", [a]) -> Some (truncate (acos (float a)))
-  | ("asin", [a]) -> Some (truncate (asin (float a)))
-  | ("atan", [a]) -> Some (truncate (atan (float a)))
-  | ("cosh", [a]) -> Some (truncate (cosh (float a)))
-  | ("sinh", [a]) -> Some (truncate (sinh (float a)))
-  | ("tanh", [a]) -> Some (truncate (tanh (float a)))
-  | ("atan2", [a;b]) -> Some (truncate (atan2 (float a) (float b)))
+  | ("succ", [a])   -> Some (succ a)
+  | ("pred", [a])   -> Some (pred a)
+  | ("abs", [a])    -> Some (abs a)
+  | ("pow", [a;n])  -> Some (truncate ((float a) ** (float n)))
+  | ("sqrt", [a])   -> Some (truncate (sqrt (float a)))
+  | ("exp", [a])    -> Some (truncate (exp (float a)))
+  | ("log", [a])    -> Some (truncate (log (float a)))
+  | ("log10", [a])  -> Some (truncate (log10 (float a)))
+  | ("cos", [a])    -> Some (rd cos a)
+  | ("sin", [a])    -> Some (rd sin a)
+  | ("tan", [a])    -> Some (rd tan a)
+  | ("acos", [a])   -> Some (ra acos a)
+  | ("asin", [a])   -> Some (ra asin a)
+  | ("atan", [a])   -> Some (ra atan a)
+  | ("cosh", [a])   -> Some (rd cosh a)
+  | ("sinh", [a])   -> Some (rd sinh a)
+  | ("tanh", [a])   -> Some (rd tanh a)
+  | ("atan2",[a;b]) -> Some (ra2 atan2 a b)
   | _ -> None