You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
varƒ=require('f'),vectorious=require('vectorious');// returns an array containing the range of numbers [m, n)varrange=(m,n)=>vectorious.Vector.range(m,n).toArray();// will hold our answersvarans;
A great resource for functional problems is Project Euler,
so let's go ahead and solve the first five problems.
Full source is available in this GitHub repo.
Most of the code is self-explanatory, but you should at least have knowledge of basic Array
operations (i.e. map, reduce, filter, forEach, concat) and how to use them.
Multiples of 3 and 5
Find the sum of all the multiples of 3 or 5 below 1000.
// add two numbersvaradd=(a,b)=>a+b;// check if number is multiple of 5 or 3varmultiple=(x)=>(x%5===0||x%3===0);ans=range(0,1000).filter(multiple).reduce(add);console.log('Multiples of 3 and 5:',ans);
Even fibonacci numbers
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
// check if number is evenvareven=(x)=>x%2===0;varfib=[1,2];while(ƒ.last(fib)<4000000)fib=ƒ.concat([1,2],ƒ.zipWith(add,fib,ƒ.tail(fib)));ans=fib.filter(even).reduce(add);console.log('Even fibonacci numbers:',ans);
Largest prime factor
What is the largest prime factor of the number 600851475143?
// integer square rootvarisqrt=(x)=>Math.floor(Math.sqrt(x));// integer divisionvaridiv=(a,b)=>Math.floor(a/b);// check n mod xvarmod=(n)=>(x)=>n%x===0;// get prime factors from arbitrary numbervarfactors=(n)=>{varprime=ƒ.take(1,range(2,isqrt(n)+2).filter(mod(n)));if(!prime.length)returnn===1 ? [] : [n];returnprime.concat(factors(idiv(n,prime)));};ans=ƒ.last(factors(600851475143));console.log('Largest prime factor:',ans);
Largest palindrome product
Find the largest palindrome made from the product of two 3-digit numbers.
// check if number is palindromevarpalindrome=(n)=>n===parseInt((''+n).split('').reverse().join(''));ans=[];range(100,1000).forEach(x=>range(x,1000).forEach(y=>ans.push(x*y)));ans=Math.max(...ans.filter(palindrome));console.log('Largest palindrome product:',ans);
Smallest multiple
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
// product of two numbersvarproduct=(a,b)=>a*b;// reduce an array of arrays with productvarproductReduce=(xs)=>xs.reduce(product);// check if array is homogeneous// i.e. [2, 2, 2] is homogeneous, [2, 2, 3] is not.varhomogeneous=(xs)=>ƒ.head(xs)===ƒ.last(xs);// if array contains duplicates with differing lengths,// keep the one that is longest// i.e. if array contains [2], [2, 2] and [2, 2, 2] only keep [2, 2, 2]varduplicates=(xs,_,self)=>self.filter((ys)=>ƒ.head(xs)===ƒ.head(ys)&&xs.length<ys.length).length ?
false : true;ans=range(2,20).map(factors).filter(homogeneous).filter(duplicates).map(productReduce).reduce(product);console.log('Smallest multiple:',ans);
I've added a comment section below where you can ask questions or highlight any
errors with the code.
The text was updated successfully, but these errors were encountered:
Originally posted
2015-11-20
.In this exercise we will use node.js 5.0.0 and
ƒ
for Haskell-style functions,vectorious
for creating ranges.Install above libraries with npm:
Then include them in your project:
A great resource for functional problems is Project Euler,
so let's go ahead and solve the first five problems.
Full source is available in this GitHub repo.
Most of the code is self-explanatory, but you should at least have knowledge of basic
Array
operations (i.e.
map
,reduce
,filter
,forEach
,concat
) and how to use them.Multiples of 3 and 5
Even fibonacci numbers
Largest prime factor
Largest palindrome product
Smallest multiple
I've added a comment section below where you can ask questions or highlight any
errors with the code.
The text was updated successfully, but these errors were encountered: