Struct FibBox

Source
pub struct FibBox { /* private fields */ }
Expand description

The Fibonacci Box, or FibBox struct contains 4 members: f1, dif, ctr, f2

f1Mpz, IntegerLesser Factor f2-2*dif ctr-dif
difMpz, IntegerDifference of Squares |(f2-f1)/2| f2-ctr
ctrMpz, IntegerCentered value |(f2+f1)/2| f1+dif
f2Mpz, IntegerGreater 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

Source

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
Source

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
Source

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)
Source

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
Source

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)
Source

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)
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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);
Source

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)
Source

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)
Source

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)
Source

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)
Source

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)
Source

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)
Source

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)
Source

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)

Trait Implementations§

Source§

impl Clone for FibBox

Source§

fn clone(&self) -> FibBox

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FibBox

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for FibBox

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for FibBox

Source§

fn eq(&self, other: &FibBox) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl StructuralPartialEq for FibBox

Auto Trait Implementations§

§

impl Freeze for FibBox

§

impl RefUnwindSafe for FibBox

§

impl Send for FibBox

§

impl Sync for FibBox

§

impl Unpin for FibBox

§

impl UnwindSafe for FibBox

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.