Skip to content

Commit b5d84aa

Browse files
committed
task3 - add baseline
1 parent 799b72c commit b5d84aa

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
### Taxicab numbers
2+
3+
# A `taxicab` number is an integer that can be expressed as the sum of two cubes of positive integers in two different ways: `a^3 + b^3 = c^3 + d^3`.
4+
# For example, `1729` is the smallest taxicab number: `9^3 + 10^3 = 1^3 + 12^3`.
5+
# Design an algorithm to find all taxicab numbers with `a`, `b`, `c`, and `d` less than `n`.
6+
7+
# - Version 1: Use time proportional to `(n^2)*log(n)` and space proportional to `n^2`.
8+
# - Version 2: Use time proportional to `(n^2)*log(n)` and space proportional to `n`.
9+
10+
def baseline(n)
11+
arr = []
12+
13+
1.upto(n) do |a|
14+
1.upto(n) do |b|
15+
1.upto(n) do |c|
16+
1.upto(n) do |d|
17+
next if [a, b, c, d].uniq.size < 4
18+
r1 = a**3 + b**3
19+
r2 = c**3 + d**3
20+
arr << r1 if r1 == r2
21+
end
22+
end
23+
end
24+
end
25+
26+
arr.sort.uniq
27+
end
28+
29+
p baseline(30)

0 commit comments

Comments
 (0)