Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Programming introduction / Practice-02.10.21.cpp
diff options
context:
space:
mode:
1 files changed, 11 insertions, 1 deletions
-rw-r--r--Year 1/Programming introduction/Practice-02.10.21.cpp12
diff --git a/Year 1/Programming introduction/Practice-02.10.21.cpp b/Year 1/Programming introduction/Practice-02.10.21.cpp
index 0c524f3..960e211 100644
--- a/Year 1/Programming introduction/Practice-02.10.21.cpp
+++ b/Year 1/Programming introduction/Practice-02.10.21.cpp
@@ -148,7 +148,17 @@ void ex7() {
// (f(a, b) == f(c, d)) ⇐⇒ (a == c & b == d)
int f(int a, int b) {
- return fac(a) + fac(b);
+ // This is some old code of mine: https://gitlab.com/Syndamia/algorithms/-/blob/master/C_C++/FindSpiralNumber.cpp
+ int n, v = max(abs(a), abs(b)), g = 8 * (v * (v + 1) / 2);
+
+ if (b == v || a == v) { // Top row or right column
+ n = g - a + b - 6 * v;
+ }
+ else { // Left column or bottom row
+ n = g + a - b - 2 * v;
+ }
+
+ return n;
}
void ex8() {