I just solved this coding challenge, but I don't understand why my solution works 🤔

Kelvin Wangonya - Sep 3 '19 - - Dev Community

I've been trying to do one programming challenge a day on Kattis, and I just solved this one. I really had no idea how to solve it at first, so I just played around with the sample input/output data provided and noticed a pattern:

sample data

In the final sample, an input of 10 and 10 gives an output of 91: that's 10 * (10-1) + 1). Taking the first input to be x and the second to be y, this gives a formula of x * (y-1) + 1 which gives the correct output for all the other inputs and passes all test cases:

# https://open.kattis.com/problems/faktor
import sys


def faktor(articles, impact):
    print(int(articles)*(int(impact)-1) + 1)


if __name__ == '__main__':
    a, i = sys.stdin.readline().split()
    faktor(a, i)
Enter fullscreen mode Exit fullscreen mode

test cases

The thing is, that formula doesn't seem to have anything to do with the question in the challenge. Maybe I'm missing something 🤔

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .