I am writing an algorithm which outputs all prime numbers in range `[3; n]`. Take a look at my code and I will explain the problem:

```
#include <iostream>
using namespace std;

int main()
{
    int n;

    cout << "Enter the value of n:" << endl;
    cin >> n;

    int k = (n - 2) / 2 + 1;

    bool* nums = new bool[k];
    for (int i = 0; i < k; i++) nums[i] = true; 

    for (int i = 1; i < k; i++)
    {
        int j = i;

        while (i + j + 2 * i * j <= k)
        {
            nums[i + j + 2 * i * j] = false;
            j++;
        }
    }

    for (int i = 1; i < k; i++)
    {
        if (nums[i])
        {
            cout << 2 * i + 1 << endl;
        }
    }

    delete[]nums;
    nums = NULL;

    system("pause>0");
}

```

For `n < 200` everything works well. But for `n >= 200` I get the error:

```
Debug Error!
<...>
HEAP CORRUPTION DETECTED: after Normal block (#764) at
0x008655A0.
CRT detected that the application wrote to memory after end of heap buffer.

```

How to fix it? I am sure that this problem is surrounding dynamic array `nums`.