pub struct FibBox { /* private fields */ }
Expand description
The Fibonacci Box, or FibBox struct contains 4 members: f1, dif, ctr, f2
f1 | Mpz, Integer | Lesser Factor | f2-2*dif | ctr-dif |
dif | Mpz, Integer | Difference of Squares | |(f2-f1)/2| | f2-ctr |
ctr | Mpz, Integer | Centered value | |(f2+f1)/2| | f1+dif |
f2 | Mpz, Integer | Greater Factor | f1+2*dif | dif+ctr |
The individual members are prviate. To access the individual members, use similarly named methods..
Usage:
use fibbox::FibBox;
let fb = FibBox::new(1, 1, 2, 3);
let f1 = fb.f1();
let dif = fb.dif();
let ctr = fb.ctr();
let f2 = fb.f2();
Each of the access methods is a borrow from the struct’s private members.
In addition, the core usage behind a Fibonacci Box is its relationship to Pythagorean Triples
use rug::Integer;
use rug::ops::Pow;
use fibbox::FibBox;
let fb = FibBox::new(1, 1, 2, 3);
let pt = fb.as_pytup(); // This is a tuple, 3 members, a pythagorean triple.
assert_eq!(Integer::from(fb.f1()*fb.f2()), pt.0);
let dos = Integer::from(2);
let csq = Integer::pow(Integer::from(fb.ctr()), 2);
let dsq = Integer::pow(Integer::from(fb.dif()), 2);
let cd = Integer::from(fb.ctr()*fb.dif());
assert_eq!(Integer::from(&csq-&dsq), pt.0);
assert_eq!(Integer::from(&dos*&cd), pt.1);
assert_eq!(Integer::from(&csq+&dsq), pt.2);
Implementations§
Source§impl FibBox
impl FibBox
Sourcepub fn new<T: Into<Integer>, U: Into<Integer>, V: Into<Integer>, W: Into<Integer>>(
f1: T,
dif: U,
ctr: V,
f2: W,
) -> FibBox
pub fn new<T: Into<Integer>, U: Into<Integer>, V: Into<Integer>, W: Into<Integer>>( f1: T, dif: U, ctr: V, f2: W, ) -> FibBox
If the gmp-mpfr-sys
library supports conversion,
this new function will support conversion from whichever types are input.
use rug::Integer;
use fibbox::FibBox;
let fb = FibBox::new(1i8, 1u16, 2i32, 3u64); // Works
let fb = FibBox::new(1i16, 1u32, 2u64, 3i128); // Works
let uno = Integer::from(1u8);
let dos = Integer::from(2i32);
let tres = Integer::from(3u128);
let fb = FibBox::new(&uno, &uno, dos, tres); // Works
Sourcepub fn new_from_tuple<T: Into<Integer>, U: Into<Integer>, V: Into<Integer>, W: Into<Integer>>(
tuvw: (T, U, V, W),
) -> FibBox
pub fn new_from_tuple<T: Into<Integer>, U: Into<Integer>, V: Into<Integer>, W: Into<Integer>>( tuvw: (T, U, V, W), ) -> FibBox
Accepts a tuple of 4 integers and returns a new instance.
Usage:
use fibbox::FibBox;
let tup = (1, 2, 3, 5);
let fb = FibBox::new_from_tuple(tup);
println!("{:?}", fb.as_pytup());
println!("{:?}", fb.as_tuple());
println!("{:?}", fb);
// Output:
// (5, 12, 13)
// (1, 2, 3, 5)
// 1 1 2 3
Sourcepub fn new_from_pytrip<A: Into<Integer>, B: Into<Integer>, C: Into<Integer>>(
a: A,
b: B,
c: C,
) -> FibBox
pub fn new_from_pytrip<A: Into<Integer>, B: Into<Integer>, C: Into<Integer>>( a: A, b: B, c: C, ) -> FibBox
Returns a new instance from 3 integer inputs, representing a Pythagorean Triple.
Usage:
use fibbox::FibBox;
let fb = FibBox::new_from_pytrip(3, 4, 5);
println!("{:?}", fb);
println!("{:?}", fb.as_pytup());
// Output:
// 1 1 2 3
// (3, 4, 5)
Sourcepub fn new_from_pytrip_tuple<A: Into<Integer>, B: Into<Integer>, C: Into<Integer>>(
abc: (A, B, C),
) -> FibBox
pub fn new_from_pytrip_tuple<A: Into<Integer>, B: Into<Integer>, C: Into<Integer>>( abc: (A, B, C), ) -> FibBox
Accepts a tuple of 3 integers, representing a Pythagorean Triple, and returns a new instance.
Usage:
use fibbox::FibBox;
let tup = (3, 4, 5);
let fb = FibBox::new_from_pytrip_tuple(tup);
println!("{:?}", fb.as_pytup());
println!("{:?}", fb);
// Output:
// (3, 4, 5)
// 1 1 2 3
Sourcepub fn as_tuple(&self) -> (Integer, Integer, Integer, Integer)
pub fn as_tuple(&self) -> (Integer, Integer, Integer, Integer)
Returns the struct’s members as a tuple (of 4 integers)
Usage:
use fibbox::FibBox;
let fb = FibBox::new(1, 1, 2, 3);
let tup = fb.as_tuple();
println!("{:?}", tup);
// Output:
// (1, 1, 2, 3)
Sourcepub fn as_pytup(&self) -> (Integer, Integer, Integer)
pub fn as_pytup(&self) -> (Integer, Integer, Integer)
Returns a 3 member tuple, (A, B, C), such that A^2+B^2==C^2
Usage:
use fibbox::FibBox;
let fb = FibBox::new(1, 1, 2, 3);
let pt = fb.as_pytup();
println!("{:?}", pt);
// Output:
// (3, 4, 5)
Sourcepub fn is_primitive(&self) -> bool
pub fn is_primitive(&self) -> bool
Returns true or false, whether or not the Fibonacci Box represents a primitive Pythagorean Triple or not.
Usage:
use fibbox::FibBox;
let fb = FibBox::new(1, 1, 2, 3); // (3, 4, 5)
println!("{:?}", fb.is_primitive()); // true
assert!(fb.gcd() == 1);
let np = FibBox::new(9, 3, 12, 15); // (135, 72, 153)
println!("{:?}", np.is_primitive()); // true
assert!(np.gcd() == 3);
Sourcepub fn gcd(&self) -> Integer
pub fn gcd(&self) -> Integer
Returns the GCD; 1 if primitive, else a positive integer.
Usage:
// Same example as `is_primitive(&self)`
use fibbox::FibBox;
let fb = FibBox::new(1, 1, 2, 3); // (3, 4, 5)
println!("{:?}", fb.is_primitive()); // true
assert!(fb.gcd() == 1);
let np = FibBox::new(9, 3, 12, 15); // (135, 72, 153)
println!("{:?}", np.is_primitive()); // false
assert!(np.gcd() == 3);
Sourcepub fn Root() -> FibBox
pub fn Root() -> FibBox
Returns the default base or root
use fibbox::FibBox;
let a = FibBox::new(1, 1, 2, 3);
let b = FibBox::Root();
assert_eq!(a, b);
Sourcepub fn f1(&self) -> &Integer
pub fn f1(&self) -> &Integer
Returns the value of f1 (borrowed).
Usage:
use rug::Integer;
use fibbox::FibBox;
type Mpz = Integer;
let fb = FibBox::new(1, 1, 2, 3); // (3, 4, 5)
assert_eq!(Mpz::from(fb.f1()), 1);
Sourcepub fn dif(&self) -> &Integer
pub fn dif(&self) -> &Integer
Returns the value of dif (borrowed).
Usage:
use rug::Integer;
use fibbox::FibBox;
type Mpz = Integer;
let fb = FibBox::new(1, 1, 2, 3); // (3, 4, 5)
assert_eq!(Mpz::from(fb.dif()), 1);
Sourcepub fn ctr(&self) -> &Integer
pub fn ctr(&self) -> &Integer
Returns the value of ctr (borrowed).
Usage:
use rug::Integer;
use fibbox::FibBox;
type Mpz = Integer;
let fb = FibBox::new(1, 1, 2, 3); // (3, 4, 5)
assert_eq!(Mpz::from(fb.ctr()), 2);
Sourcepub fn f2(&self) -> &Integer
pub fn f2(&self) -> &Integer
Returns the value of f2 (borrowed).
Usage:
use rug::Integer;
use fibbox::FibBox;
type Mpz = Integer;
let fb = FibBox::new(1, 1, 2, 3); // (3, 4, 5)
assert_eq!(Mpz::from(fb.f2()), 3);
Sourcepub fn price_a(&mut self)
pub fn price_a(&mut self)
Mutates the struct following the Price (2008) A branch.
use fibbox::FibBox;
let mut fb = FibBox::new(1, 1, 2, 3);
println!("{} {:?}", fb, fb.as_pytup());
fb.price_a();
println!("{} {:?}", fb, fb.as_pytup());
fb.price_a();
println!("{} {:?}", fb, fb.as_pytup());
fb.price_a();
println!("{} {:?}", fb, fb.as_pytup());
fb.price_a();
println!("{} {:?}", fb, fb.as_pytup());
// Output:
// 1 1 2 3 (3, 4, 5)
// 1 2 3 5 (5, 12, 13)
// 1 4 5 9 (9, 40, 41)
// 1 8 9 17 (17, 144, 145)
// 1 16 17 33 (33, 544, 545)
Sourcepub fn price_b(&mut self)
pub fn price_b(&mut self)
Mutates the struct following the Price (2008) B branch.
use fibbox::FibBox;
let mut fb = FibBox::new(1, 1, 2, 3);
let pt = fb.as_pytup();
println!("{} {:?}", fb, pt);
fb.price_b();
println!("{} {:?}", fb, pt);
fb.price_b();
println!("{} {:?}", fb, pt);
fb.price_b();
println!("{} {:?}", fb, pt);
fb.price_b();
println!("{} {:?}", fb, pt);
// Output:
// 1 1 2 3 (3, 4, 5)
// 3 1 4 5 (15, 8, 17)
// 5 3 8 11 (55, 48, 73)
// 11 5 16 21 (231, 160, 281)
// 21 11 32 43 (903, 704, 1145)
Sourcepub fn price_c(&mut self)
pub fn price_c(&mut self)
Mutates the struct following the Price (2008) C branch.
use fibbox::FibBox;
let mut fb = FibBox::Root();
let mut pt = fb.as_pytup();
println!("{} {:?}", fb, pt);
fb.price_c(); pt = fb.as_pytup();
println!("{} {:?}", fb, pt);
fb.price_c(); pt = fb.as_pytup();
println!("{} {:?}", fb, pt);
fb.price_c(); pt = fb.as_pytup();
println!("{} {:?}", fb, pt);
fb.price_c(); pt = fb.as_pytup();
println!("{} {:?}", fb, pt);
// Output:
// 1 1 2 3 (3, 4, 5)
// 1 3 4 7 (7, 24, 25)
// 1 7 8 15 (15, 112, 113)
// 1 15 16 31 (31, 480, 481)
// 1 31 32 63 (63, 1984, 1985)
Sourcepub fn price_p(&mut self)
pub fn price_p(&mut self)
Mutates the struct following the Price (2008) tree, becoming the parent (tree) value.
use fibbox::FibBox;
let mut fb = FibBox::Root();
let mut pt = fb.as_pytup();
println!("{} {:?}", fb, pt);
fb.price_a(); pt = fb.as_pytup();
println!("{} {:?}", fb, pt);
fb.price_b(); pt = fb.as_pytup();
println!("{} {:?}", fb, pt);
fb.price_c(); pt = fb.as_pytup();
println!("{} {:?}", fb, pt);
fb.price_p(); pt = fb.as_pytup();
println!("{} {:?}", fb, pt);
fb.price_p(); pt = fb.as_pytup();
println!("{} {:?}", fb, pt);
fb.price_p(); pt = fb.as_pytup();
println!("{} {:?}", fb, pt);
assert!(fb == FibBox::Root());
// Output:
// 1 1 2 3 (3, 4, 5)
// 1 3 4 7 (5, 12, 13)
// 1 7 8 15 (35, 12, 37)
// 5 7 12 19 (95, 168, 193)
// 5 1 6 7 (35, 12, 37)
// 1 2 3 5 (5, 12, 13)
// 1 1 2 3 (3, 4, 5)
Sourcepub fn berggren_a(&mut self)
pub fn berggren_a(&mut self)
Mutates the struct following the B. Berggren (1934) A branch.
use fibbox::FibBox;
let mut fb = FibBox::new(1, 1, 2, 3);
let mut pt = fb.as_pytup();
println!("{} {:?}", fb, pt);
fb.berggren_a(); pt = fb.as_pytup();
println!("{} {:?}", fb, pt);
fb.berggren_a(); pt = fb.as_pytup();
println!("{} {:?}", fb, pt);
fb.berggren_a(); pt = fb.as_pytup();
println!("{} {:?}", fb, pt);
fb.berggren_a(); pt = fb.as_pytup();
println!("{} {:?}", fb, pt);
// Output:
// 1 1 2 3 (3, 4, 5)
// 3 1 4 5 (15, 8, 17)
// 5 1 6 7 (35, 12, 37)
// 7 1 8 9 (63, 16, 65)
// 9 1 10 11 (99, 20, 101)
Sourcepub fn berggren_b(&mut self)
pub fn berggren_b(&mut self)
Mutates the struct following the B. Berggren (1934) B branch.
use fibbox::FibBox;
let mut fb = FibBox::Root();
let pt = fb.as_pytup();
println!("{} {:?}", fb, fb.as_pytup());
fb.berggren_b();
println!("{} {:?}", fb, fb.as_pytup());
fb.berggren_b();
println!("{} {:?}", fb, fb.as_pytup());
fb.berggren_b();
println!("{} {:?}", fb, fb.as_pytup());
fb.berggren_b();
println!("{} {:?}", fb, fb.as_pytup());
// Output:
// 1 1 2 3 (3, 4, 5)
// 3 2 5 7 (21, 20, 29)
// 7 5 12 17 (119, 120, 169)
// 17 12 29 41 (697, 696, 985)
// 41 29 70 99 (4059, 4060, 5741)
Sourcepub fn berggren_c(&mut self)
pub fn berggren_c(&mut self)
Mutates the struct following the B. Berggren (1934) C branch.
use fibbox::FibBox;
let mut fb = FibBox::new(1, 1, 2, 3);
let pt = fb.as_pytup();
println!("{} {:?}", fb, pt);
fb.berggren_c();
println!("{} {:?}", fb, pt);
fb.berggren_c();
println!("{} {:?}", fb, pt);
fb.berggren_c();
println!("{} {:?}", fb, pt);
fb.berggren_c();
println!("{} {:?}", fb, pt);
// Output:
// 1 1 2 3 (3, 4, 5)
// 1 2 3 5 (5, 12, 13)
// 1 3 4 7 (7, 24, 25)
// 1 4 5 9 (9, 40, 41)
// 1 5 6 11 (11, 60, 61)
Sourcepub fn berggren_p(&mut self)
pub fn berggren_p(&mut self)
Mutates the struct following the B. Berggren (1934) tree, becoming the parent (tree) value.
use fibbox::FibBox;
let mut fb = FibBox::Root();
let mut pt = fb.as_pytup();
println!("{} {:?}", fb, pt);
fb.berggren_c(); pt = fb.as_pytup();
println!("{} {:?}", fb, pt);
fb.berggren_a(); pt = fb.as_pytup();
println!("{} {:?}", fb, pt);
fb.berggren_b(); pt = fb.as_pytup();
println!("{} {:?}", fb, pt);
fb.berggren_p(); pt = fb.as_pytup();
println!("{} {:?}", fb, pt);
fb.berggren_p(); pt = fb.as_pytup();
println!("{} {:?}", fb, pt);
fb.berggren_p(); pt = fb.as_pytup();
println!("{} {:?}", fb, pt);
assert!(fb == FibBox::Root());
// Output:
// 1 1 2 3 (3, 4, 5)
// 3 1 4 5 (15, 8, 17)
// 5 4 9 13 (65, 72, 97)
// 5 9 14 23 (115, 252, 277)
// 5 4 9 13 (65, 72, 97)
// 3 1 4 5 (15, 8, 17)
// 1 1 2 3 (3, 4, 5)