Last active
August 15, 2016 10:55
-
-
Save ARR4N/d47855a7de49ad62b3da6f7ee5980303 to your computer and use it in GitHub Desktop.
Implementation of Fourier Transform as described on SMBC
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
'use strict'; | |
/* | |
* Documentation: http://www.smbc-comics.com/?id=2874 | |
* Copyright 2016 Arran Schlosberg (https://arranschlosberg.com) | |
* License: MIT | |
* Written in honour of the Viking sitting in the corner. | |
*/ | |
interface fourierTransform { | |
(base10: number) => fourierRepresentation; | |
} | |
interface fourierRepresentation { | |
base:number; | |
fours:number; | |
representation:string; | |
} | |
var fourier:fourierTransform = (base10: number): fourierRepresentation => { | |
var chars:string[] = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/'.split(''); | |
base10 = base10|0; // force integer | |
var fouriest: fourierRepresentation = { | |
'base': 10, | |
'fours': 0, // assume 0 and this will update if necessary | |
'representation': base10.toString() | |
}; | |
for(var base:number = 5; base<=chars.length; base++){ // technically this could go to base<=base10 but then we can't ensure enough characters for a representation | |
var BaseX:string = '', // any similarity to the rocketry is purely coincidental | |
remains:number = base10, | |
digit:number, | |
fours:number = 0; | |
for(var rounds:number = 0; rounds<64 && remains>0; rounds++){ // avoid an infinite loop when I make a mistake | |
digit = remains % base; | |
if(digit==4){ | |
fours++; | |
} | |
BaseX = chars[digit] + BaseX; | |
remains = (remains / base)|0; | |
} | |
console.debug('Base %d: %s has %d four(s)', base, BaseX, fours); | |
if(fours > fouriest.fours){ | |
console.debug('More fours in base %d', base); | |
fouriest.base = base; | |
fouriest.fours = fours; | |
fouriest.representation = BaseX; | |
} | |
} | |
return fouriest; | |
} | |
console.debug(fourier(624)); // See documentation | |
exports = fourier; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment