# ~$ Dissecting the KnightCTF Programming challenges!

Posted on Jan. 30th, 2022.

#### Write-ups

#### Loop in a Loop (100 points)

This challenge was a simple code reversal challenge, written in C++.

I rarely do C++ and never when I could use something else, so the solution will be in Python.

But first, analysis:

- The code takes a flag as input.
- It then loops on all indices, and then initiates a second loop on all remaining indices.
- The code inverts the characters at positions
`j`

and`j+1`

.

To reverse this we do the exact same loop with reversed parameters (i: length -> 0, j: length -> i), and then print the original flag:

The flag is `KCTF{b451c_pr06r4mm1ng}`

#### Reverse the Answer (50 points)

This is a coding challenge aimed at the resolution of a mathematical problem.

The statement of the problem is the following:

- Let
`x = 1`

- Let
`calculation = (x*(x+1)) + (2 *(x + 1))`

- Let
`reversed_calc = reversed number of calculation`

[for example if`calculation = 123`

, reversed_calc will be`321`

] - If
`reversed_calc`

can be divided by`4`

without reminder then`answer = answer + reversed_calc`

- Repeat all the calculations until you have
`x = 543`

- The final answer will be the flag when
`x = 543`

- Flag Format:
`KCTF{answer_here}`

Example Flag :`KCTF{123}`

We perform the loop for x going from 1 to 543.

To facilitate the calculation, we write the following program, in Python:

This produces `12252696`

, so the flag is `KCTF{12252696}`

.

#### Square Sum (50 points)

Have you ever heard the term "The sum of two squares"?

It's like the following :

And so on. In the example of 16, if we add the square of 0 & 4 we get 16. So here we are getting two values 0 & 4. So that's the answer.

So write a program & find out the two values of 25000. Conditions are the following:

- Remove the duplicates
- Pick the third one

Flag Format: `KCTF{0,1}`

We can produce an algorithm that loops between both bounds (or more accurately the square root of both bounds). This can quite easily be written in Python:

This produces:

We can remove the duplicates:

The third result is 90, 130 so the flag is `KCTF{90,130}`

.