-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
709 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "largeint" | ||
version = "0.1.0" | ||
authors = ["Maximilian Kahn <[email protected]>"] | ||
license = "MIT" | ||
description = "A library that supports large integer arithmetic using LargeInt." | ||
readme = "README.md" | ||
documentation = "https://docs.rs/largeint" | ||
repository = "https://github.com/Starfunk/largeint" | ||
keywords = ["largeint", "large", "int","bigint","num"] | ||
categories = ["no-std"] | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
# largeint | ||
A library that implements the `LargeInt` type, used for working with arbitrarily large integers in Rust! | ||
|
||
## Getting Started | ||
First, add largeint to your dependencies: | ||
```toml | ||
[dependencies] | ||
largeint = "0.1.0" | ||
``` | ||
Next, add this to the root of your crate to bring the contents of largeint into the scope of your project: | ||
```rust | ||
extern crate largeint; | ||
|
||
use largeint::largeint::*; | ||
``` | ||
|
||
|
||
You can then easily create many instances of `LargeInt`: | ||
```rust | ||
let largeint1 = new(String::from("999999999999999999999"), Sign::Positive); | ||
let largeint2 = new(String::from("999999999999999999999"), Sign::Negative); | ||
let largeint3 = new(String::from("0"), Sign::Unsigned); | ||
``` | ||
An instance of `LargeInt` contains two fields, the scalar value of the integer stored as a `String` and the sign of the integer stored as the enum, `Sign`, which can be `Positive`, `Negative`, or `Unsigned` (note that `0` is the only integer that should be assigned `Unsigned`). | ||
|
||
Using `new()` to create an instance of `LargeInt` is highly recommmended as there are checks in place to ensure that the `LargeInt` will be created properly. | ||
|
||
Refer to the documentation for more details. | ||
|
||
## An Example | ||
|
||
```rust | ||
extern crate largeint; | ||
|
||
use largeint::largeint::*; | ||
|
||
fn main() { | ||
|
||
// Adding two LargeInts. | ||
let largeint1 = new(String::from("33901489213409093401849249010492000112"), Sign::Positive); | ||
let largeint2 = new(String::from("8294839402902010934029489031849310009324234230"), Sign::Negative); | ||
let largeint3 = largeint1.add(&largeint2); | ||
let largeint4 = new(String::from("8294839369000521720620395630000060998832234118"), Sign::Negative); | ||
assert_eq!(largeint3,largeint4); | ||
|
||
// Subtracting two LargeInts. | ||
let largeint1 = new(String::from("33901489213409093401849249010492000112"), Sign::Negative); | ||
let largeint2 = new(String::from("100320394280329423048093284093240234809833999"), Sign::Negative); | ||
let largeint3 = largeint1.subtract(&largeint2); | ||
let largeint4 = new(String::from("100320360378840209638999882243991224317833887"), Sign::Positive); | ||
assert_eq!(largeint3,largeint4); | ||
|
||
//The get_int() method returns the scalar value of the LargeInt as a String. | ||
println!("The value of largeint1 is: {}", largeint1.get_int()); | ||
|
||
//The get_sign() method returns the Sign of the LargeInt as a String. | ||
println!("The Sign of largeint1 is: {}", largeint1.get_sign()); | ||
} | ||
|
||
``` | ||
|
||
## License | ||
This project is licensed under the MIT License - see [LICENSE.md](https://github.com/Starfunk/largeint/blob/master/LICENSE) for more details. | ||
|
||
## Future Updates | ||
Integer multiplication and division for `LargeInt` is currently being developed and should be released sometime in the next few weeks in the `"0.2.0"` update. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
|
||
use largeint::*; | ||
|
||
pub fn addition(large1: &str, large2: &str) -> String { | ||
let large1 = to_vec(&large1); | ||
let large2 = to_vec(&large2); | ||
let large3: &Vec<u8>; | ||
let large4: &Vec<u8>; | ||
let mut sum = vec![]; | ||
let mut counter: u8 = 0; | ||
let mut input: u8; | ||
|
||
if large1.len() >= large2.len() { | ||
large3 = &large1; | ||
large4 = &large2; | ||
} else { | ||
large3 = &large2; | ||
large4 = &large1; | ||
} | ||
let len_1 = large3.len(); | ||
let len_2 = large4.len(); | ||
for i in 0..len_2 { | ||
input = large3[len_1 - i - 1] + large4[len_2 - i - 1] + counter; | ||
if input >= 10 { | ||
input = input - 10; | ||
sum.insert(0, input); | ||
counter = 1; | ||
} else { | ||
sum.insert(0, input); | ||
counter = 0; | ||
} | ||
} | ||
if len_1 > len_2 { | ||
for i in (0..len_1 - len_2).rev() { | ||
input = large3[i] + counter; | ||
|
||
if input == 10 { | ||
sum.insert(0, 0); | ||
counter = 1; | ||
} else { | ||
sum.insert(0, input); | ||
counter = 0; | ||
} | ||
} | ||
if counter == 1 { | ||
sum.insert(0, 1) | ||
} | ||
} | ||
if len_1 == len_2 && counter == 1 { | ||
sum.insert(0, 1); | ||
} | ||
let digits = vec_to_str(&sum); | ||
digits | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use largeint::*; | ||
|
||
pub fn difference(large1: &str, large2: &str) -> String { | ||
let mut large1 = to_vec(&large1); | ||
let large2 = to_vec(&large2); | ||
let mut output_vec: Vec<u8> = Vec::new(); | ||
let mut output_str = String::from(""); | ||
let len1 = large1.len(); | ||
let len2 = large2.len(); | ||
if len1 > len2 { | ||
let index = len1 - len2; | ||
for mut i in (0..len2).rev() { | ||
if large1[i + index] >= large2[i] { | ||
output_vec.insert(0, large1[i + index] - large2[i]) | ||
} else if large2[i] > large1[i + index] { | ||
let i_2 = i; | ||
let mut count = 0; | ||
while i >= 1 && large2[i] >= large1[i + index]{ | ||
i = i - 1; | ||
count = count + 1; | ||
} if i == 0 && large2[i] >= large1[i + index] { | ||
let mut loopback = 0; | ||
while large1[i + index - 1 - loopback] == 0 { | ||
count = count + 1; | ||
loopback = loopback + 1; | ||
} | ||
for _ in 0..count+1 { | ||
large1[i + index - 1 - loopback] = large1[i + index - 1 - loopback] - 1; | ||
large1[i + index - loopback] = large1[i + index - loopback] + 10; | ||
if i < count { | ||
i = i + 1; | ||
} | ||
} | ||
} else { | ||
for _ in 0..count { | ||
large1[i + index] = large1[i + index] - 1; | ||
large1[i + index + 1] = large1[i + index + 1] + 10; | ||
i = i + 1; | ||
} | ||
} | ||
i = i_2; | ||
output_vec.insert(0, large1[i + index] - large2[i]); | ||
} | ||
} | ||
for k in (0..index).rev() { | ||
output_vec.insert(0, large1[k]); | ||
} | ||
output_str = vec_to_str(&output_vec); | ||
output_str = String::from(output_str.trim_left_matches("0")); | ||
} else if &large1.len() == &large2.len() { | ||
for mut i in (0..len1).rev() { | ||
if large1[i] >= large2[i] { | ||
output_vec.insert(0, large1[i] - large2[i]) | ||
} else if large2[i] > large1[i] { | ||
let mut count = 0; | ||
while i >= 1 && large2[i] >= large1[i]{ | ||
i = i - 1; | ||
count = count + 1; | ||
} | ||
for _ in 0..count { | ||
large1[i] = large1[i] - 1; | ||
large1[i + 1] = large1[i + 1] + 10; | ||
i = i + 1; | ||
} | ||
output_vec.insert(0, large1[i] - large2[i]); | ||
} | ||
} | ||
output_str = vec_to_str(&output_vec); | ||
output_str = String::from(output_str.trim_left_matches("0")); | ||
} | ||
output_str | ||
} |
Oops, something went wrong.