-
Notifications
You must be signed in to change notification settings - Fork 0
/
FizzBuzz.h
74 lines (64 loc) · 1.22 KB
/
FizzBuzz.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#ifndef FIZZBUZZ_H
#define FIZZBUZZ_H
#include <iostream>
#include <string>
#include <vector>
#include <map>
typedef std::map<int, std::string> IntStrDict;
void printString(std::string tString)
{
std::cout << tString << '\n';
}
std::string ifEmpty(int i, std::string tString)
{
if (tString.empty())
{
tString += std::to_string(i);
}
return tString;
}
std::string checkModZero(int i, IntStrDict tDict)
{
std::string tString;
for (auto const &j : tDict)
{
if (i % j.first == 0)
{
tString += j.second;
}
}
return tString;
}
std::vector<int> getFizzPrimes(int size) // getPrimes should also return '2', but we're only interested in 3 and higher so getFizzPrimes
{
std::vector<int> primes;
int j = 1; // Magic number.
for (int i = 0; i <= size; ++i)
{
bool foundPrime = false;
while (!foundPrime)
{
for (auto it = primes->begin(); it != primes->end(); it++)
{
if (*it > j / 2)
{
primes.push_back(it);
break;
}
if (j % *it == 0)
{
break;
}
}
j += 2; // Note this skips 2.
}
}
}
std::string FizzBuzzer(int i, const IntStrDict & tDict)
{
std::string print;
print = checkModZero(i, tDict);
print = ifEmpty(i, print);
return print;
}
#endif // fizzbuzz