-
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
1 parent
4d65f88
commit 6e82b8e
Showing
15 changed files
with
308 additions
and
51 deletions.
There are no files selected for viewing
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
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,81 @@ | ||
use std::ops::{Add,Mul}; | ||
|
||
use num_traits::{one,zero,One,Zero}; | ||
|
||
use crate::include::iter_cache::cache_iterator; | ||
|
||
pub fn fib<I>() -> impl Iterator<Item = I> { | ||
return cache_iterator(Fibonacci::<I>::new()); | ||
} | ||
|
||
pub fn fib_by_3<I>() -> impl Iterator<Item = I> { | ||
return cache_iterator(FibonacciBy3::<I>::new()); | ||
} | ||
|
||
pub struct Fibonacci<I> { | ||
a: I, | ||
b: I, | ||
} | ||
|
||
impl<I> Default for Fibonacci<I> where I: Zero + One { | ||
fn default() -> Self { | ||
return Fibonacci::<I>{ | ||
a: zero(), | ||
b: one(), | ||
}; | ||
} | ||
} | ||
|
||
impl<I> Fibonacci<I> where I: Zero + One { | ||
pub fn new() -> Self { | ||
return Default::default(); | ||
} | ||
} | ||
|
||
impl<I> Iterator for Fibonacci<I> where I: Zero + One + Add { | ||
type Item = I; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
let prior_a = a; | ||
let prior_b = b; | ||
self.b = self.a + self.b; | ||
self.a = prior_b; | ||
return Some(prior_a); | ||
} | ||
} | ||
|
||
pub struct FibonacciBy3<I> { | ||
a: I, | ||
b: I, | ||
} | ||
|
||
impl<I> Default for FibonacciBy3<I> where I: Zero + One { | ||
fn default() -> Self { | ||
let two = one() + one(); | ||
let four = two + two; | ||
return FibonacciBy3::<I>{ | ||
a: two, | ||
b: four + four, | ||
}; | ||
} | ||
} | ||
|
||
impl<I> FibonacciBy3<I> where I: Zero + One + Add { | ||
pub fn new() -> Self { | ||
return Default::default(); | ||
} | ||
} | ||
|
||
impl<I> Iterator for FibonacciBy3<I> where I: Zero + One + Add + Mul { | ||
type Item = I; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
let two = one() + one(); | ||
let four = two + two; | ||
let prior_a = a; | ||
let prior_b = b; | ||
self.b = four * self.a + self.b; | ||
self.a = prior_b; | ||
return Some(prior_a); | ||
} | ||
} |
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
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod iter_cache; | ||
pub mod factors; | ||
pub mod fibonacci; | ||
pub mod math; | ||
pub mod primes; | ||
pub mod problems; | ||
|
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
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
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
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
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
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
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
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,26 @@ | ||
/* | ||
Project Euler Problem 36 | ||
Rust implementing a `reverse_bits()` function made this much more pleasant than expected | ||
Problem: | ||
The decimal number, 585 = 10010010012 (binary), is palindromic in both bases. | ||
Find the sum of all numbers, less than one million, which are palindromic in | ||
base 10 and base 2. | ||
(Please note that the palindromic number, in either base, may not include | ||
leading zeros.) | ||
*/ | ||
use crate::include::utils::{is_palindrome,Answer}; | ||
|
||
pub fn p0036() -> Answer { | ||
let mut answer: u64 = 0; | ||
for x in 1..1000000 { | ||
if x == x.reverse_bits() && is_palindrome(x) { | ||
answer += x; | ||
} | ||
} | ||
return Answer::Int(answer.into()); | ||
} |
Oops, something went wrong.