pub struct PrimeByte { /* private fields */ }Expand description
A “byte of primes”, a chunk of 8 bits corresponding to the 8 values in the (0..30) range that are not divisible by 2, 3, or 5. Those values are also called k-values.
To learn more, read the guide.
“k-values” are values k, such that (N % 30 = k) and N is coprime with 30. Those values are listed here.
Implementations§
Source§impl PrimeByte
impl PrimeByte
Sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new byte, setting all k-values as prime
§Examples
use prime_data::PrimeByte;
let byte = PrimeByte::new();
assert_eq!(u8::from(byte), 0b11111111);Sourcepub fn set_nonprime(&mut self, k_value: u8) -> Result<bool, ()>
pub fn set_nonprime(&mut self, k_value: u8) -> Result<bool, ()>
Sets one of the bits to non-prime/composite based on the k-value
If the bit was already set to non-prime, returns false. Otherwise, returns true.
Returns an OutOfBounds error if the given value is not a k-value, returns an error.
To understand what k-value maps to what bit, refer to the k-values
§Examples
use prime_data::PrimeByte;
let mut byte = PrimeByte::from(0b10110111);
// k-value 1 corresponds to the first bit
// returns Ok(true) because the first bit was indeed, a one
assert!(byte.set_nonprime(1).unwrap());
assert_eq!(byte.as_u8(), 0b00110111);
// now, if we try to set it to non-prime again, it'll return false,
// because it's already a zero
assert!(!byte.set_nonprime(1).unwrap());
// when given a non-k-value, returns an error
assert!(byte.set_nonprime(4).is_err());Sourcepub fn as_boolean_array(&self) -> [bool; 8]
pub fn as_boolean_array(&self) -> [bool; 8]
Converts the bits into boolean entries
§Examples
use prime_data::PrimeByte;
let byte = PrimeByte::from(0b10100110);
assert_eq!(
byte.as_boolean_array(),
[true, false, true, false, false, true, true, false]
);Sourcepub fn as_k_values(&self) -> Vec<u8> ⓘ
pub fn as_k_values(&self) -> Vec<u8> ⓘ
Retrieves all bits set to one and converts them into their respective k-values
For more information, read the guide, or refer to the list of k-values.
If you wish to retrieve k-values within a specific range, see PrimeByte::as_k_values_in_range.
§Examples
use prime_data::PrimeByte;
let byte = PrimeByte::from(0b10100110);
assert_eq!(
byte.as_k_values(),
vec![1, 11, 19, 23]
);Sourcepub fn as_k_values_in_range(&self, range: RangeInclusive<u8>) -> Vec<u8> ⓘ
pub fn as_k_values_in_range(&self, range: RangeInclusive<u8>) -> Vec<u8> ⓘ
Retrieves all bits set to one and converts them into their respective k-values, as long as they fall inside the inclusive range
For more information, read the guide, or refer to the list of k-values.
If you wish to retrieve all k-values, see PrimeByte::as_k_values.
§Examples
use prime_data::PrimeByte;
let byte = PrimeByte::from(0b10100110);
assert_eq!(
byte.as_k_values_in_range(2..=23),
vec![11, 19, 23]
);Sourcepub fn as_primes(&self, offset: u64) -> Vec<u64>
pub fn as_primes(&self, offset: u64) -> Vec<u64>
Retrieves the k-values and converts them to actual prime numbers.
For more information, read the guide.
Calling this function with offset = 1 yields the same results as PrimeByte::as_k_values,
except that it returns a vector of u64 instead of u8.
If you wish to only retrieve primes within a range, see PrimeByte::as_primes_in_range
§Examples
use prime_data::PrimeByte;
let byte = PrimeByte::from(0b10100110);
assert_eq!(
byte.as_primes(21),
vec![631, 641, 649, 653]
);Sourcepub fn as_primes_in_range(
&self,
offset: u64,
range: RangeInclusive<u8>,
) -> Vec<u64>
pub fn as_primes_in_range( &self, offset: u64, range: RangeInclusive<u8>, ) -> Vec<u64>
Retrieves the k-values, as long as they fall inside the inclusive range, and converts them to actual prime numbers
Calling this function with offset = 1 yields the same results as
PrimeByte::as_k_values_in_range, except that it returns a vector of u64 instead of u8.
If you wish to retrieve all primes, see PrimeByte::as_primes
§Examples
use prime_data::PrimeByte;
let byte = PrimeByte::from(0b10100110);
assert_eq!(
byte.as_primes_in_range(21, 2..=23),
vec![641, 649, 653]
);Sourcepub fn overwrite_at(&mut self, overwriting: Self, position: u8)
pub fn overwrite_at(&mut self, overwriting: Self, position: u8)
Overwrites the bits in this byte with the bits in the other byte, from a given position
The position should be a bit index in the range (0..=7), 0 meaning it’ll overwrite the entire byte with the given one.
This function is useful for joining two data structures of Prime Bytes, as most of the time the former’s end and the latter’s start intersect in the middle of a prime byte. That way, we overwrite the byte at the intersection.
§Panics
Panics if position > 7
§Examples
use prime_data::PrimeByte;
let mut original = PrimeByte::from(0b00000000);
let new_bits = PrimeByte::from(0b11111111);
original.overwrite_at(new_bits, 5);
assert_eq!(
u8::from(original),
0b00000111
);Sourcepub fn is_prime(&self, x: u8) -> bool
pub fn is_prime(&self, x: u8) -> bool
Vefifies if the given x is prime, based on the k-values
Warning: It will return false for 2, 3, and 5. So take care of those cases before you call this function.
Second Warning: Will return false for values above 29. So always make sure to apply modulo 30 when calling this function.
§Examples
use prime_data::PrimeByte;
let byte = PrimeByte::from(0b00101000);
assert!( byte.is_prime(11));
assert!(!byte.is_prime(13));
assert!( byte.is_prime(17));Sourcepub fn count_primes(&self) -> u64
pub fn count_primes(&self) -> u64
Counts the number of primes (a.k.a the number of ones) it has.
If you wish to count primes in a specific range, see PrimeByte::count_primes_in_range
§Examples
use prime_data::PrimeByte;
let byte = PrimeByte::from(0b11010111);
assert_eq!(byte.count_primes(), 6);Sourcepub fn count_primes_in_range(&self, range: RangeInclusive<u8>) -> u64
pub fn count_primes_in_range(&self, range: RangeInclusive<u8>) -> u64
Counts the number of primes it has as long as their k-values fall within the range
If you wish to count all primes, see PrimeByte::count_primes
§Examples
use prime_data::PrimeByte;
let byte = PrimeByte::from(0b11010111);
assert_eq!(byte.count_primes_in_range(0..=30), 6);
assert_eq!(byte.count_primes_in_range(2..=30), 5);
assert_eq!(byte.count_primes_in_range(8..=12), 0);Sourcepub fn as_u8(&self) -> u8
pub fn as_u8(&self) -> u8
Converts byte into a u8
This has the same effect as calling u8::from(). It’s meant to be an alternative way
of converting into a u8, having the byte be written on the left instead of right.
§Examples
use prime_data::PrimeByte;
let byte = PrimeByte::new();
assert_eq!(byte.as_u8(), u8::from(byte))Sourcepub fn matches_in_range(
&self,
other: PrimeByte,
range: RangeInclusive<u8>,
) -> bool
pub fn matches_in_range( &self, other: PrimeByte, range: RangeInclusive<u8>, ) -> bool
Compares two bytes to see if their bits match in the given k-value range
Range is expected to be within (0..=30), but it will not return an error or panic, if given anything above 30. If you pass it some range like (30..=199), since none of the bits fall in that range, none will be compared, and hence the function will trivially return true.
There is no Self::matches method (without a range restriction) because that’s the same as
verifying if the two bytes are equal. PrimeByte implements Eq.
§Examples
use prime_data::PrimeByte;
let byte = PrimeByte::from(0b10111010);
let other = PrimeByte::from(0b10110111);
assert!( byte.matches_in_range(other, 0..=14));
assert!( byte.matches_in_range(other, 23..=23));
assert!(!byte.matches_in_range(other, 16..=17));
assert!( byte.matches_in_range(other, 30..=255));