function fib_(x: LongInt; var fib_2: LongInt): LongInt;
var
a, b, c, d: LongInt;
begin
if x = 0 then
begin
fib_ := 0;
fib_2 := 1;
end
else
begin
a := fib_(x div 2, b);
c := a * (2 * b - a);
d := b * b + a * a;
if x mod 2 = 0
then begin
fib_ := c;
fib_2 := d;
end
else begin
fib_ := d;
fib_2 := c + d;
end;
end;
end;
function fib(x: LongInt): LongInt;
var
y: LongInt;
begin
fib := fib_(x, y);
end;
var
x: LongInt;
begin
ReadLn(x);
x := fib(x);
WriteLn(x);
end.
|
main :: IO ()
main
= do x <- readLn
print (fib x)
fib :: Int -> Int
fib x = fst (fib_ x)
fib_ :: Int -> (Int, Int)
fib_ x
= if x == 0 then (0, 1) else
let (a, b) = fib_ (x `div` 2)
c = a * (2 * b - a)
d = b * b + a * a
in if x `mod` 2 == 0 then (c, d) else (d, c + d)
|
(*
function M(n: Integer): Integer; forward;
*)
function F(n: Integer): Integer;
begin
if n = 0 then
F := 1
else
F := n - M(F(n-1));
end;
function M(n: Integer): Integer;
begin
if n = 0 then
M := 0
else
M := n - F(M(n-1));
end;
var
i: Integer;
begin
for i := 0 to 19 do
write(F(i), ' ');
writeln;
for i := 0 to 19 do
write(M(i), ' ');
writeln;
end.
|
import Data.Foldable (traverse_)
main :: IO ()
main
= do traverse_ (\ i -> putStr (show (f i) ++ " ")) [0 .. 19]
putStrLn []
traverse_ (\ i -> putStr (show (m i) ++ " ")) [0 .. 19]
putStrLn []
f :: Int -> Int
f n = if n == 0 then 1 else n - m (f (n - 1))
m :: Int -> Int
m n = if n == 0 then 0 else n - f (m (n - 1))
|
var
i, j, x: Integer;
begin
WriteLn('Multiplication Table');
for i := 1 to 10 do begin
for j := 1 to 10 do begin
x := i*j;
Write(x);
if (x >= 1) and (x <= 9) then Write(' ');
if (x >= 10) and (x <= 99) then Write(' ');
end;
WriteLn();
end
end.
|
import Control.Monad (when)
import Data.Foldable (traverse_)
main :: IO ()
main
= do putStrLn "Multiplication Table"
traverse_
(\ i ->
do traverse_
(\ j ->
let x = i * j in
do putStr (show x)
when (x >= 1 && x <= 9) (putStr " ")
when (x >= 10 && x <= 99) (putStr " "))
[1 .. 10]
putStrLn [])
[1 .. 10]
|