C# – Algorithm – Bear And Prime, Interactive Problem in CodeForces – Part 2

In Part 1 here I have shown the code that gives full points in the interactive CodeForces.Com problem for Bear And Prime.csThe trick is that writing hard-coded values in a code is really far away from a good practice in programming and although in that case it was ok, because this was a competition, in general it is not well accepted.

Thus, I have decided to rewrite the problem a little, thus avoiding the hard-coded values. Thus, I have decided to change the problem into the following – read the maximal number for the values, which the bear can think of and start printing.

Thus, I have to manage to build the two lists by myself – the list of the prime numbers from [2 to the user’s input] and the list of squares of those prime number up to the input. The first one is actually exactly Sieve of  Eratosthenes and the second one has to do with squaring the results from the Sieve. Then we simply concatenate the two lists and we start asking, using them. As you see, this is the result for 200, the second list is with the squares, equal to 4,9,25,49,121,169.

ea

 

The code looks like this:

 

Here is the code in github.com, enjoy it! 😀

About

VBA Developer

Tagged with: , ,