Problem Statement
When trying to divide coins "equally" between two people, an old gag goes something like this...
- The person dividing up the loot says "One for you, and one for me" giving one coin to the other person and then themselves.
- Next they say "And that's two for you", while giving the other person a second coin.
- Then, they say "And that's one, two, for me", giving themselves two coins, additional to the first one they already had.
- Then, they say "That's three for you, and one, two, three, for me", given the other person a third coin before giving themselves three more coins.
- ... and so on
The are N coins to be divided. Assuming they are divided in this sneaky fashion, how many coins will the first person end up getting?
Definition
- Class:
- OneForYou
- Method:
- coinCount
- Parameters:
- long
- Returns:
- long
- Method signature:
- long coinCount(long N)
- (be sure your method is public)
Constraints
- N will be between 0 and 10^15, inclusive.
Examples
2
Returns: 1
In this case, it's actually fair, each person gets one coin.9
Returns: 6
This is three full rounds of dividing coins.11
Returns: 7
Here we run out of coins before the person dividing the coins takes four of them, so he takes as many as he can.33
Returns: 26