some prototype mini utils
This commit is contained in:
parent
e21b888b6c
commit
921377c2cc
6 changed files with 163 additions and 0 deletions
math/fib
9
math/fib/Cargo.toml
Normal file
9
math/fib/Cargo.toml
Normal file
|
@ -0,0 +1,9 @@
|
|||
[package]
|
||||
name = "fib"
|
||||
version = "0.1.0"
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
nested = { path = "../../nested" }
|
58
math/fib/src/main.rs
Normal file
58
math/fib/src/main.rs
Normal file
|
@ -0,0 +1,58 @@
|
|||
|
||||
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 )
|
||||
( Pipe Shot MachineWord )
|
||||
");
|
||||
|
||||
let mut f1 = unsafe { File::from_raw_fd(1) };
|
||||
eprintln!("
|
||||
< 1: n'th fibonacci number
|
||||
( ℕ )
|
||||
( MachineInt )
|
||||
( MachineWord )
|
||||
( Pipe Shot MachineWord )
|
||||
");
|
||||
|
||||
nested::magic_header();
|
||||
|
||||
let mut bytes = [0 as u8; 8];
|
||||
f0.read_exact(&mut bytes);
|
||||
let n = u64::from_le_bytes(bytes);
|
||||
bytes = fib(n).to_le_bytes();
|
||||
f1.write(&bytes);
|
||||
}
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue