CCC '08 S2 - Pennies in the Ring

View as PDF

Submit solution

Points: 5
Time limit: 1.0s
Memory limit: 256M

Problem type
Canadian Computing Competition: 2008 Stage 1, Senior #2

The game "Pennies in the Ring" is often played by bored computer programmers who have gotten tired of playing solitaire. The objective is to see how many pennies can be put into a circle. The circle is drawn on a grid, with its center at the coordinate (0, 0). A single penny is placed on every integer grid coordinate (e.g., (1, 1), (1, 2), etc.) that lies within or on the circle. It's not a very exciting game, but it's very good for wasting time. Your goal is to calculate how many pennies are needed for a circle with a given radius.


The input is a sequence of positive integer values, one per line, where each integer is the radius of a circle. You can assume the radius will be less than or equal to 25\,000. The last integer will be indicated by 0. You may assume that the grid is large enough for two pennies to be on adjacent integer coordinates and not touch.


You are to output, each on its own line, the number of pennies needed for each circle. You do not need to output 0 for the last 0. You may assume that the number of possible pennies is less than 2 billion (which is only $20 million dollars: computer scientists have lots of money).

Sample Input


Output for Sample Input



  • 1
    gavin_chen  commented on Nov. 4, 2022, 12:34 a.m.

    can someone check my code i know itr works but its too slow at the last test case please and thank you :D

  • 8
    OneYearOld  commented on Nov. 22, 2020, 1:31 p.m.

    FYI, creating a square that fully encompasses the circle (like such):

    enter image description here

    and checking every point in that square to see whether it lands in the circle will probably TLE. (You have to check up to 2.5 billion points).

  • -1
    HARRIBO  commented on Aug. 23, 2019, 4:24 a.m.

    Can someone tell me what's wrong with my program

    • 4
      magicalsoup  commented on Aug. 23, 2019, 2:33 p.m.

      Try this case


      The correct output should be


      • -8
        HARRIBO  commented on Aug. 23, 2019, 10:24 p.m.

        This comment is hidden due to too much negative feedback. Show it anyway.

        • 19
          Kirito  commented on Aug. 23, 2019, 11:20 p.m. edit 3

          A grid

          Observe that there are 5 lattice points within the circle.

  • 1
    dawangk  commented on June 3, 2018, 2:04 a.m.

    TLE for last two cases

    • 16
      DarthVader336  commented on June 12, 2018, 11:12 p.m.

      This can be done in O(n) time.