Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
147 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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include <cstdio> | ||
#include <vector> | ||
#include <cmath> | ||
|
||
using namespace std; | ||
|
||
typedef unsigned long long int llu; | ||
|
||
const llu N = 1000000000; | ||
const int MAX_PRIME = 100; | ||
|
||
int prime_cnt = 1; | ||
llu primes[ MAX_PRIME ]; | ||
|
||
void eratosthenes() { | ||
primes[ 0 ] = 2; | ||
|
||
for ( llu candidate = 3; candidate < MAX_PRIME; candidate += 2 ) { | ||
bool prime = true; | ||
for ( int witness = 0; | ||
witness < prime_cnt | ||
&& primes[ witness ] * primes[ witness ] <= candidate; | ||
++witness ) { | ||
if ( candidate % primes[ witness ] == 0 ) { | ||
prime = false; | ||
break; | ||
} | ||
} | ||
if ( prime ) { | ||
primes[ prime_cnt++ ] = candidate; | ||
} | ||
} | ||
} | ||
|
||
inline llu value( vector< llu > factors ) { | ||
llu ret = 1; | ||
|
||
for ( int i = 0; i < prime_cnt; ++i ) { | ||
ret *= pow( primes[ i ], factors[ i ] ); | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
inline bool increment( vector< llu > &factors ) { | ||
for ( int i = 0; i < prime_cnt; ++i ) { | ||
++factors[ i ]; | ||
if ( value( factors ) <= N ) { | ||
return true; | ||
} | ||
factors[ i ] = 0; | ||
} | ||
return false; | ||
} | ||
|
||
int main() { | ||
eratosthenes(); | ||
|
||
llu total = 0; | ||
vector< llu > factorization; | ||
|
||
for ( int i = 0; i < prime_cnt; ++i ) { | ||
factorization.push_back( 0 ); | ||
} | ||
|
||
while ( increment( factorization ) ) { | ||
++total; | ||
} | ||
|
||
printf( "%llu\n", total + 1 ); | ||
|
||
return 0; | ||
} |
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,50 @@ | ||
/* | ||
* Simple ETA library for project euler | ||
* MIT licensed | ||
* | ||
* Dionysis "dionyziz" Zindros <dionyziz@gmail.com> | ||
*/ | ||
#include "eta.h" | ||
#include <cstdio> | ||
#include <cassert> | ||
|
||
const float EPSILON = 0.0001; | ||
|
||
ETA::ETA() { | ||
reset(); | ||
} | ||
|
||
void ETA::reset() { | ||
time( &t1 ); | ||
} | ||
|
||
void ETA::get( float completed, int &h, int &m, int &s ) { | ||
time_t t2; | ||
time( &t2 ); | ||
|
||
assert( completed > 0 ); | ||
|
||
int diff = ( int )t2 - t1; | ||
|
||
s = ( int )( ( float )diff / completed - diff ); | ||
// printf( "Total time in seconds: %i\n", s ); | ||
|
||
m = s / 60; | ||
s %= 60; | ||
|
||
h = m / 60; | ||
m %= 60; | ||
} | ||
|
||
void ETA::print( float completed ) { | ||
int h, m, s; | ||
|
||
if ( completed < EPSILON ) { | ||
printf( "∞" ); | ||
} | ||
else { | ||
get( completed, h, m, s ); | ||
printf( "%d:%02d:%02d", h, m, s ); | ||
} | ||
fflush( stdout ); | ||
} |
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,24 @@ | ||
/* | ||
* Simple ETA library for project euler | ||
* MIT licensed | ||
* | ||
* Dionysis "dionyziz" Zindros <dionyziz@gmail.com> | ||
*/ | ||
#ifndef ETA_H | ||
#define ETA_H | ||
|
||
#include <sys/types.h> | ||
#include <time.h> | ||
|
||
class ETA { | ||
public: | ||
ETA(); | ||
void reset(); | ||
void get( float, int &, int &, int & ); | ||
void print( float ); | ||
|
||
private: | ||
time_t t1; | ||
}; | ||
|
||
#endif |