Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions contrib/isaac/oo_and_element_recursion/Chain.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
public class Chain {

public static void runIterations(int iterations, int times) {
for(int t = 0 ; t < times; t++) {
System.gc();
long start = System.nanoTime();
for(int i = 0; i < iterations ; i++) {
Soldier.countoffSoldiers(40,3);
}
long end = System.nanoTime();
System.out.println(((end - start) * 1.0)/ (iterations * 1000 ));
}
}

public static void main(String[] args) {
int ITER = 1000000;
System.out.println("Element Recursion Object Oriented");
System.out.println(Soldier.countoffSoldiers(40,3) );
runIterations(ITER,10);
}
}
15 changes: 15 additions & 0 deletions contrib/isaac/oo_and_element_recursion/Shootout.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
final class Shoutout {

private int k = 1, kth;

//Shoutout(int n) { kth = n; } // standard Josephus problem
Shoutout(int n) { kth = n; k = n; }

boolean wasKth(){
boolean wasKth = k == kth;
if (wasKth) k = 1; else k++;
return wasKth;
}
}


50 changes: 50 additions & 0 deletions contrib/isaac/oo_and_element_recursion/Soldier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
final class Soldier {

private int number = 0;
private Soldier previous, next;

Soldier(Soldier previousSoldier, int number)
{
this.number = number;

if (previousSoldier != null){

next = previousSoldier.next;
previousSoldier.next = this;

previous = previousSoldier;
next.previous = this;

} else {
next = this;
previous = this;
}
}


int countoff(Shoutout shout)
{
if (next != this){

if (shout.wasKth()) {
previous.next = next;
next.previous = previous;
}
return next.countoff(shout);

} else {
return number;
}
}


static int countoffSoldiers(int n, int kth){
Soldier soldier = null;
for (int i = 1; i <= n; i++) soldier = new Soldier(soldier,i);

soldier = soldier.next; // set to the first Soldier constructed

return soldier.countoff( new Shoutout(kth) );
}
}

87 changes: 87 additions & 0 deletions contrib/isaac/oo_and_element_recursion/josephus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
$ /usr/local/src/v8/d8 josephus.js
26
element recursion object oriented
3.266
3.264
3.283
3.286
3.286
3.286
3.285
3.286
3.285
3.284
*/

function Soldier(previousSoldier,number){
this.number = number

if (previousSoldier != null){

this.next = previousSoldier.next
previousSoldier.next = this

this.previous = previousSoldier
this.next.previous = this

} else {
this.next = this
this.previous = this
}
}


Soldier.prototype.countoff = function(shout){
if (this.next != this){

if (shout.wasKth()) {
this.previous.next = this.next
this.next.previous = this.previous
}
return this.next.countoff(shout)

} else {
return this.number
}
}


function countoffSoldiers(n,kth) {
var soldier = null
for (var i = 1; i <= n; i++) soldier = new Soldier(soldier,i)

soldier = soldier.next // set to the first Soldier constructed

return soldier.countoff( new Shoutout(kth) )
}



//function Shoutout(n) { this.kth = n; this.k = 1 } // standard Josephus problem
function Shoutout(n) { this.kth = n; this.k = n }

Shoutout.prototype.wasKth = function(){
var wasKth = this.k == this.kth
if (wasKth) this.k = 1; else this.k++;
return wasKth
}


//-----------------------------------------------------------------------------

function run_iterations(iterations, times) {
for (var t = 0 ; t < times ; t++) {
var start = new Date()
for(var i = 0 ; i < iterations ; i++) {
countoffSoldiers(40,3)
}
var end = new Date()
print ((end.getTime() - start.getTime()) * 1000 / ITERS)
}
}

print(countoffSoldiers(40,3))
ITERS = 1000000
print ("element recursion object oriented")
run_iterations(ITERS,10)
41 changes: 0 additions & 41 deletions combined/Chain.java → element-recursion/Chain.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,6 @@ private static int[] soldiers(int n)
return a;
}

public static int countoffSoldiersReduction(int n, int kth)
{
int[] s = soldiers(n);
int[] survivors = new int[n];
int[] swap;
int k = 0, count = n;

while (count > 1) {
int m = 0;

for (int i = 0 ; i < count; i++)
if (i != k) {
survivors[m++] = s[i];

} else {
k += kth;
}

k -= count; // wrap around

swap = s; s = survivors; survivors = swap;
count = m;
}

return s[0];
}
public static int countoffSoldiersRecursion(int n, int kth)
{
LinkedList<Integer> soldiers = new LinkedList<Integer>();
Expand Down Expand Up @@ -75,18 +49,6 @@ private static int countoff(
}
}

