Skip to content

Instantly share code, notes, and snippets.

@wileam
Last active August 17, 2020 06:33
Show Gist options
  • Save wileam/e0d07c3fbecc19352be58944ebedd52c to your computer and use it in GitHub Desktop.
Save wileam/e0d07c3fbecc19352be58944ebedd52c to your computer and use it in GitHub Desktop.
nth in joined Demlo Number String
// What's the 2000th digit in
// 1121123211234321123454321...?
// Assumption:
// Assume it's a number of joined demlo number, https://mathworld.wolfram.com/DemloNumber.html
// find the nth digit in the number.
function findNthDigitInJoinedDemloNumber(n) {
// find the rowIndex of demlo number and the charAtIndex of that number
const rowIndex = Math.ceil(Math.sqrt(n));
const lengthOfDemloNumber = rowIndex * 2 - 1;
const charAtIndex = lengthOfDemloNumber - (Math.pow(rowIndex, 2) - n);
// get the demlo number
const demloNumber = getDemloNumberbyIndex(rowIndex);
const stringDemloNumber = String(demloNumber).endsWith('n') // bigint
? String(demloNumber).slice(0, -1)
: String(demloNumber);
const digit = stringDemloNumber.charAt(charAtIndex - 1);
// console.log(rowIndex, charAtIndex, demloNumber, digit);
console.log(`the ${n}th digit is ${digit}`);
return digit;
}
function getDemloNumberbyIndex(rowIndex) {
const repunit = (10n ** BigInt(rowIndex) - 1n) / 9n; // https://mathworld.wolfram.com/Repunit.html
// console.log(repunit)
return repunit * repunit;
}
findNthDigitInJoinedDemloNumber(2000); //8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment