์ผ | ์ | ํ | ์ | ๋ชฉ | ๊ธ | ํ |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- ์๋ฐ ๊ฐ๋
- ์ค๋ผํดDB
- OS
- C
- Python challenge
- Rust
- ํ์ด์ฌ ์ฒผ๋ฆฐ์ง
- Database
- ์๋ฐ
- ํ์ด์ฌ ์ฑ๋ฆฐ์ง
- ubuntu
- ์๋ฐ ๊ธฐ์ด
- ํ์ด์ฌ
- ์ด์์ฒด์
- Python
- ๋ฐ์ดํฐ ํต์
- ๋ฌ์คํธ ์์
- java
- Operating System
- ๋ฐ์ดํฐ๋ฒ ์ด์ค
- ๋ฐฑ์ค
- ์๊ณ ๋ฆฌ์ฆ
- ๋ฌ์คํธ ํ๋ก๊ทธ๋๋ฐ ๊ณต์ ๊ฐ์ด๋
- ๋ฌ์คํธ
- ๋ฐฑ์ค ๋ฌ์คํธ
- ์ฐ๋ถํฌ
- Reversing
- ํ์ด์ฌ ์๊ณ ๋ฆฌ์ฆ
- data communication
- ์ค๋ผํด
Archives
- Today
- Total
IT’s Portfolio
[Rust] Start Rust (Day 8) - Rust Example Script 2 [Nth in Fibonacci Sequence] ๋ณธ๋ฌธ
Development Study/Rust
[Rust] Start Rust (Day 8) - Rust Example Script 2 [Nth in Fibonacci Sequence]
f1r3_r41n 2023. 1. 18. 14:38728x90
๋ฐ์ํ
๐ฆ Rust Day 8
๐ณ๏ธ Rust Example Script 2 - Nth in Fibonacci Sequence
1๏ธโฃ Description
- ์กฐ๊ฑด
0<=n<=45
์ ๋ถํฉํ๋ ์ ์ n์ ์ ๋ ฅ๋ฐ์ n๋ฒ์งธ ํผ๋ณด๋์น ์์ด์ ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ ์์ฑ
2๏ธโฃ how it works
- ์กฐ๊ฑด
0<=n<=45
์ ๋ถํฉํ๋ ์ ์ n์ ์ ๋ ฅ๋ฐ๋๋ค. - n๋ฒ์งธ ํผ๋ณด๋์น ์์ด์ ์ถ๋ ฅํ๋ค.
3๏ธโฃ Code
use std::io;
fn simple_fibo(n: u32) -> u64 {
let mut a = 0;
let mut b = 1;
if n <= 1 {
return n.into();
}
for _ in 0..n-1 {
let tmp = a;
a = b;
b = tmp+b;
}
return b;
}
fn array_fibo(n: u32) -> usize {
let mut fibo_arr: [usize; 2] = [0, 1];
let n: usize = n.try_into().unwrap();
for i in fibo_arr.len()..n+1 {
fibo_arr[i%2] = fibo_arr[(n-1)%2] + fibo_arr[(n-2)%2];
}
return fibo_arr[n%2]
}
fn recursive_fibo(n: u32) -> u64 {
if n <= 1 {
return n.into();
} else {
return recursive_fibo(n-1) + recursive_fibo(n-2);
}
}
fn main() {
loop {
let mut number = String::new();
io::stdin().read_line(&mut number)
.expect("Unable to read input!");
let number: u32 = match number.trim().parse() {
Ok(num) => num,
Err(_) => {
println!("Error! You have to input number!");
continue
},
};
println!("Simple Fibonacci Sequence: {}", simple_fibo(number));
println!("Recursive Fibonacci Sequence: {}", recursive_fibo(number));
println!("Array Fibonacci Sequence: {}", array_fibo(number));
break;
}
}
4๏ธโฃ Result
- n: 15
15 // input Simple Fibonacci Sequence: 610 // output Recursive Fibonacci Sequence: 610 // output Array Fibonacci Sequence: 610 // output
- n: 44
44 // input Simple Fibonacci Sequence: 701408733 // output Recursive Fibonacci Sequence: 701408733 // output Array Fibonacci Sequence: 701408733 // output
5๏ธโฃ Reference
- Febonacci Sequence in Python
- https://it-neicebee.tistory.com/150
- ์ด ๊ธ์
์ฌ๊ท์ ๋์ ๊ณํ๋ฒ
๋ฐํ๋ ฌ ์ฐ์ฐ
์ ๋ํ Rust ๊ตฌํ์ ํ์์ Rust ์๋ จ๋ ๋ถ์กฑ์ผ๋ก ๊ตฌํํ์ง ๋ชปํ์. ์์ผ๋ก ๋ ๊ณต๋ถํด์ ํด๋น ๋ฐฉ๋ฒ์ Rust๋ก ๊ตฌํํด๋ณด๋ ๊ธ์ ์ ๋ก๋ํ ์์ ์.
- Rust official Document about
core::convert::Into
- Rust official Document about
core::convert::TryInto
728x90
๋ฐ์ํ
'Development Study > Rust' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Rust] Start Rust (Day 10) - Using Structs to Structure Related Data (4) | 2023.02.01 |
---|---|
[Rust] Start Rust (Day 9) - Understanding Ownership (3) | 2023.01.24 |
[Rust] Start Rust (Day 7) - Rust Example Script 1 [Temperature Converter] (0) | 2023.01.09 |
[Rust] String Type (String, str) (0) | 2023.01.05 |
[Rust] Start Rust (Day 6) - Common Programming Concepts [2] (2) | 2023.01.03 |
Comments