| « Prev | Page 1 | Page 2 | Page 3 | Page 4 | Page 5 | Page 6 | Page 7 | Page 8 | Next » |
Re: Programming Praxis: Josephus' Circle
2009-07-29 09:12
•
by
Josephus
(unregistered)
|
|
Last
|
|
SML, indexing the men from 0:
fun j(n,k,i) = (if i=1 then k-1 else k+j(n-1,k,i-1)) mod n; fun J(n,k) = j(n,k,n); |
Re: Programming Praxis: Josephus' Circle
2009-07-29 09:18
•
by
Anon
(unregistered)
|
|
Josephus' Circle, AKA Eeny, meeny, miny, moe. The solution is to argue about how many words are in the rhyme and whether the person selected at the end is "it" or "not it".
|
|
$ cat jos.c
#include <stdio.h> #include <stdlib.h> /* f(n,k)=(f(n-1,k)+k) mod n f(1,k)=0 */ static int f (int n, int k) { if (n == 1) return 0; return (f (n-1, k) + k) % n; } int main (int argc, const char **argv) { int n, k; if (argc != 3) { fprintf (stderr, "Usage: %s <N> <K>\n", argv[0]); return -1; } n = atoi (argv[1]); k = atoi (argv[2]); fprintf (stdout, "With %d soldiers and killing every %d'th, the safe spot is p osition %d\n", n, k, f (n, k) + 1); return 0; } $ gcc -g -O2 -W -Wall -Wextra jos.c -o jos $ ./jos.exe 12 3 With 12 soldiers and killing every 3'th, the safe spot is position 10 $ ./jos.exe 40 3 With 40 soldiers and killing every 3'th, the safe spot is position 28 $ |
|
Ha, now we have a way to stop people from posting "Frist!!1".
Anyways, random solution here
ps: TRWTF is the forum software erroring out because I forgot the /code tag. |
Re: Programming Praxis: Josephus' Circle
2009-07-29 09:22
•
by
DaveK
|
Hah, I just rewrote history and doomed Josephus with an off-by-one error. There weren't 40 soldiers, there were 40 soldiers /with Josephus/.
|
|
This is pretty brain dead, but coffee hasn't done it's job yet:
function jcircle($men, $skip) |
Re: Programming Praxis: Josephus' Circle
2009-07-29 09:31
•
by
silent d
(unregistered)
|
|
I think this is more about being the guy who decides where the "top of the circle" is than anything else.
|
|
I doubt I could find the safe spot very quickly, but I've always been good at finding the G spot.
|
Re: Programming Praxis: Josephus' Circle
2009-07-29 09:40
•
by
steenbergh
|
Fat lotta good that'll do you in a cave full of suicidal men... |
Re: Programming Praxis: Josephus' Circle
2009-07-29 09:42
•
by
JonsJava
(unregistered)
|
|
An ugly script to handle this (via PHP)
I couldn't think of a better name for the function. |
Re: Programming Praxis: Josephus' Circle
2009-07-29 09:47
•
by
Alex Mont
(unregistered)
|
|
Consider the function J(n,k) which is the position of the surviving soldier relative to the current starting point of the count, where n is the number of remaining soldiers and k is the interval (3 in the example). (Note that counting 3 at a time actually means you're only skipping 2 at a time.) Note that "position" is counting only currently alive soldiers.
If n=1 then clearly we are done, and J(1,k) = 0 for any k. Now consider the case of n>1. We have something like this:
Where the caret marks where the count starts. Now at the next step (assuming again k=3)
So now suppose that J(n-1,k) = x. That means that the surviving soldier is X positions after the caret in the second diagram. But the caret in the second diagram is 3 positions after the caret in the first diagram, so the surviving soldier is X+3 positions after the caret in the first diagram. Of course you have to take the mod of that by n to account for wrap around. Thus our formula is (code in Python):
Note that because of the way the recursion is set up, at no point do you have to keep track of which positions of soldiers are dead already. And it is an O(n) algorithm. |
Re: Programming Praxis: Josephus' Circle
2009-07-29 09:50
•
by
Addison
(unregistered)
|
This is sexy. |
Re: Programming Praxis: Josephus' Circle
2009-07-29 09:54
•
by
Dave
(unregistered)
|
|
Re: Programming Praxis: Josephus' Circle
2009-07-29 09:56
•
by
jfruh
(unregistered)
|
|
If I'm remembering right, Josephus was actually one of the last two standing, upon which he said to the other guy, as they were surrounded by corpses, "Maybe surrender isn't so shameful after all?" Not sure if the algorithm for figuring out how to be one of the last two is significantly easier.
|
Re: Programming Praxis: Josephus' Circle
2009-07-29 09:56
•
by
Ben Forta
(unregistered)
|
|
A not as ugly as the PHP script in CF
<code> <cfparam name="url.soldiers" type="numeric" default="12"> <cfparam name="url.skip" type="numeric" default="3"> <cfset circle=""> <cfloop from=1 to=#url.soldiers# index=x> <cfset circle=listAppend(circle,x)> </cfloop> <cfset x=1> <cfloop condition="listLen(circle) gt 1"> <cfset x+=url.skip-1> <cfif x gt listLen(circle)> <cfset x=x mod listLen(circle)> </cfif> <cfoutput>Deleting man at position #listGetAt(circle,x)#<br/></cfoutput> <cfset circle=listDeleteAt(circle, x)> </cfloop> <cfoutput>The last name standing was in position #circle#<br/></cfoutput> </code> |
Re: Programming Praxis: Josephus' Circle
2009-07-29 09:57
•
by
QSR
(unregistered)
|
|
A little bit of scala:
def josephus(soldiers:Int, toSkip:Int):Int = { josephus(soldiers, toSkip, 0) } def josephus(soldiers:Int, toSkip:Int, first:Int):Int = { if(soldiers == 1) { 0 } else { val position = josephus(soldiers - 1, toSkip, (first + toSkip) % (soldiers - 1)); if(position < first) { position; } else { position + 1; } } } |
Re: Programming Praxis: Josephus' Circle
2009-07-29 09:57
•
by
Ben Forta
(unregistered)
|
|
Sorry messed up the code tag
|
|
ColdFusion
|
Re: Programming Praxis: Josephus' Circle
2009-07-29 09:59
•
by
Purpurowy Tentakyl
(unregistered)
|
|
a simple python solution:
|
Re: Programming Praxis: Josephus' Circle
2009-07-29 10:02
•
by
Kyle
(unregistered)
|
Beat me to it.
|
Re: Programming Praxis: Josephus' Circle
2009-07-29 10:03
•
by
pjt33
|
Virtually identical. In my solution, replace fun J(n,k) = j(n,k,n);with fun J2(n,k) = j(n,k,n-1); |
Re: Programming Praxis: Josephus' Circle
2009-07-29 10:03
•
by
Code Independent
(unregistered)
|
In that case, I'll find the P Spot. |
Re: Programming Praxis: Josephus' Circle
2009-07-29 10:05
•
by
null
(unregistered)
|
|
You stand as a first one to die and then invert the alive-dead state and you're the only one alive.
|
Re: Programming Praxis: Josephus' Circle
2009-07-29 10:05
•
by
Robert Kosten
(unregistered)
|
|
PHP:
function j($n, $k) {
|
Re: Programming Praxis: Josephus' Circle
2009-07-29 10:06
•
by
Code Dependent
|
Shouldn't God be static? |
|
C#:
private int Josephus(int soldiers, int everyOther) { List<int> alive = new List<int>(); for (int i = 1; i <= soldiers; i++) alive.Add(i); int ndx = 0; while (alive.Count > 1) alive.RemoveAt(ndx = (ndx + everyOther - 1) % alive.Count); return alive[0]; } |
|
Pipped at the post by the one and only Ben Forta. I'm half livid, half honoured :)
|
Re: Programming Praxis: Josephus' Circle
2009-07-29 10:15
•
by
Andreas Kromann
(unregistered)
|
|
Assuming that the start position is index 1.
He is safe as he is the only one in the game: f(1,k) = 1 In general you solve the n'th case by reducing the problem by 1 and solving, adding k afterwards: f(n,k) = f(n-1,k)+ k mod n+1 = f(n-2,k) + 2k mod n = f(1,k) + (n-1)k mod n = 1 + (n-1)k mod n So all you need to do is: public static int f(int n, int k) { return 1 + (((n-1) * k) % n); } |
Re: Programming Praxis: Josephus' Circle
2009-07-29 10:15
•
by
Tom Parker
(unregistered)
|
|
Crude Python method (which calculates lists of soldiers along the way):
|
Re: Programming Praxis: Josephus' Circle
2009-07-29 10:17
•
by
Kman
(unregistered)
|
|
c++ templates
template <unsigned int s, unsigned int k> struct safe { static const unsigned int result = (safe<s - 1, k>::result + k) % s; }; template <unsigned int k> struct safe<1, k> { static const unsigned int result = 0; }; unsigned int index = safe<12, 3>::result; it came out looking exactly like the python implementation |
Re: Programming Praxis: Josephus' Circle
2009-07-29 10:17
•
by
Ahox
(unregistered)
|
|
What happened to the locker problem comment? I can't find it anymore, and I had such a nice solution
|
Re: Programming Praxis: Josephus' Circle
2009-07-29 10:17
•
by
Andreas Kromann
(unregistered)
|
thats 'mod n' ofc and not 'mod n+1' :) |
Re: Programming Praxis: Josephus' Circle
2009-07-29 10:18
•
by
Satanicpuppy
|
Truly, there is nothing sexier than recursion. It's especially elegant for problems like this one. Unfortunately, it's a bitch to maintain, and a memory hog for larger functions. |
Re: Programming Praxis: Josephus' Circle
2009-07-29 10:20
•
by
David
(unregistered)
|
|
Javascript (0 starting index):
(function(n,s){
|
Re: Programming Praxis: Josephus' Circle
2009-07-29 10:20
•
by
Scope
(unregistered)
|
|
But then we wouldn't be able to mock God in a unit test!
|
Re: Programming Praxis: Josephus' Circle
2009-07-29 10:22
•
by
Scope
(unregistered)
|
But then we wouldn't be able to mock God in a unit test! |
|
I just wanted to say that the animations accompanying these are brilliant. :)
|
Re: Programming Praxis: Josephus' Circle
2009-07-29 10:24
•
by
horizon
(unregistered)
|
|
assuming that the starting position is 0:
|
Re: Programming Praxis: Josephus' Circle
2009-07-29 10:24
•
by
guns
(unregistered)
|
similarly (ruby1.9): j = lambda { |n,k| n == 1 ? 0 : (j.(n-1, k) + k) % n } |
Re: Programming Praxis: Josephus' Circle
2009-07-29 10:27
•
by
Ralfe Poisson
(unregistered)
|
|
Here is a solution in PHP:
$a = array(); $b = 0; for ($x = 0; $x < $argv[1]; $x++) { $a[] = $x + 1; } while (sizeof($a) > 1) { $c = ($b + $argv[2] - 1) % sizeof($a); $b = $c; unset($a[$c]); $a = array_values($a); } print $a[0]; |
package require Tcl 8.5 Sure I could have used a modulus, but I like the notion of representing the circle of soldiers literally. |
Re: Programming Praxis: Josephus' Circle
2009-07-29 10:29
•
by
Random Net Poster
(unregistered)
|
|
I prefer to avoid recursion if possible (and in C++ for no good reason):
#include <iostream> #include <cstdlib> using namespace std; /* f(n,k)=(f(n-1,k)+k) mod n f(1,k)=0 => a[n]=a[n-1]%n -> (...(((k%2 +k)%3 +k%4) +k%5... +k)%n */ int main (int argc, const char **argv) { int n, k; if (argc != 3) { cerr << "Usage: "<< argv[0] << "<N> <K>\n"; return -1; } n = atoi (argv[1]); k = atoi (argv[2]); int r = 0; for(int a=2; a<=n; a++) r= (r+k)%a; cout<< "With " <<n<<" soldiers and killing every "<< k <<"'th, the safe spot is position "<<r+1<<endl; return 0; } |
Re: Programming Praxis: Josephus' Circle
2009-07-29 10:29
•
by
DesGrieux
(unregistered)
|
|
In Java, using Lists:
Using the recursion:
|
Re: Programming Praxis: Josephus' Circle
2009-07-29 10:31
•
by
d3matt
|
yes... the God class is a singleton... |
|
Vba:
|
Re: Programming Praxis: Josephus' Circle
2009-07-29 10:34
•
by
Code Dependent
|
Be not misled, God is not mocked; for whatsoever a man codeth, this shall he also support. |
Re: Programming Praxis: Josephus' Circle
2009-07-29 10:36
•
by
aehiilrs
(unregistered)
|
And also, if your grandparents are there, what the word ending in "ger" is. Talk about embarrassing. |
|
99 characters of ruby on the wall...
def josephus(s,n)c=(0...s).to_a ;i=0;while c.size>1;c[i,1]=nil;i=i+n;i=i%c.size;end;return c[0];end; or with line breaks instead of ; def josephus(s,n)c=(0...s).to_a i=0 while c.size>1 c[i,1]=nil i=i+n i=i%c.size end return c[0]; end |
Re: Programming Praxis: Josephus' Circle
2009-07-29 10:39
•
by
Wongo
(unregistered)
|
|
Mmh, these solutions overlook one important point: "Josephus somehow managed to figure out — very quickly — where to stand with forty others".
Unless Ole Joe had a phenomenal stack-based brain, he simply couldn't recurse through the 860 iterations required for a 41 soldiers circle (starting at 2). So there must be some kind of shortcut (e.g. to know whether a number is divisible by 5, you don't have to actually divide it, just check whether the final digit is 5 or 0). Indeed, here is the list of safe spots according to the number of soldiers (assuming a skip of 3): Soldiers: 2, Last Spot: 2 Soldiers: 3, Last Spot: 2 Soldiers: 4, Last Spot: 1 Soldiers: 5, Last Spot: 4 Soldiers: 6, Last Spot: 1 Soldiers: 7, Last Spot: 4 Soldiers: 8, Last Spot: 7 Soldiers: 9, Last Spot: 1 Soldiers: 10, Last Spot: 4 Soldiers: 11, Last Spot: 7 Soldiers: 12, Last Spot: 10 Soldiers: 13, Last Spot: 13 Soldiers: 14, Last Spot: 2 Soldiers: 15, Last Spot: 5 Soldiers: 16, Last Spot: 8 Soldiers: 17, Last Spot: 11 Soldiers: 18, Last Spot: 14 Soldiers: 19, Last Spot: 17 Soldiers: 20, Last Spot: 20 Soldiers: 21, Last Spot: 2 Soldiers: 22, Last Spot: 5 Soldiers: 23, Last Spot: 8 Soldiers: 24, Last Spot: 11 Soldiers: 25, Last Spot: 14 Soldiers: 26, Last Spot: 17 Soldiers: 27, Last Spot: 20 Soldiers: 28, Last Spot: 23 Soldiers: 29, Last Spot: 26 Soldiers: 30, Last Spot: 29 Soldiers: 31, Last Spot: 1 Soldiers: 32, Last Spot: 4 Soldiers: 33, Last Spot: 7 Soldiers: 34, Last Spot: 10 Soldiers: 35, Last Spot: 13 Soldiers: 36, Last Spot: 16 Soldiers: 37, Last Spot: 19 Soldiers: 38, Last Spot: 22 Soldiers: 39, Last Spot: 25 Soldiers: 40, Last Spot: 28 Soldiers: 41, Last Spot: 31 The series on the right looks conspicuously susceptible to a shortcut. But I can't find it for the life of me... Anyone ? |
| « Prev | Page 1 | Page 2 | Page 3 | Page 4 | Page 5 | Page 6 | Page 7 | Page 8 | Next » |