start c++ implementation
This commit is contained in:
parent
75509d217a
commit
4591ba1222
|
@ -1 +1 @@
|
||||||
*.class
|
*.class
|
||||||
|
|
60
README.md
60
README.md
|
@ -1,31 +1,31 @@
|
||||||
CS426 - Operating Systems
|
CS426 - Operating Systems
|
||||||
|
|
||||||
# Collatz assignment
|
# Collatz assignment
|
||||||
|
|
||||||
The Collatz conjecture is simple. It says that for every starting number
|
The Collatz conjecture is simple. It says that for every starting number
|
||||||
the sequence below always eventually gets to a 1.
|
the sequence below always eventually gets to a 1.
|
||||||
f(n) = n/2 if n is even
|
f(n) = n/2 if n is even
|
||||||
f(n+1) = 3n+1 if n is odd
|
f(n+1) = 3n+1 if n is odd
|
||||||
|
|
||||||
For example.
|
For example.
|
||||||
17 -> 52 -> 26 -> 13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
|
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.
|
### 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
|
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
|
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
|
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
|
number with a value in prev[], you can use that instead of continuing the
|
||||||
computation.
|
computation.
|
||||||
|
|
||||||
If you have multiple threads, you must use good locking.
|
If you have multiple threads, you must use good locking.
|
||||||
|
|
||||||
### Objectives
|
### Objectives
|
||||||
|
|
||||||
* Can compute the collatz sequence length for a given number.
|
* Can compute the collatz sequence length for a given number.
|
||||||
* Can find the longest sequence.
|
* Can find the longest sequence.
|
||||||
* Uses Java threads
|
* Uses Java threads
|
||||||
* Uses C++ Threads
|
* Uses C++ Threads
|
||||||
* Uses the array prev[] above.
|
* Uses the array prev[] above.
|
||||||
* Does better load balancing that simple partitioning.
|
* Does better load balancing that simple partitioning.
|
|
@ -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
|
Binary file not shown.
|
@ -1,12 +1,38 @@
|
||||||
#import <unistd.h>
|
// Ben Harris
|
||||||
|
|
||||||
|
#import <unistd.h>
|
||||||
|
#import <cstdlib>
|
||||||
int collatz(int start) {
|
#import <string>
|
||||||
int cnt = 0;
|
#import <iostream>
|
||||||
while(start != 1) {
|
#import "collatz.h"
|
||||||
cnt++;
|
using namespace std;
|
||||||
start = start % 2 == 0 ? start/2 : 3*start + 1;
|
|
||||||
}
|
int *prev = new int[1000000];
|
||||||
return cnt;
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -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
|
Binary file not shown.
|
@ -1,37 +1,39 @@
|
||||||
class Collatz {
|
// Ben Harris
|
||||||
|
|
||||||
int prev[] = new int[1000000];
|
class Collatz {
|
||||||
int max = 0;
|
|
||||||
int maxsteps = 0;
|
int prev[] = new int[1000000];
|
||||||
|
int max = 0;
|
||||||
public static void main(String[] args) {
|
int maxsteps = 0;
|
||||||
System.out.println("collatz calc");
|
|
||||||
|
public static void main(String[] args) {
|
||||||
Collatz c = new Collatz();
|
System.out.println("javaCollatz");
|
||||||
|
|
||||||
for(int i = 1; i <= 100000; i++){
|
Collatz c = new Collatz();
|
||||||
System.out.println(i + "\tnum of steps: " + c.collatz(i));
|
|
||||||
}
|
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.");
|
}
|
||||||
}
|
|
||||||
|
System.out.println("Longest path was at " + c.max + " with " + c.maxsteps + " steps.");
|
||||||
int collatz(int start) {
|
}
|
||||||
int cnt = 0;
|
|
||||||
int initval = start;
|
int collatz(int start) {
|
||||||
while(start != 1) {
|
int cnt = 0;
|
||||||
cnt++;
|
int initval = start;
|
||||||
if(start < 1000000 && prev[start] != 0) {
|
while(start != 1) {
|
||||||
start = prev[start];
|
cnt++;
|
||||||
continue;
|
if(start < 1000000 && prev[start] != 0) {
|
||||||
}
|
start = prev[start];
|
||||||
start = start % 2 == 0 ? start/2 : 3*start + 1;
|
continue;
|
||||||
}
|
}
|
||||||
prev[start] = cnt;
|
start = start % 2 == 0 ? start/2 : 3*start + 1;
|
||||||
if(cnt > max){
|
}
|
||||||
max = initval;
|
prev[start] = cnt;
|
||||||
maxsteps = cnt;
|
if(cnt > max){
|
||||||
}
|
max = initval;
|
||||||
return cnt;
|
maxsteps = cnt;
|
||||||
}
|
}
|
||||||
|
return cnt;
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue