2023-06-07 06:08:12 +00:00
|
|
|
#include <iostream>
|
2023-06-09 16:15:53 +00:00
|
|
|
#include <cmath>
|
|
|
|
#include <cassert>
|
2023-06-07 06:08:12 +00:00
|
|
|
#include "Spigot.hpp"
|
|
|
|
|
|
|
|
Spigot::Spigot(int requestedNumDigits) {
|
|
|
|
/* create list for number of digits times 10 / 3 */
|
|
|
|
this->spigotListLength = (requestedNumDigits * 10) / 3;
|
|
|
|
this->spigotList.resize(this->spigotListLength, 2); // list is initialized with all 2
|
|
|
|
this->carry = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Spigot::pump(void) {
|
|
|
|
int tempPreDigit = 0;
|
2023-06-09 16:15:53 +00:00
|
|
|
int j = this->spigotListLength - 1;
|
|
|
|
int i = j;
|
2023-10-08 11:29:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This could be handled using boost::compute
|
|
|
|
* with a BOOST_COMPUTE_FUNCTION that just
|
|
|
|
* multiplies every item by 10.
|
|
|
|
*/
|
2023-06-07 06:08:12 +00:00
|
|
|
while(i >= 0) {
|
|
|
|
this->spigotList[i] *= 10;
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
this->carry = 0;
|
2023-06-09 16:15:53 +00:00
|
|
|
i = j;
|
2023-06-07 06:08:12 +00:00
|
|
|
// note this does *not* handle the i=0 case
|
2023-10-08 11:29:55 +00:00
|
|
|
/*
|
|
|
|
* This might also be able to be sped up
|
|
|
|
* with OpenCL/boost::compute by creating
|
|
|
|
* another custom function, but because
|
|
|
|
* the carry has to be passed along this might
|
|
|
|
* not work well
|
|
|
|
*/
|
2023-06-07 06:08:12 +00:00
|
|
|
while(i > 0) {
|
|
|
|
this->spigotList[i] += this->carry;
|
|
|
|
this->carry = (this->spigotList[i] / (i * 2 + 1)) * i;
|
|
|
|
this->spigotList[i] = this->spigotList[i] % (i * 2 + 1);
|
|
|
|
i--;
|
|
|
|
}
|
|
|
|
// special case of i = 0
|
|
|
|
this->spigotList[0] += this->carry;
|
2023-06-09 16:15:53 +00:00
|
|
|
tempPreDigit = this->spigotList[0] / 10;
|
2023-06-07 06:08:12 +00:00
|
|
|
this->spigotList[0] -= (tempPreDigit * 10);
|
|
|
|
|
2023-06-09 16:15:53 +00:00
|
|
|
try {
|
|
|
|
// handle the cases depending on the tempPreDigit
|
|
|
|
if(tempPreDigit >= 0 && tempPreDigit <= 8) {
|
|
|
|
// output all predigits
|
|
|
|
long unsigned pdLen = this->preDigits.size();
|
|
|
|
for(long unsigned int j = 0; j < pdLen; j++) {
|
2023-06-20 21:55:09 +00:00
|
|
|
std::cout << this->preDigits.back();
|
2023-06-09 16:15:53 +00:00
|
|
|
this->preDigits.pop_back();
|
|
|
|
}
|
|
|
|
this->preDigits.insert(this->preDigits.begin(), tempPreDigit);
|
|
|
|
|
|
|
|
} else if(tempPreDigit == 9) {
|
|
|
|
this->preDigits.insert(this->preDigits.begin(), tempPreDigit);
|
|
|
|
} else if(tempPreDigit == 10) {
|
|
|
|
tempPreDigit = 0;
|
|
|
|
long unsigned pdLen = this->preDigits.size();
|
|
|
|
for(long unsigned int j = 0; j < pdLen; j++) {
|
2023-06-20 21:55:09 +00:00
|
|
|
std::cout << (this->preDigits.back() + 1) % 10;
|
2023-06-09 16:15:53 +00:00
|
|
|
this->preDigits.pop_back();
|
|
|
|
}
|
|
|
|
this->preDigits.insert(this->preDigits.begin(), tempPreDigit);
|
|
|
|
} else {
|
|
|
|
std::cout << "We should never have gotten here -- shit has hit the fan!" << std::endl;
|
2023-06-07 06:08:12 +00:00
|
|
|
}
|
2023-06-09 16:15:53 +00:00
|
|
|
} catch (int e) {
|
|
|
|
std::cout << "An exception " << e << " occurred." << std::endl;
|
2023-06-07 06:08:12 +00:00
|
|
|
}
|
2023-06-20 21:55:09 +00:00
|
|
|
std::cout << std::flush;
|
2023-06-07 06:08:12 +00:00
|
|
|
return;
|
|
|
|
}
|