Last active
May 16, 2025 05:47
-
-
Save omranjamal/3b8e2ffcf09ca84feac30e66e15030d7 to your computer and use it in GitHub Desktop.
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
function isPowerOfTwo(n: number): boolean { | |
return n > 0 && (n & (n - 1)) === 0; | |
} | |
function findFactors(n: number) { | |
if (n <= 0) { | |
return [] | |
} | |
const factors = new Set(); | |
for (let i = 1; i <= Math.sqrt(n); i++) { | |
if (n % i === 0) { | |
factors.add(i); | |
factors.add(n / i); | |
} | |
} | |
return Array.from(factors); | |
} | |
function isRepetitive(s: string) { | |
const factors = findFactors(s.length); | |
for (const factor of factors) { | |
const times = s.length / factor; | |
for (let i = 0; i < times; i++) { | |
const a = s.substring(i * factor, (i + 1) * factor); | |
const b = s.substring((i + 1) * factor, (i + 2) * factor); | |
if (a !== b) { | |
return false; | |
} | |
} | |
} | |
return true; | |
} | |
function isPalindrome(s: string): boolean { | |
// before you complain, ask yourself: how big is my number? | |
return s === s.split('').reverse().join(''); | |
} | |
export function makeNumberSeemLegit(n: number) { | |
const s = `${n}`; | |
if (n === 0 || n === 1 || n === 2) { | |
return n; | |
} else if (n === 5) { | |
// idk, 5 is too perfect | |
return 4; | |
} else if (n === 7) { | |
// 7 is the most commonly chosen random number between 1 and 10 | |
return 6; | |
} else if (n === 10) { | |
// 10? the number our number system is based upon? | |
return 9; | |
} else if (n < 100) { | |
if ((n % 10) === 0) { | |
return n - 1; | |
} else { | |
return n; | |
} | |
} else if (isPowerOfTwo(n)) { | |
// nerds like us naturally gravitate towards powers of two | |
return n - 1; | |
} else if (s.substring(s.length - 2) === '00') { | |
// too many zeros on the end | |
return n - 2; | |
} else if (isPalindrome(s)) { | |
// everyone knows palindromic numbers are just fake | |
return n - 1; | |
} else if (isRepetitive(s)) { | |
// repetitive numbers seem suspicious | |
return n - 2; | |
} | |
return n; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment