Last active
August 29, 2015 14:20
-
-
Save qgp9/caa307eff78bfa5126c2 to your computer and use it in GitHub Desktop.
Prime Factor
This file contains hidden or 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
#!/bin/bash | |
TargetNumber=${1:-5712925402340856} | |
{ | |
echo "##### Target Number = $TargetNumber" | |
echo | |
echo -n "##### primeFactor.pl : perl ";perl -E'say $^V' | |
time ./primeFactor.pl $TargetNumber | |
echo | |
echo -n "##### primeFactor.js : nodejs ";node --version | |
time ./primeFactor.js $TargetNumber | |
echo | |
echo -n "##### primeFactor.cxx : clang-600.0.57 -O2" | |
g++ -O2 primeFactor.cxx >& /dev/null | |
time ./a.out $TargetNumber | |
} 2>&1 | tee result.txt | |
This file contains hidden or 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
#include <iostream> | |
#include <vector> | |
#include <cstdlib> | |
using namespace std; | |
int main( int argc, char* argv[] ) | |
{ | |
unsigned long value = 5712925402340856ul; | |
if( argc > 1 ) | |
value = atol( argv[1] ); | |
unsigned long denom = 2; | |
vector<unsigned long> primeFactors; | |
cout << "Target number = " << value << endl; | |
while( denom <= value ) | |
{ | |
if( value % denom == 0 ) | |
{ | |
primeFactors.push_back( denom ); | |
value /= denom; | |
}else{ | |
denom += denom>2 ? 2 : 1; | |
if( denom*denom > value ) | |
denom = value; | |
} | |
} | |
for( auto factor : primeFactors ) | |
cout<<factor<<" * "; | |
cout<<endl; | |
return 0; | |
} |
This file contains hidden or 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
#!/usr/bin/env node | |
var num = 5712925402340856; | |
if( process.argv.length > 2 ) { | |
num=process.argv[2]; | |
} | |
var loopStartNum = 2; | |
var condition = true; | |
var prime = []; | |
while(condition){ | |
if (num%loopStartNum == 0) { | |
num = num/loopStartNum; | |
prime.push(loopStartNum); | |
} else { | |
loopStartNum += loopStartNum>2 ? 2:1; | |
if(num == loopStartNum || loopStartNum*loopStartNum > num){ | |
prime.push(num); | |
condition = false; | |
} | |
} | |
} | |
console.log(num); | |
console.log(prime); |
This file contains hidden or 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
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
my $v = shift || 29387422028002; | |
my $i=2; | |
my @o = (); | |
while($i<=$v){ | |
if($v%$i==0){ | |
push @o,$i; | |
$v/=$i; | |
}else{ | |
$i += $i>2 ? 2 : 1; | |
$i=$v if $i*$i>$v; | |
} | |
} | |
print join " x ",@o; | |
print "\n"; |
This file contains hidden or 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
##### Target Number = 5712925402340856 | |
##### primeFactor.pl : perl v5.18.2 | |
2 x 2 x 2 x 3 x 3 x 3 x 3 x 3 x 3 x 7 x 241 x 24097 x 24097 | |
real 0m0.016s | |
user 0m0.009s | |
sys 0m0.004s | |
##### primeFactor.js : nodejs v0.12.2 | |
1 | |
[ 2, 2, 2, 3, 3, 3, 3, 3, 3, 7, 241, 24097, 24097, 1 ] | |
real 0m0.068s | |
user 0m0.053s | |
sys 0m0.014s | |
##### primeFactor.cxx : clang-600.0.57 -O2Target number = 5712925402340856 | |
2 * 2 * 2 * 3 * 3 * 3 * 3 * 3 * 3 * 7 * 241 * 24097 * 24097 * | |
real 0m0.004s | |
user 0m0.001s | |
sys 0m0.002s |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment