From c66608290ad6d99dc366ecb7246180807b532769 Mon Sep 17 00:00:00 2001 From: uchitsa Date: Wed, 2 Oct 2024 19:17:14 +0300 Subject: [PATCH 1/8] add binpow-matrix.rs --- rust/binpow-matrix.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 rust/binpow-matrix.rs diff --git a/rust/binpow-matrix.rs b/rust/binpow-matrix.rs new file mode 100644 index 00000000..91c59bed --- /dev/null +++ b/rust/binpow-matrix.rs @@ -0,0 +1,39 @@ +use std::array; + +type Matrix2x2 = [i32; 4]; + +const IDENTITY_MATRIX: Matrix2x2 = [1, 0, 0, 1]; + +fn mul(first: &Matrix2x2, second: &Matrix2x2) -> Matrix2x2 { + [ + first[0] * second[0] + first[1] * second[2], + first[0] * second[1] + first[1] * second[3], + first[2] * second[0] + first[3] * second[2], + first[2] * second[1] + first[3] * second[3], + ] +} + +fn binpow(a: &Matrix2x2, n: i32) -> Matrix2x2 { + if n == 0 { + IDENTITY_MATRIX + } else if n % 2 == 1 { + let temp = binpow(a, n - 1); + mul(&temp, a) + } else { + let b = binpow(a, n / 2); + mul(&b, &b) + } +} + +fn calc(n: i32) -> i32 { + let n = n + 1; + let factor = [0, 1, 1, 1]; + let multiplier = binpow(&factor, n); + let base = [0, 1, 0, 0]; + mul(&base, &multiplier)[0] +} + +fn main() { + let n = 10; + println!("Fibonacci number at position {}: {}", n, calc(n)); +} From 23ae3c2b1240d9396a320910c71cb8dd7b00eb25 Mon Sep 17 00:00:00 2001 From: uchitsa Date: Sat, 5 Oct 2024 02:49:26 +0300 Subject: [PATCH 2/8] add functions.rs --- rust/functions.rs | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 rust/functions.rs diff --git a/rust/functions.rs b/rust/functions.rs new file mode 100644 index 00000000..1b34ebbc --- /dev/null +++ b/rust/functions.rs @@ -0,0 +1,33 @@ +#[inline(never)] +fn less(a: i32, b: i32) -> bool { + a < b +} + +#[inline(never)] +fn sub(a: i32, b: i32) -> i32 { + a - b +} + +#[inline(never)] +fn add(a: i32, b: i32) -> i32 { + a + b +} + +#[inline(never)] +fn fibo(x: i32) -> i32 { + if less(x, 2) { + 1 + } else { + add(fibo(sub(x, 1)), fibo(sub(x, 2))) + } +} + +#[inline(never)] +fn calc(x: i32) -> i32 { + fibo(x) +} + +fn main() { + let x = 10; + println!("Fibonacci of {} is {}", x, calc(x)); +} From 25acf1f4ca0f0d0e584cbd0a0aa3e2802ae919c4 Mon Sep 17 00:00:00 2001 From: uchitsa Date: Sat, 5 Oct 2024 02:51:21 +0300 Subject: [PATCH 3/8] add copyright --- rust/functions.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/rust/functions.rs b/rust/functions.rs index 1b34ebbc..af2d67d8 100644 --- a/rust/functions.rs +++ b/rust/functions.rs @@ -1,3 +1,23 @@ +// Copyright (c) 2022-2024 Yegor Bugayenko +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + #[inline(never)] fn less(a: i32, b: i32) -> bool { a < b From c6ba78fae261f4cce6ca9a638a6f5b6c5cf22941 Mon Sep 17 00:00:00 2001 From: uchitsa Date: Sat, 5 Oct 2024 02:52:08 +0300 Subject: [PATCH 4/8] Delete rust/binpow-matrix.rs --- rust/binpow-matrix.rs | 39 --------------------------------------- 1 file changed, 39 deletions(-) delete mode 100644 rust/binpow-matrix.rs diff --git a/rust/binpow-matrix.rs b/rust/binpow-matrix.rs deleted file mode 100644 index 91c59bed..00000000 --- a/rust/binpow-matrix.rs +++ /dev/null @@ -1,39 +0,0 @@ -use std::array; - -type Matrix2x2 = [i32; 4]; - -const IDENTITY_MATRIX: Matrix2x2 = [1, 0, 0, 1]; - -fn mul(first: &Matrix2x2, second: &Matrix2x2) -> Matrix2x2 { - [ - first[0] * second[0] + first[1] * second[2], - first[0] * second[1] + first[1] * second[3], - first[2] * second[0] + first[3] * second[2], - first[2] * second[1] + first[3] * second[3], - ] -} - -fn binpow(a: &Matrix2x2, n: i32) -> Matrix2x2 { - if n == 0 { - IDENTITY_MATRIX - } else if n % 2 == 1 { - let temp = binpow(a, n - 1); - mul(&temp, a) - } else { - let b = binpow(a, n / 2); - mul(&b, &b) - } -} - -fn calc(n: i32) -> i32 { - let n = n + 1; - let factor = [0, 1, 1, 1]; - let multiplier = binpow(&factor, n); - let base = [0, 1, 0, 0]; - mul(&base, &multiplier)[0] -} - -fn main() { - let n = 10; - println!("Fibonacci number at position {}: {}", n, calc(n)); -} From bb508eb8f4d8b5b38bb4f99c2785710fba39a248 Mon Sep 17 00:00:00 2001 From: uchitsa Date: Mon, 7 Oct 2024 15:39:52 +0300 Subject: [PATCH 5/8] Update functions.rs From a4179157bdc37b3e564c5782f9e42bfdabf0b326 Mon Sep 17 00:00:00 2001 From: uchitsa Date: Wed, 16 Oct 2024 18:31:09 +0300 Subject: [PATCH 6/8] fix typo (cherry picked from commit 2961c45dbbd65b671aadc413a887d31084731db5) --- cpp/binpow-matrix.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/binpow-matrix.cpp b/cpp/binpow-matrix.cpp index 23ad19c6..bc4843e1 100644 --- a/cpp/binpow-matrix.cpp +++ b/cpp/binpow-matrix.cpp @@ -33,7 +33,7 @@ matrix2on2 mul(const matrix2on2 &first, const matrix2on2 &second) { const matrix2on2 IDENTITY_MATRIX = {1, 0, 0, 1}; // See https://e-maxx.ru/algo/binary_pow matrix2on2 binpow(const matrix2on2 &a, int n) { - matrix2on2 result{}; + matrix2on2 result; if (n == 0) { result = IDENTITY_MATRIX; } else if (n % 2 == 1) { From 5264f55498c7e6c04d2888959720379282c23949 Mon Sep 17 00:00:00 2001 From: uchitsa Date: Wed, 16 Oct 2024 18:53:20 +0300 Subject: [PATCH 7/8] add binpow-matrix.rs --- rust/binpow-matrix.rs | 59 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 rust/binpow-matrix.rs diff --git a/rust/binpow-matrix.rs b/rust/binpow-matrix.rs new file mode 100644 index 00000000..3d7b3cc4 --- /dev/null +++ b/rust/binpow-matrix.rs @@ -0,0 +1,59 @@ +// Copyright (c) 2022-2024 Yegor Bugayenko +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +// SOFTWARE. + +use std::array; + +type Matrix2x2 = [i32; 4]; + +const IDENTITY_MATRIX: Matrix2x2 = [1, 0, 0, 1]; + +fn mul(first: &Matrix2x2, second: &Matrix2x2) -> Matrix2x2 { + [ + first[0] * second[0] + first[1] * second[2], + first[0] * second[1] + first[1] * second[3], + first[2] * second[0] + first[3] * second[2], + first[2] * second[1] + first[3] * second[3], + ] +} + +fn binpow(a: &Matrix2x2, n: i32) -> Matrix2x2 { + if n == 0 { + IDENTITY_MATRIX + } else if n % 2 == 1 { + let temp = binpow(a, n - 1); + mul(&temp, a) + } else { + let b = binpow(a, n / 2); + mul(&b, &b) + } +} + +fn calc(n: i32) -> i32 { + let n = n + 1; + let factor = [0, 1, 1, 1]; + let multiplier = binpow(&factor, n); + let base = [0, 1, 0, 0]; + mul(&base, &multiplier)[0] +} + +fn main() { + let n = 10; + println!("Fibonacci number at position {}: {}", n, calc(n)); +} From 7c42c1a2cc10caab8a447bd9c53cf1a7e7c88571 Mon Sep 17 00:00:00 2001 From: uchitsa Date: Fri, 1 Nov 2024 13:48:53 +0300 Subject: [PATCH 8/8] remove unused import --- rust/binpow-matrix.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/rust/binpow-matrix.rs b/rust/binpow-matrix.rs index 3d7b3cc4..8cc6c7f3 100644 --- a/rust/binpow-matrix.rs +++ b/rust/binpow-matrix.rs @@ -18,8 +18,6 @@ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. -use std::array; - type Matrix2x2 = [i32; 4]; const IDENTITY_MATRIX: Matrix2x2 = [1, 0, 0, 1];