Advertisement
burjui

The Josephus Problem solution

Nov 12th, 2016
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 1.52 KB | None | 0 0
  1. import std.algorithm;
  2. import std.conv;
  3. import std.exception;
  4. import std.math;
  5. import std.range;
  6. import std.stdio;
  7. import std.typecons;
  8.  
  9. void main()
  10. {
  11.     enum count = 100;
  12.     enum verbose = false;
  13.     iota(1, count + 1)
  14.         .map!(i => tuple!("n", "naive", "formula")(i, i.solveNaively(verbose), i.solveViaFormula))
  15.         .each!((solution)
  16.         {
  17.             "n %s -> %s (naive), %s (formula) ".writefln(solution.n, solution.naive, solution.formula);
  18.             enforce(solution.naive == solution.formula, "solutions don't match");
  19.         });
  20. }
  21.  
  22. class Node
  23. {
  24.     uint n;
  25.     Node next;
  26.     this(uint n) { this.n = n; }
  27. }
  28.  
  29. uint solveNaively(uint n, bool verbose)
  30. {
  31.     auto ring = makeRing(n);
  32.     if (verbose)
  33.         "==> %s".writefln(ring.ringToString);
  34.     Node node = ring;
  35.     while (node.next != node)
  36.     {
  37.         if (verbose)
  38.             "%s -> %s".writefln(node.n, node.next.n);
  39.         node = node.next = node.next.next;
  40.     }
  41.     return node.n;
  42. }
  43.  
  44. uint solveViaFormula(uint n)
  45. {
  46.     return (n - pow(2, cast(uint) log2(n))) * 2 + 1;
  47. }
  48.  
  49. Node makeRing(uint count)
  50. {
  51.     enforce(count);
  52.     auto nodes = iota(count).map!(i => new Node(i + 1)).array;
  53.     nodes.each!((i, node) => node.next = (i < count - 1 ? nodes[i + 1] : nodes.front));
  54.     return nodes.front;
  55. }
  56.  
  57. string ringToString(Node ring)
  58. {
  59.     string s;
  60.     Node node = ring;
  61.     do
  62.     {
  63.         s ~= (s.empty ? "" : ", ") ~ node.n.to!string;
  64.         node = node.next;
  65.     } while (node != ring);
  66.     return s;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement