Skip to content

Commit cd4f363

Browse files
committed
some ds exercises fib and gcd lcm
1 parent 9d5698b commit cd4f363

10 files changed

Lines changed: 198 additions & 0 deletions

File tree

comp/rust/Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

comp/rust/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "rust"
3+
version = "0.1.0"
4+
edition = "2024"
5+
6+
[dependencies]

comp/rust/src/main.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use std::{i32, io};
2+
3+
fn main() {
4+
let mut input = String::new();
5+
6+
io::stdin().read_line(&mut input).expect("Failed to read line");
7+
8+
let mut nums = input
9+
.trim()
10+
.split_whitespace()
11+
.map(|x| x.parse::<i32>().expect("Not a number"));
12+
13+
let n = nums.next().unwrap();
14+
let m = nums.next().unwrap();
15+
16+
17+
}

comp/test.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
alpha = ("a".."z")
2+
ref = {}
3+
4+
alpha.each_with_index { |k, v| ref[k] = v }
5+
str1 = gets.chomp.downcase
6+
str2 = gets.chomp.downcase
7+
8+
new_s1 = []
9+
new_s2 = []
10+
11+
str1.split("").each do |i|
12+
new_s1 << ref[i]
13+
end
14+
15+
str2.split("").each do |i|
16+
new_s2 << ref[i]
17+
end
18+
19+
equal = 0
20+
21+
new_s1.each_with_index do |val, i|
22+
if val == new_s2[i]
23+
equal = 1
24+
elsif (val < new_s2[i])
25+
equal = 0
26+
puts("-1")
27+
exit
28+
else
29+
equal = 0
30+
puts("1")
31+
exit
32+
end
33+
end
34+
35+
if equal == 1
36+
puts("0")
37+
end

cpp/DSnA/fibonacci.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int fib(int num) {
5+
if (num <= 1) {
6+
return num;
7+
} else {
8+
return fib(num - 1) + fib(num - 2);
9+
}
10+
}
11+
12+
int main() {
13+
int num;
14+
cin >> num;
15+
16+
cout << fib(num) << endl;
17+
18+
return 0;
19+
}

cpp/DSnA/fibonacci_last.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#include <iostream>
2+
#include <string>
3+
#include <vector>
4+
using namespace std;
5+
6+
vector<long long> cache;
7+
8+
long long fib(int num) {
9+
if (num == 0)
10+
return 0;
11+
if (num == 1)
12+
return 1;
13+
14+
int first = 0, second = 1, next = 0;
15+
for (int i = 2; i <= num; i++) {
16+
next = (first + second) % 10;
17+
first = second;
18+
second = next;
19+
}
20+
return second;
21+
}
22+
23+
void last(int num) {
24+
string num_str = to_string(num);
25+
char last_num = num_str.back();
26+
27+
cout << last_num << endl;
28+
}
29+
30+
int main() {
31+
int num;
32+
cin >> num;
33+
34+
last(fib(num));
35+
36+
return 0;
37+
}

cpp/DSnA/fibonacci_memo.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <iostream>
2+
#include <vector>
3+
using namespace std;
4+
5+
vector<long long> cache;
6+
7+
long long fib(int num) {
8+
if (num <= 1) return num;
9+
if (cache[num] != -1) return cache[num];
10+
11+
cache[num] = fib(num - 1) + fib(num - 2);
12+
return cache[num];
13+
}
14+
15+
int main() {
16+
int num;
17+
cin >> num;
18+
19+
cache.assign(num + 1, -1);
20+
21+
cout << fib(num) << endl;
22+
return 0;
23+
}

cpp/DSnA/gcd.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
// gcd(a, b) = gcd(b, a % b)
5+
6+
void gcd(int numA, int numB) {
7+
while (numB != 0) {
8+
int temp = numB;
9+
numB = numA % numB;
10+
numA = temp;
11+
}
12+
13+
cout << numA << endl;
14+
}
15+
16+
int main() {
17+
int numA, numB;
18+
cin >> numA >> numB;
19+
20+
gcd(numA, numB);
21+
22+
return 0;
23+
}

cpp/DSnA/lcm.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
// lcm = (a * b) / gcd(a, b)
5+
// to avoid overflow we divide first the multiply
6+
// lcm = (a / gcd(a, b)) * b
7+
8+
long long gcd(long long numA, long long numB) {
9+
while (numB != 0) {
10+
long long temp = numB;
11+
numB = numA % numB;
12+
numA = temp;
13+
}
14+
15+
return numA;
16+
}
17+
18+
long long lcm(long long numA, long long numB) {
19+
return (numA / gcd(numA, numB)) * numB;
20+
}
21+
22+
int main() {
23+
long long numA, numB;
24+
cin >> numA >> numB;
25+
26+
cout << lcm(numA, numB) << endl;
27+
28+
return 0;
29+
}

cpp/DSnA/max_pairwise

-51 KB
Binary file not shown.

0 commit comments

Comments
 (0)