From 52cc15b76cc94af6cc34110a836af290f8173f79 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 2 Nov 2021 19:14:51 +0200 Subject: Fixed solution for ex 8 with an old solution to a similar exercise --- Year 1/Programming introduction/Practice-02.10.21.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'Year 1/Programming introduction/Practice-02.10.21.cpp') 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() { -- cgit v1.3.1-26-gb76a