-
Notifications
You must be signed in to change notification settings - Fork 572
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from davepl/drag-race
Drag race
- Loading branch information
Showing
8 changed files
with
181 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
PrimeCrystal/primes | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
FROM haxe:4.2.1-alpine3.13 | ||
|
||
RUN apk update && apk add --no-cache g++ python3 musl-locales bash | ||
RUN ln -s /usr/include/locale.h /usr/include/xlocale.h | ||
|
||
WORKDIR /opt/app | ||
COPY . . | ||
|
||
RUN haxelib install hxcpp | ||
|
||
COPY compile_and_run_all.sh . | ||
RUN chmod a+x compile_and_run_all.sh | ||
|
||
ENTRYPOINT [ "/opt/app/compile_and_run_all.sh" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Haxe solution by [Taylor Robinson](https://github.com/tayiorrobinson) | ||
|
||
`[faithful]` | ||
|
||
An implementation of a Prime Sieve in Haxe. | ||
|
||
## What's Haxe? | ||
|
||
See: https://haxe.org/ | ||
|
||
Haxe is a programming language that while it does have it's own interpreter, can also be cross-compiled to other languages such as C++, Python, C# & more. | ||
|
||
## Run Instructions | ||
|
||
|
||
1. Make sure you have a working [Haxe](https://haxe.org/) installation. | ||
- Make sure you have setup HaxeLib by typing `haxelib setup` | ||
|
||
For simplicity, you can run `bash compile_and_run_all.sh` to complete all the steps with 1 command. | ||
|
||
### Haxe Interpreter | ||
|
||
2. `haxe interp.hxml` | ||
|
||
### Python | ||
|
||
2. `haxe python.hxml` | ||
3. `python3 bin/py.py` | ||
|
||
### C++ | ||
|
||
2. `haxe cpp.hxml` | ||
1. If you get a `Error: Library hxcpp is not installed`, run `haxelib install hxcpp` | ||
3. `./bin/cpp/Main` | ||
|
||
## Output | ||
|
||
My machine is a MacBook Pro 2020 (M1, 8GB RAM) running macOS Big Sur. | ||
|
||
``` | ||
TayIorRobinson_Haxe_C++;2052;10.0021259784698;1 | ||
TayIorRobinson_Haxe_HaxeEval;49;10.1027731895446777;1 | ||
TayIorRobinson_Haxe_Python;26;10.114451885223389;1 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/bin/bash | ||
|
||
haxelib install hxcpp | ||
|
||
|
||
haxe cpp.hxml | ||
./bin/cpp/Main | ||
haxe interp.hxml | ||
haxe python.hxml | ||
python3 bin/py.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--cpp bin/cpp | ||
--class-path src | ||
--main Main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--interp | ||
--class-path src | ||
--main Main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--python bin/py.py | ||
--class-path src | ||
--main Main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import sys.net.Host; | ||
import haxe.ValueException; | ||
import haxe.exceptions.ArgumentException; | ||
import haxe.ds.List; | ||
|
||
class PrimeSieve { | ||
var sieveSize:Int; | ||
var rawbits:Array<Bool>; | ||
var primeCounts:Map<Int,Int> = [ | ||
10 => 1, | ||
100 => 25, | ||
1000 => 168, | ||
10000 => 1229, | ||
100000 => 9592, | ||
1000000 => 78498, | ||
10000000 => 664579, | ||
100000000 => 5761455 | ||
]; | ||
|
||
public function new(limit:Int):Void { | ||
sieveSize = limit; | ||
rawbits = [for (i in 0...Std.int((limit+1)/2)) true]; | ||
} | ||
|
||
function ValidateResults():Bool { | ||
if (primeCounts[sieveSize] != null) return primeCounts[sieveSize] == countPrimes() | ||
else return false; | ||
} | ||
|
||
function GetBit(index:Int):Bool { | ||
if (index % 2 == 0) return false; | ||
else return rawbits[Std.int(index/2)]; | ||
} | ||
|
||
function clearBit(index:Int):Void { | ||
if (index % 2 == 0) throw new ArgumentException("index","If you're setting even bits, you're sub-optimal for some reason!"); | ||
else rawbits[Std.int(index/2)] = false; | ||
} | ||
|
||
public function runSieve():Void { | ||
var factor = 3; | ||
var q = Math.sqrt(sieveSize); | ||
|
||
while (factor < q) { | ||
for (num in factor...sieveSize) { | ||
if (GetBit(num)) { | ||
factor = num; | ||
break; | ||
} | ||
} | ||
|
||
var num = factor * 3; | ||
while (num < sieveSize) { | ||
num += factor * 2; | ||
clearBit(num); | ||
} | ||
factor += 2; | ||
} | ||
} | ||
|
||
function countPrimes():Int { | ||
return rawbits.filter((b) -> b).length; | ||
} | ||
|
||
public function printResults(showResults:Bool, duration:Float, passes:Int) { | ||
if (showResults) Sys.print("2, "); | ||
var count = 1; | ||
for (num in 3...sieveSize) { | ||
if (GetBit(num)) { | ||
if (showResults) Sys.print(Std.string(num) + ", "); | ||
count += 1; | ||
} | ||
} | ||
if (count != this.countPrimes()) throw new ValueException("Terrible error"); | ||
|
||
Sys.println("\nPasses: " + Std.string(passes) + ", Time " + Std.string(duration) + ", Avg: " + Std.string(duration/passes) + ", Limit: " + Std.string(this.sieveSize) + ", Count: " + Std.string(count) + ", Valid: " + Std.string(this.ValidateResults())); | ||
} | ||
} | ||
|
||
class Main { | ||
static public function main():Void { | ||
var tStart:Float = Sys.time(); | ||
var passes:Int = 0; | ||
var sieve:PrimeSieve = null; | ||
while (Sys.time() - tStart < 10) { | ||
sieve = new PrimeSieve(1000000); | ||
sieve.runSieve(); | ||
passes += 1; | ||
} | ||
var tD:Float = Sys.time() - tStart; | ||
//sieve.printResults(false,tD,passes); | ||
#if interp | ||
var language = 'HaxeEval'; | ||
#elseif cpp | ||
var language = "C++"; | ||
#elseif python | ||
var language = "Python"; | ||
#else | ||
var language = "UnknownTarget"; | ||
#end | ||
Sys.println("TayIorRobinson_Haxe_" + language + ";" + Std.string(passes) + ";" + Std.string(tD) + ";1"); | ||
} | ||
} |