Last active
March 27, 2023 01:28
-
-
Save Raffaello/a74bd247a99fb2f3c87e63137ec9cc37 to your computer and use it in GitHub Desktop.
FizzFuzzPrimeQ3 test
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
// Online C++ compiler to run C++ program online | |
#include <iostream> | |
#include <string> | |
#include <vector> | |
using namespace std; | |
vector<string> sol(long n) | |
{ | |
const long n1 = n-1; | |
// lookup prime | |
bool* not_primes = new bool[n+3]; // safe guard | |
not_primes[1-1]=true; | |
not_primes[2-1]=false; | |
// init | |
for(long i = 3-1; i<n1; i+=2) not_primes[i]=false; | |
// 2 first, pair numbers | |
for(long i = 4-1; i<n1; i+=2) | |
not_primes[i]=true; | |
for(long i = 3-1; i<n1; i++) | |
{ | |
if(not_primes[i++]) | |
continue; | |
// cout << i << "\n"; | |
for(long j=2*i-1; j<n1; j+=i) | |
{ | |
not_primes[j]=true; | |
} | |
} | |
// for(long i = 0; i <n1; i++) | |
// { | |
// cout << i+1 << " " << not_primes[i] << "\n"; | |
// } | |
vector<string> res; | |
res.resize(n1); | |
// to avoid using j%3 or j%5 | |
long i3=3-1; | |
long i5 =5-1; | |
for(long j = 0; j<n1;j++) | |
{ | |
if(!not_primes[j]) | |
{ | |
res[j]="Prime"; | |
// update indices | |
if(j == i3) i3+=3; | |
if(j == i5) i5+=5; | |
continue; | |
} | |
if(j == i3) | |
{ | |
res[j]="Fizz"; | |
i3+=3; | |
} | |
if(j == i5) | |
{ | |
res[j]+="Fuzz"; | |
i5+=5; | |
continue; | |
} | |
if(res[j].size() == 0) | |
res[j] = to_string(j+1); | |
} | |
delete[] not_primes; | |
return res; | |
} | |
int main() { | |
// Write C++ code here | |
auto res = sol(121); | |
//cout << "h2\n"; | |
cout << "res size: " << res.size() <<"\n"; | |
for(const string v : res) | |
{ | |
cout << v << "\n"; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment