Skip to content

Instantly share code, notes, and snippets.

@mattcollier
Created April 17, 2016 16:10
Show Gist options
  • Save mattcollier/df24f72ca2c6bbc2d315d732a5e642ce to your computer and use it in GitHub Desktop.
Save mattcollier/df24f72ca2c6bbc2d315d732a5e642ce to your computer and use it in GitHub Desktop.
cyclicity
var lastDigit = function(str1, str2){
var x = Number(str1.substr(str1.length - 1));
if(Number(str2) === 0) {
return 1;
}
if([0, 1, 5, 6].indexOf(x) >= 0) {
return x;
}
if([4,9].indexOf(x) >= 0) {
return cycle(2, x, str2);
}
if([2, 3, 8, 7].indexOf(x) >= 0) {
return cycle(4, x, str2);
}
}
function cycle(cyclicity, x, y) {
var t = Number(modulo(y, cyclicity));
if(t === 0) {
return Math.pow(x, cyclicity) % 10;
}
return Math.pow(x, t) % 10;
}
function modulo (divident, divisor) {
var cDivident = '';
var cRest = '';
for (var i in divident ) {
var cChar = divident[i];
var cOperator = cRest + '' + cDivident + '' + cChar;
if ( cOperator < parseInt(divisor) ) {
cDivident += '' + cChar;
} else {
cRest = cOperator % divisor;
if ( cRest == 0 ) {
cRest = '';
}
cDivident = '';
}
}
cRest += '' + cDivident;
if (cRest == '') {
cRest = 0;
}
return cRest;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment