Chronus Interview Question
Software Engineer / Developers#include<iostream>
using namespace std;
int getNthNumber(int number,int Number[]){
int five,seven,three,nextNumber;
five=seven=three=0;
Number[0]=1;
for(int i=1;i<number;i++){
nextNumber=min(min(Number[five]*5,Number[seven]*7),min(Number[five]*5,Number[three]*3));
if(nextNumber==Number[five]*5)
five++;
if(nextNumber==Number[seven]*7)
seven++;
if(nextNumber==Number[three]*3)
three++;
Number[i]=nextNumber;
}
return nextNumber;
}
int main(){
int number=6;int Number[150];
cout<<getNthNumber(number,Number);
return 0;
}
//Print out all prime numbers from 1 to n
private static void printOutAllPrimeNumbers(int n)
{
int i = 0, j = 0;
bool prime = true;
for (i = 1; i < n; i++)
{
prime = true;
for (j = 2; j <= i / 2; j++)
{
if (i % j == 0)
prime = false;
}
if (prime == true)
Console.WriteLine(i);
}
}
Kindly think before posting.
Its obviously wrong.
Only numbers of the form N= 3^p * 5 ^q * 7^r 0<=p,q,r are needed
Use 3 stacks - one each for powers of 3,5,7
The smallest is 3 * 5^0 * 7^0.Push/Pop an element to stack(s) which is next highest number from previous number.
Eg: k =1 => 3^1 * 5^0 * 7^0,
k = 2 => 3*1 * 5^1 * 7^0
k = 3 => 3^2 * 5^1 * 7 ^0
k = 4 => 3^1 * 5^2 * 7^0
so on.. you need to keep popping from a stack to find the next highest number.
considering the array as sorted in ascending order...one can check that whether the number has only 3 5 and 7 as factors...if yes then increment the count...when the count reaches k then print that number...
The code to check whether the number i s factor of 3 5 and 7 is--
//code to divide the number by max power of 3 or 5 or 7
int maxdivide(int n, int m)
{
if((n%m) == 0)
n = n/m;
return n;
}
//code to check whether the number is multiple of 3 5 and 7 only
int check(int n)
{
if(n>1)
n = maxdivide(n,3);
if(n>1)
n = maxdivide(n,5);
if(n>1)
n = maxdivide(n,7);
if(n==1)
return 1;
else return 0;
}
Is it not enough to check weather the array contains elements which are divisible by 3*5*7.
- appu October 09, 2011