|
You're in the right area [not]. If it was 100, then 73 would survive but there were 1,000.
[clue]
For smaller cases:
Prisoners 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Survivor 1 1 3 1 3 5 7 1 3 5 7 9 11 13 15 1 3 5 7 9
There is a formula here...
[fixed]
speramus in juniperus
modified 3-Dec-13 14:23pm.
|
|
|
|
|
Are you sure your clue is right? For example, with 14 prisoners, how can #15 be the survivor?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Small mistyping, I fix...
speramus in juniperus
|
|
|
|
|
Brain's not working properly today - I can see the pattern, but I can't see the formula. Is the answer #977?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Well done!
For the number of prisoners P find N, the highest power of 2 less than or equal to P. The survivor S, is:
S = (P-N) * 2 + 1
speramus in juniperus
|
|
|
|
|
Ah! That's what I did. I was expecting a more mathematical formula than that.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
This is nasty and sloppy, but it figures it out to be 505. Where did I got wrong (other than write some horrific code )?
class Program
{
static List<int> captures = new List<int>();
static void Main(string[] args)
{
PopulateCaptures();
int survivor = FindLastOne();
Console.WriteLine("The last one is: {0}", survivor);
Console.Write("\n\nHit any key to continue...");
Console.ReadKey();
}
static void PopulateCaptures()
{
for (int i = 1; i <= 1000; i++)
captures.Add(i);
}
static int FindLastOne(int startingPosition = 0)
{
int retVal = -1;
List<int> dead = new List<int>();
if (captures.Count == 1)
{
retVal = captures[0];
return retVal;
}
if (startingPosition == 0)
startingPosition = 2;
if (startingPosition == 1)
captures.RemoveAt(0);
for (int i = 1; i <= captures.Count; i++)
{
if (i % 2 == 0)
{
dead.Add(captures[i - 1]);
}
}
foreach (int i in dead)
{
captures.Remove(i);
}
if (captures.Count % 2 != 0)
startingPosition = 1;
if (captures.Count == 1)
retVal = captures[0];
else
retVal = FindLastOne(startingPosition);
return retVal;
}
}
|
|
|
|
|
Try this:
int index = 1;
var prisoners = Enumerable.Range(1, 1000).ToList();
while (prisoners.Count != 1)
{
while (index < prisoners.Count)
{
prisoners.RemoveAt(index);
index++;
}
if (index == prisoners.Count + 1)
{
index = 1;
}
else
{
index = 0;
}
}
Console.WriteLine(prisoners[0]);
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Found the error in my ways
It now works.
static int FindLastManStanding(int startingPosition = 2)
{
int retVal = -1;
bool lastWasKilled = false;
List<int> dead = new List<int>();
if (startingPosition == 1)
captures.RemoveAt(0);
int max = captures.Max();
for (int i = 1; i <= captures.Count; i++)
{
if (i % 2 == 0)
{
if ( max == captures[i - 1])
lastWasKilled = true;
dead.Add(captures[i - 1]);
}
}
dead.ForEach(x => captures.Remove(x));
startingPosition = lastWasKilled ? 2 : 1;
if (captures.Count == 1)
retVal = captures[0];
else
retVal = FindLastManStanding(startingPosition);
return retVal;
}
|
|
|
|
|
Khaaaaaaaan!
The language is JavaScript. that of Mordor, which I will not utter here
I hold an A-7 computer expert classification, Commodore. I'm well acquainted with Dr. Daystrom's theories and discoveries. The basic design of all our ship's computers are JavaScript.
|
|
|
|
|
"...to the last I grapple with thee; from hell's heart I stab at thee; for hate's sake I spit my last breath at thee."
Don't think Ahab was too happy really...
|
|
|
|
|
OriginalGriff wrote:
"...to the last I grapple with thee; from hell's heart I stab at thee; for hate's sake I spit my last breath at thee." |
That quote always reminds me of the Simpsons!
|
|
|
|
|
Herman Melville for me, I'm afraid!
|
|
|
|
|
|
I'll see you and raise![^]
speramus in juniperus
|
|
|
|
|
Is this the shortest solution?
var switcher = true;
var range = Enumerable.Range(1,1000).ToList();
while( range.Count > 1)
range = range.Where(x => (switcher = !switcher) == false).ToList();
range.Dump();
|
|
|
|
|
Keeping up with today's trend, let me ask,
To those who are willing to do this task,
Given digits numbered one to nine,
Only positive and without a minus sign,
Not repeating a digit find a two number set,
The product of which none other can offset.
|
|
|
|
|
Not really clear on what "The product of which none other can offset." means, but here's my go.
I'm assuming it means won't result in a different number when multiplied by another. 3 and 7 gives us ten. One by Zero leaves us with 0.
3 + 7 --> 10
1 * 0 --> 0
|
|
|
|
|
I guess I did not do too well with the rhyme.
I just mean the largest product.
|
|
|
|
|
Perhaps not perfectly, but still pretty bloody impressive. (to an aussie )
Interesting question. Bookmarked for future fun.
Thanks.
|
|
|
|
|
Poetry + Maths = MehGerbil Logging Out
|
|
|
|
|
97531 * 8642
speramus in juniperus
|
|
|
|
|
|
... no, just sums.
speramus in juniperus
|
|
|
|
|
just sums for some, awesome for some other..
|
|
|
|