public static void runIterationsReduction(int iterations, int times) {
for(int t = 0 ; t < times; t++) {
System.gc();
long start = System.nanoTime();
for(int i = 0; i < iterations ; i++) {
countoffSoldiersReduction(40,3);
}
long end = System.nanoTime();
System.out.println(((end - start) * 1.0)/ (iterations * 1000 ));
}
}

public static void runIterationsRecursive(int iterations, int times) {
for(int t = 0 ; t < times; t++) {
System.gc();
Expand All @@ -101,9 +63,6 @@ public static void runIterationsRecursive(int iterations, int times) {

public static void main(String[] args) {
int ITER = 1000000;
System.out.println("List Reduction");
System.out.println(countoffSoldiersReduction(40,3));
runIterationsReduction(ITER,10);
System.out.println("Element Recursion");
System.out.println(countoffSoldiersRecursion(40,3) );
runIterationsRecursive(ITER,10);
Expand Down
37 changes: 23 additions & 14 deletions element-recursion/josephus.clj
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
(defn vec-range [start end]
(loop [start start r (transient [])]
(if (= start end)
(persistent! r)
(recur (inc start) (conj! r start)))))
(set! *warn-on-reflection* true)
(set! *unchecked-math* true)

(defn shout [people n counter]
(cond
(= (count people) 1) (nth people 0)
(zero? counter) (recur (subvec people 1) n (inc counter))
:else (let [counter (if (= counter (dec n)) 0 (inc counter))]
(recur (conj (subvec people 1) (nth people 0)) n counter))))

(defn josephus [people nth]
(shout (vec-range 1 (inc people)) nth 0))
;; N.B. referentially transparent and threadsafe even with internal
;; use of mutation.
(defn josephus
[people nth]
(let [shout
(fn [^java.util.LinkedList people ^long n ^long counter]
(cond
(= (.size people) 1) (.get people 0)
(zero? counter) (do
(.remove people 0)
(recur people n (inc counter)))
:else (let [counter (if (= counter (dec n))
0 (inc counter))
f (.get people 0)]
(.remove people 0)
(.add people f)
(recur people n counter))))
al (java.util.LinkedList.)]
(dotimes [x people]
(.add al (inc x)))
(shout al nth 0)))

(defn run-iterations [iterations times]
(dotimes [_ times]
Expand Down
73 changes: 42 additions & 31 deletions element-recursion/josephus.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,61 @@
function array_init(size) {
people = []
for (var i = 0; i < size; i++) {
people[i] = i + 1;
}
/*
$ /usr/local/src/v8/d8 josephus.js
26
element recursion
7.496
7.517
7.522
7.527
7.526
7.524
7.526
7.527
7.524
7.526
*/

return people
}

function shout(people, nth, counter) {
if (people.length == 1) {
return people[0]
} else {
if (counter == 0) {
people.shift()
counter = counter + 1
} else {
if (counter == nth - 1) {
counter = 0
} else {
counter = counter + 1
}
val = people.shift()
people.push(val)
}
return shout(people,nth,counter)
}

function countoff(soldiers, kth, k) {
var i = soldiers.shift()

if (soldiers.length > 0){
if (k != kth){
k++
soldiers.push(i)
} else {
k = 1
}
return countoff(soldiers, kth, k)

} else {
return i
}
}

function josephus(size, nth) {
people = array_init(size)
return shout(people, nth, 0);

function countoffSoldiers(n, kth) {
var soldiers = []
for (var i = 0; i < n; i++) soldiers[i] = i+1

//return countoff(soldiers, kth, 1); // standard Josephus problem
return countoff(soldiers, kth, kth)
}


//-----------------------------------------------------------------------------

function run_iterations(iterations, times) {
for (var t = 0 ; t < times ; t++) {
var start = new Date()
for(var i = 0 ; i < iterations ; i++) {
josephus(40,3)
countoffSoldiers(40,3)
}
var end = new Date()
print ((end.getTime() - start.getTime()) * 1000 / ITERS)
}
}

print(josephus(40,3))
print(countoffSoldiers(40,3))
ITERS = 1000000
print ("element recursion")
run_iterations(ITERS,10)
Loading