Skip to content

Instantly share code, notes, and snippets.

@zardoru
Last active April 23, 2018 05:01
Show Gist options
  • Save zardoru/9065d16f004bee0b32e15762bb133c6f to your computer and use it in GitHub Desktop.
Save zardoru/9065d16f004bee0b32e15762bb133c6f to your computer and use it in GitHub Desktop.
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cmath>
unsigned long quickdigitalroot(unsigned long num) {
return num - std::floor( ( (double)num - 1.0) / 9.0 );
}
bool is10InDigitalRoot(unsigned long num) {
long dr = 0;
while (num > 0) {
dr += num % 10;
num /= 10;
}
if (dr > 10) { // keep going
is10InDigitalRoot(dr);
}
if (dr == 10) { // yes, yes it is
return true;
}
return false;
}
unsigned long decAsHexToDec(unsigned long num)
{
unsigned long result = 0;
long pow_idx = 0;
while (num > 0) {
long dig = num % 10;
result += dig * pow(16, pow_idx);
pow_idx++;
num /= 10;
}
return result;
}
long attempts = 0;
void cheese(unsigned long start_number, long start_bit = 0, long bits_left = 9)
{
if (bits_left == 0)
{
attempts++;
unsigned long readashex = decAsHexToDec(start_number);
// long dr = quickdigitalroot(readashex);
bool dr = is10InDigitalRoot(readashex);
if (dr && readashex != 1)
std::cout << std::left << std::setw(12) << start_number << " fits " << " ashex " << readashex << std::endl;
else
std::cerr << "\r" << start_number << " attempt: " << attempts << " as hex: " << readashex << " dr: " << dr << " ";
return;
}
unsigned long bit_n = 1 << start_bit;
if ( (start_number & bit_n) == 0 ) {
// number is not preset
if (start_bit + bits_left < 32) {
// with ith bit on
cheese(start_number | bit_n, start_bit + 1, bits_left - 1);
// without ith bit on
cheese(start_number, start_bit + 1, bits_left);
}
}
}
int main() {
cheese(1 << 30);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment