Skip to content

Instantly share code, notes, and snippets.

@tianmingzuo
tianmingzuo / newLargestPalindromeProduct6.js
Created September 27, 2019 22:42
Largest palindrome product: A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made from the product of two n-digit numbers.
function isPalindrome(s) {
let len = s.length;
for (let i = 0; i < Math.floor(len / 2); i++) {
if (s.charAt(i) !== s.charAt(len - i - 1)) {
return false;
}
}
return true;
}
@tianmingzuo
tianmingzuo / smallestMultiple.js
Created September 30, 2019 15:23
Smallest multiple: 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to n?
function gcd(a, b) { //greatest common divisor
if(isNaN(a) || isNaN(b)){
//throw new Error("a or b should be a number");
//console.log('a or b should be a number');
return false;
}
if(Math.floor(a) !== a || Math.floor(b) !== b){
//throw new Error("a or b should be an integer");
//console.log('a or b should be an integer');
return false;
function isPrime(num) {
if(num === 2 || num === 3){
return true;
}
for(let i =2; i<=Math.floor(Math.sqrt(num)); i++){
if(num % i === 0){
return false;
}
}
@tianmingzuo
tianmingzuo / maxProductInAseries.js
Created October 1, 2019 13:38
The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832. Find the n adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
function largestProductinaSeries(n) {
// Good luck!
let thousandDigits = [7,3,1,6,7,1,7,6,5,3,1,3,3,0,6,2,4,9,1,9,2,2,5,1,1,9,6,7,4,4,2,6,5,7,4,7,4,2,3,5,5,3,4,9,1,9,4,9,3,4,9,6,9,8,3,5,2,0,3,1,2,7,7,4,5,0,6,3,2,6,2,3,9,5,7,8,3,1,8,0,1,6,9,8,4,8,0,1,8,6,9,4,7,8,8,5,1,8,4,3,8,5,8,6,1,5,6,0,7,8,9,1,1,2,9,4,9,4,9,5,4,5,9,5,0,1,7,3,7,9,5,8,3,3,1,9,5,2,8,5,3,2,0,8,8,0,5,5,1,1,1,2,5,4,0,6,9,8,7,4,7,1,5,8,5,2,3,8,6,3,0,5,0,7,1,5,6,9,3,2,9,0,9,6,3,2,9,5,2,2,7,4,4,3,0,4,3,5,5,7,6,6,8,9,6,6,4,8,9,5,0,4,4,5,2,4,4,5,2,3,1,6,1,7,3,1,8,5,6,4,0,3,0,9,8,7,1,1,1,2,1,7,2,2,3,8,3,1,1,3,6,2,2,2,9,8,9,3,4,2,3,3,8,0,3,0,8,1,3,5,3,3,6,2,7,6,6,1,4,2,8,2,8,0,6,4,4,4,4,8,6,6,4,5,2,3,8,7,4,9,3,0,3,5,8,9,0,7,2,9,6,2,9,0,4,9,1,5,6,0,4,4,0,7,7,2,3,9,0,7,1,3,8,1,0,5,1,5,8,5,9,3,0,7,9,6,0,8,6,6,7,0,1,7,2,4,2,7,1,2,1,8,8,3,9,9,8,7,9,7,9,0,8,7,9,2,2,7,4,9,2,1,9,0,1,6,9,9,7,2,0,8,8,8,0,9,3,7,7,6,6,5,7,2,7,3,3,3,0,0,1,0,5,3,3,6,7,8,8,1,2,2,0,2,3,5,4,2,1,8,0,9,7,5,1,2,5,4,5,4,0,5,9,4,7,5,2,2,4,3,5,2,5,8,4,9,0,7,7,1,1,6,7,0,5,5,6,0,1,3,6,0,4,
@tianmingzuo
tianmingzuo / specialPythagoreanTriplet.js
Created October 1, 2019 14:26
Special Pythagorean triplet: A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2 For example, 32 + 42 = 9 + 16 = 25 = 52. There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product abc such that a + b + c = n.
function specialPythagoreanTriplet(n) {
let i, j, k;
for(i=1; i<Math.floor(n/2); i++){
for(j=i+1; j<Math.floor(n/2); j++){
k = n-i-j;
if(i**2 + j**2 === k**2){
return i*j*k;
}
}
}
@tianmingzuo
tianmingzuo / summationOfPrimes.js
Created October 1, 2019 15:13
Summation of primes: The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below n.
function isPrime(num) {
if(num === 2 || num === 3){
return true;
}
for(let i =2; i<=Math.floor(Math.sqrt(num)); i++){
if(num % i === 0){
return false;
}
}
@tianmingzuo
tianmingzuo / largeSum.js
Last active October 2, 2019 01:16
Large sum: Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.
function largeSum(arr) {
let sum = 0;
let len = arr.length;
let i, j;
let newArr = [];
for(i=0; i<len; i++){
let item = arr[i].slice(0,13); //The later part of digits will not affect the first 10 digits.
let num = parseInt(item);
newArr.push(num);
}
@tianmingzuo
tianmingzuo / longestCollatzSequence.js
Created October 2, 2019 02:36
Longest Collatz sequence: The following iterative sequence is defined for the set of positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Using the rule above and starting with 13, we generate the following sequence: 13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 It can be seen that this sequence (starting at 13 and finishing at 1) contains …
function handleNum (n){
if(n%2 === 0){
n = n/2;
}else{
n = 3*n+1;
}
return n;
}
//console.log(handleNum (8));
@tianmingzuo
tianmingzuo / nDigitFabonacciNum.js
Created October 3, 2019 13:51
1000-digit Fibonacci number: The Fibonacci sequence is defined by the recurrence relation: Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1. Hence the first 12 terms will be: F1 = 1 F2 = 1 F3 = 2 F4 = 3 F5 = 5 F6 = 8 F7 = 13 F8 = 21 F9 = 34 F10 = 55 F11 = 89 F12 = 144 The 12th term, F12, is the first term to contain three digits. What is the index of t…
function digitFibonacci(n) {
let a = 1;
let b = 1;
let i = 2;
while(('' + b).length < n){
[a, b] = [b, a+b];
i++
}
// Good luck!
@tianmingzuo
tianmingzuo / distinctPowers.js
Created October 3, 2019 14:47
Distinct powers: Consider all integer combinations of ab for 2 ≤ a ≤ 5 and 2 ≤ b ≤ 5: 22=4, 23=8, 24=16, 25=32 32=9, 33=27, 34=81, 35=243 42=16, 43=64, 44=256, 45=1024 52=25, 53=125, 54=625, 55=3125 If they are then placed in numerical order, with any repeats removed, we get the following sequence of 15 distinct terms: 4, 8, 9, 16, 25, 27, 32, 6…
function distinctPowers(n) {
// Good luck!
let i, j, k;
let arr = [];
for(i=2; i<=n; i++){
for(j=2; j<=n; j++){
let flag = true;
let item = i**j;
for(k=0; k<arr.length; k++){