start c++ implementation

This commit is contained in:
Ben Harris 2016-09-04 14:17:30 -04:00
parent 75509d217a
commit 4591ba1222
8 changed files with 134 additions and 79 deletions

2
.gitignore vendored
View File

@ -1 +1 @@
*.class
*.class

View File

@ -1,31 +1,31 @@
CS426 - Operating Systems
# Collatz assignment
The Collatz conjecture is simple. It says that for every starting number
the sequence below always eventually gets to a 1.
f(n) = n/2 if n is even
f(n+1) = 3n+1 if n is odd
For example.
17 -> 52 -> 26 -> 13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
### Your mission is to find the starting number that is less than 1,000,000 with the longest sequence.
This might take a long time. To make it faster, you might cache previous
answers. Suppose you have an array prev[]. Prev[n] is the length of the
sequence starting at n. If, when computing a sequence, you ever get to a
number with a value in prev[], you can use that instead of continuing the
computation.
If you have multiple threads, you must use good locking.
### Objectives
* Can compute the collatz sequence length for a given number.
* Can find the longest sequence.
* Uses Java threads
* Uses C++ Threads
* Uses the array prev[] above.
CS426 - Operating Systems
# Collatz assignment
The Collatz conjecture is simple. It says that for every starting number
the sequence below always eventually gets to a 1.
f(n) = n/2 if n is even
f(n+1) = 3n+1 if n is odd
For example.
17 -> 52 -> 26 -> 13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
### Your mission is to find the starting number that is less than 1,000,000 with the longest sequence.
This might take a long time. To make it faster, you might cache previous
answers. Suppose you have an array prev[]. Prev[n] is the length of the
sequence starting at n. If, when computing a sequence, you ever get to a
number with a value in prev[], you can use that instead of continuing the
computation.
If you have multiple threads, you must use good locking.
### Objectives
* Can compute the collatz sequence length for a given number.
* Can find the longest sequence.
* Uses Java threads
* Uses C++ Threads
* Uses the array prev[] above.
* Does better load balancing that simple partitioning.

17
cpp_src/Makefile Normal file
View File

@ -0,0 +1,17 @@
CSW = -O3 -Wall
LSW = -lfd
all:
make collatz
collatz: collatz.o Makefile
g++ collatz.o -o collatz
collatz.o: collatz.cc Makefile
g++ collatz.cc -c -o collatz.o $(CSW)
collatz.cc: collatz.h Makefile
touch collatz.cc
clean:
touch Makefile; make

BIN
cpp_src/collatz Normal file

Binary file not shown.

View File

@ -1,12 +1,38 @@
#import <unistd.h>
int collatz(int start) {
int cnt = 0;
while(start != 1) {
cnt++;
start = start % 2 == 0 ? start/2 : 3*start + 1;
}
return cnt;
}
// Ben Harris
#import <unistd.h>
#import <cstdlib>
#import <string>
#import <iostream>
#import "collatz.h"
using namespace std;
int *prev = new int[1000000];
int maxcnt = 0, maxsteps = 0;
int main (int argc, char **argv) {
cout << "cCollatz" << endl;
for (int i = 1; i <= 100000; i++) {
cout << i << "\tnum of steps: " << collatz(i) << endl;
}
cout << "Longest path was " << maxsteps << " steps for " << maxcnt << endl;
}
int collatz (int start) {
int cnt = 0;
int initval = start;
while(start != 1) {
cnt++;
if (start < 1000000 && prev[start] != 0) {
start = prev[start];
continue;
}
start = start % 2 == 0 ? start/2 : 3*start + 1;
}
prev[start] = cnt;
if (cnt > maxcnt) {
maxcnt = initval;
maxsteps = cnt;
}
return cnt;
}

10
cpp_src/collatz.h Normal file
View File

@ -0,0 +1,10 @@
// Ben Harris
#ifndef _COLLATZ_h_
#define _COLLATZ_h_
// method definitions
int main(int argc, char **argv);
int collatz(int start);
#endif

BIN
cpp_src/collatz.o Normal file

Binary file not shown.

View File

@ -1,37 +1,39 @@
class Collatz {
int prev[] = new int[1000000];
int max = 0;
int maxsteps = 0;
public static void main(String[] args) {
System.out.println("collatz calc");
Collatz c = new Collatz();
for(int i = 1; i <= 100000; i++){
System.out.println(i + "\tnum of steps: " + c.collatz(i));
}
System.out.println("Longest path was at " + c.max + " with " + c.maxsteps + " steps.");
}
int collatz(int start) {
int cnt = 0;
int initval = start;
while(start != 1) {
cnt++;
if(start < 1000000 && prev[start] != 0) {
start = prev[start];
continue;
}
start = start % 2 == 0 ? start/2 : 3*start + 1;
}
prev[start] = cnt;
if(cnt > max){
max = initval;
maxsteps = cnt;
}
return cnt;
}
// Ben Harris
class Collatz {
int prev[] = new int[1000000];
int max = 0;
int maxsteps = 0;
public static void main(String[] args) {
System.out.println("javaCollatz");
Collatz c = new Collatz();
for(int i = 1; i <= 100000; i++){
System.out.println(i + "\tnum of steps: " + c.collatz(i));
}
System.out.println("Longest path was at " + c.max + " with " + c.maxsteps + " steps.");
}
int collatz(int start) {
int cnt = 0;
int initval = start;
while(start != 1) {
cnt++;
if(start < 1000000 && prev[start] != 0) {
start = prev[start];
continue;
}
start = start % 2 == 0 ? start/2 : 3*start + 1;
}
prev[start] = cnt;
if(cnt > max){
max = initval;
maxsteps = cnt;
}
return cnt;
}
}