Skip to content

Commit

Permalink
Problem 204 - solved
Browse files Browse the repository at this point in the history
  • Loading branch information
dionyziz committed Aug 21, 2012
1 parent 5140d6f commit 1f45005
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
73 changes: 73 additions & 0 deletions problem-204/204.cpp
@@ -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;
}
50 changes: 50 additions & 0 deletions problem-204/eta.cpp
@@ -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 );
}
24 changes: 24 additions & 0 deletions problem-204/eta.h
@@ -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

0 comments on commit 1f45005

Please sign in to comment.