lib-nested/math/fib/src/main.rs

63 lines
997 B
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use std::{
fs::File,
io::{Read, Write},
os::unix::io::FromRawFd,
};
fn fib(n: u64) -> u64 {
let mut y = 0;
let mut y1 = 1;
let mut y2 = 0;
for _ in 0..n {
y = y1 + y2;
y2 = y1;
y1 = y;
}
y
}
fn main() {
nested::magic_header();
eprintln!(" Fibonacci Sequence");
nested::magic_header();
eprintln!(
"
interface (Sequence ) 0 1"
);
let mut f0 = unsafe { File::from_raw_fd(0) };
eprintln!(
"
>0: n
( )
( MachineInt )
( MachineWord )
( Stream MachineSyllab )
"
);
let mut f1 = unsafe { File::from_raw_fd(1) };
eprintln!(
"
<1: n'th fibonacci number
( )
( MachineInt )
( MachineWord )
( Stream MachineSyllab )
"
);
nested::magic_header();
let mut bytes = [0 as u8; 8];
f0.read_exact(&mut bytes).expect("");
let n = u64::from_le_bytes(bytes);
bytes = fib(n).to_le_bytes();
f1.write(&bytes).expect("");
}