code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

01 Analysis Of Algorithm

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

const { performance } = require('perf_hooks')

let timed = f => (...args) => {
  let start = performance.now()
  let ret = f(...args)
  console.log(`${f.name} took ${(performance.now() - start).toFixed(3)}ms`)
  return ret
}

// Given a number n, write a function to find sum of first n natural numbers

let func1 = n => {
  console.log((n * (n + 1)) / 2)
}
func1 = timed(func1)
func1(1000000) // took 9.458ms

let func2 = n => {
  let sum = 0
  for (let i = 1; i <= n; i++) {
    sum += i
  }
  console.log(sum)
}
func2 = timed(func2)
func2(1000000) // took 40.455ms

let func3 = n => {
  let sum = 0
  for (let i = 1; i <= n; i++) {
    for (let j = 1; j <= i; j++) {
      sum += 1
    }
  }
  console.log(sum) // took 777150.082ms
}
func3 = timed(func3)
func3(1000000)

Created 2021-03-17T23:05:19+05:18, updated 2021-03-17T23:09:49+05:18 · History · Edit