Created
April 1, 2016 12:15
Finding the nth largest number expressible by the given prime numbers.
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
include <vector> | |
#include <math.h> | |
using namespace std; | |
double find_nth_expressible(vector<int> primes, int n) | |
{ | |
vector<double> numbers; | |
list<double> queue; | |
double result; | |
queue.push_back(1); | |
numbers.push_back(1); | |
while(numbers.size() < 3*n) | |
{ | |
double cur = queue.front(); | |
queue.pop_front(); | |
for (int i = 0; i < primes.size(); i++) | |
{ | |
double r = (double)cur*primes[i]; | |
queue.push_back(r); | |
numbers.push_back(r); | |
} | |
} | |
sort(numbers.begin(), numbers.end(), compare); | |
int prev = 0; | |
int j = 0; | |
for(int i = 0; i < numbers.size(); i++) | |
{ | |
cout <<numbers[i] <<endl; | |
result = numbers[i]; | |
if (result != prev) j++; | |
prev = result; | |
if (j == n) | |
break; | |
} | |
return result; | |
} | |
int main() | |
{ | |
int n = 10; | |
vector<int> primes; | |
primes.push_back(2); | |
primes.push_back(3); | |
cout<<" 10th largest expressible : " << find_nth_expressible(primes, n)<<endl; | |
return 1; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment