commit 17b68d59cfc205c19ec79095286dc8ddb1ee0676 Author: Joshua Moerman Date: Mon Sep 23 23:17:09 2013 +0200 Initial commit. Tool works already :) diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..c339eb9 --- /dev/null +++ b/Makefile @@ -0,0 +1,3 @@ + +fourier : main.cpp + $(CXX) $(CXXFLAGS) -Ofast -Wall main.cpp -o fourier diff --git a/README.md b/README.md new file mode 100644 index 0000000..9f6d17d --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +Fourier Transform +================= + +This tool was inspired by the following comic: +![Explanation of the Fourier transform](http://www.smbc-comics.com/comics/20130201.gif) + +The comic was right about 624 being the fouriest in base 5, however 625 is fouriest in both base 12 and 23! Amazing! diff --git a/fourier-transform.sublime-project b/fourier-transform.sublime-project new file mode 100644 index 0000000..962c410 --- /dev/null +++ b/fourier-transform.sublime-project @@ -0,0 +1,8 @@ +{ + "folders": + [ + { + "path": "/Users/joshua/Documents/Code/fourier-transform" + } + ] +} diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..b3ff17a --- /dev/null +++ b/main.cpp @@ -0,0 +1,84 @@ +/* + Calculates the optimal fourier transform(s) for a number + Also outputs distribution of least-fouerist-base + Inspired by http://www.smbc-comics.com/?id=2874 + + A very naive implementation :) +*/ + +#include +#include +#include +#include +#include +#include + +using namespace std; + +int length(int number, int base){ + int length = 1; + int acc = base; + + while(number >= acc){ + length++; + acc *= base; + } + + return length; +} + +int fours(int number, int base){ + int count = (number % base) == 4; + + while(number > base){ + number /= base; + if(number % base == 4) count++; + } + + return count; +} + +// Performs a fourier transform to make the input fourier +// Done with a linear search, there are probably betters ways +// There is not always a unique base in which the input is fouriest, so we output a vector +vector fourier(int input){ + int base = 1; + int current_best = 0; + vector bests{}; + + do { + base++; + int current = fours(input, base); + if(current >= current_best){ + if(current > current_best){ + bests.clear(); + current_best = current; + } + bests.push_back(base); + } + } while(length(input, base) > 1); + + return bests; +} + +// The input number is shown, then the fouriness, then the bases in which the input is fouriest. +int main(){ + map distribution; + + cout << "Number, \t Fouriness, \t Possible bases" << endl; + for(int i = 1; i < 1000; i++){ + auto bases = fourier(i); + auto nfours = fours(i, bases.back()); + + distribution[bases.front()]++; + + cout << setw(5) << i << ", \t" << setw(5) << nfours << ", \t[ "; + copy(begin(bases), end(bases), ostream_iterator(cout, " ")); + cout << "]" << endl; + } + + cout << "\nDistribution of least bases" << endl; + for(auto x : distribution){ + cout << x.first << ", \t" << x.second << endl; + } +}