A list of coding challenges I've solved in different languages
- D0: basic
- D1: easy
- D2: hard
- D3: very hard
- https://projecteuler.net/archives
- Problems solved: 1,2,3,4,5,6,7,8,9,10
- 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 10 terms. Although it has not been proved yet (Collatz Problem), it is * thought that all starting numbers finish at 1.
- Which starting number, under one million, produces the longest chain?
- NOTE: Once the chain starts the terms are allowed to go above one million.
- n/2 = 1 => n=2 , n/2=2 => n=4 , n=8 or n=1, n = 16, n=32 or n=5 => n=64
- example: given two arrays of same length:
- [-1,3,8,2,9,5]
- [4,1,2,10,5,20]
- and a target number like 24
- find two numbers one belongs to the 1st array, the second belongs the 2nd array whom sum is the closest to the target number
- https://github.com/m-housni/coding-challenges/blob/main/D2/targetNumberFromTwoArrays.py
- The sequence of triangle numbers is generated by adding the natural numbers. So the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first * ten terms would be:
- 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...
- Let us list the factors of the first seven triangle numbers:
- 1: 1
- 3: 1,3
- 6: 1,2,3,6
- 10: 1,2,5,10
- 15: 1,3,5,15
- 21: 1,3,7,21
- 28: 1,2,4,7,14,28
- We can see that 28 is the first triangle number to have over five divisors.
- What is the value of the first triangle number to have over five hundred divisors?
- https://github.com/m-housni/coding-challenges/blob/main/D2/highlyDivisibleTriangularNumber.py
- In the 20×20 grid below, four numbers along a diagonal line have been marked in red.
- The product of these numbers is 26 × 63 × 78 × 14 = 1788696.
- What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?
- https://github.com/m-housni/coding-challenges/blob/main/D2/largestProductInGrid.py
What is the greatest product of four adjacent numbers in the same direction (up, down, left, right, or diagonally) in the 20×20 grid?*
- The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
- Find the sum of all the primes below two million.
- https://github.com/m-housni/coding-challenges/blob/main/D1/sumOfPrimes.py
- 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.
- https://github.com/m-housni/coding-challenges/blob/main/D1/specialPhytagorianTriplet.py
- examples:
- isPrime(10) # false
- isPrime(13) # true
- https://github.com/m-housni/coding-challenges/blob/main/D0/isPrime.py
- The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.
- 73167176531330624919225119674426574742355349194939698352031277450632623957831801698480186947885184858615607891129494954595017379583319528532088055112540698747158523863050715693290963295227443043556689664895044524452316173185640309871112172238311362229893423380308135336276614282806444486645238749303589072962904915604407723907138105158593079608667017242712188399879790879227492190169972088809377665727333001053367881220235421809751254540594752243525849077116705560136048395864467063244157221553975369781797784617406495514929086256932197846862248283972241375657056057490261407972968652414535100474821663704844031998900088952434506585412275886668811642717147992444292823086346567481391912316282458617866458359124566529476545682848912883142607690042242190226710556263211111093705442175069416589604080719840385096245544436298123098787992724428490918884580156166097919133875499200524063689912560717606058861164671094050775410022569831552000559357297257163626956188267042825248360082325753042075296345
- Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
- https://github.com/m-housni/coding-challenges/blob/main/D1/largestProductInSerie.py
- By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.
- What is the 10 001st prime number?
- https://github.com/m-housni/coding-challenges/blob/main/D1/prime10001.py
- The sum of the squares of the first ten natural numbers is,1^2+2^2+...+10^2 = 385
- The square of the sum of the first ten natural numbers is, (1°2+...+10)^2 = 3025
- Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025-385=2640
- Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.
- https://github.com/m-housni/coding-challenges/blob/main/D0/sumSquareDiff.py
- 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 20?
- https://github.com/m-housni/coding-challenges/blob/main/D1/smallestMultiple.js
- example pelindroms: 121, 33, 3223, 435534
- https://github.com/m-housni/coding-challenges/blob/main/D0/isPelindrom.py
- Covert an integer to an array
- Example: 123 => [1,2,3], 4354 => [4,3,5,4]
- https://github.com/m-housni/coding-challenges/blob/main/D0/numberToArray.py
- 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 3-digit numbers.
- https://github.com/m-housni/coding-challenges/blob/main/D1/largestPelindromProduct.py
- The prime factors of 13195 are 5, 7, 13 and 29.
- What is the largest prime factor of the number 600851475143 ?
- https://github.com/m-housni/coding-challenges/blob/main/D1/largestPrimeFactor.js
- Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:
- 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
- By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
- https://github.com/m-housni/coding-challenges/blob/main/D1/evenFibonacciNumbers.js
- If we list all the natural numbers below N that are multiples of p or q.
- (?) Find the sum of all the multiples of p or q below N.
- https://github.com/m-housni/coding-challenges/blob/main/D1/multiplesPnQ.js
- If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9.
- The sum of these multiples is 23.
- (?) Find the sum of all the multiples of 3 or 5 below 1000.
- Answer: 233168
- https://github.com/m-housni/coding-challenges/blob/main/D1/multiplesThreeFive.js
Given a list of integers and a target integer this function will return the index of the target if it exists or None (null in python)
Write a function which takes a peice of code as input and returns true if the program has an infinite loop and false otherwise.
Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals k.
- https://github.com/m-housni/coding-challenges/tree/main/diffuculty_2/searchSubArraysSumsToK
- https://codepen.io/m-housni/pen/RwGvNrZ
- Bubble sort
- Selection sort
- Swap sort
- Insertion sort
- Merge sort
- Quick sort
Given two arrays of integers arr1 and arr2, arr2 is subsequent array of arr1 if arr1 includes all elements of arr2 in the same order but not necessarily adjacent.
Example: [2,0,4] is subsequent array of [1,2,3,0,4]