#92 Bit calculator 


In this kata your task is to create bit calculator. Function arguments are two bit representation of numbers ("101","1","10"...), and you must return their sum in decimal representation.


Test.expect(calculate("10","10") == 4);

Test.expect(calculate("10","0") == 2);

Test.expect(calculate("101","10") == 7);

parseInt and some Math functions are disabled.


Those Math functions are enabled: pow,round,random


'매일매일개발 > Codewars' 카테고리의 다른 글

codewars #94 The Hashtag Generator (5kyu)  (0) 2018.08.09
codewars #93 Number Format (6kyu)  (0) 2018.08.08
codewars #91 Find X (6kyu)  (0) 2018.08.06
codewars #90 Cure Cancer (6kyu)  (0) 2018.08.01
codewars #89 Mexican Wave (6kyu)  (0) 2018.07.31

#91 Find X 


We have a function that takes in an integer n, and returns a number x.


Lets call this function findX(n)/find_x(n) (depending on your language):


function findX(n) {

  let x = 0;

  for (let i = 0; i < n; i++) {

    for (let j = 0; j < 2*n; j++)

      x += i + j;

  }

  return x;

}

The functions loops throught the number n and at every iteration, performs a nested loop on 2*n, at each iteration of this nested loop it increments x with the (nested loop index + parents loop index).


This works well when the numbers are reasonably small.


findX(2) //=> 16

findX(3) //=> 63

findX(5) //=> 325

But may be slow for numbers > 103


So your task is to optimize the function findX/find_x, so it works well for large numbers.


Input Range

1 <= n <= 106 (105 in JS)


Note: This problem is more about logical reasoning than it is about finding a mathematicial formula, infact there are no complex math formula involved




'매일매일개발 > Codewars' 카테고리의 다른 글

codewars #93 Number Format (6kyu)  (0) 2018.08.08
codewars #92 Bit calculator (5kyu)  (0) 2018.08.07
codewars #90 Cure Cancer (6kyu)  (0) 2018.08.01
codewars #89 Mexican Wave (6kyu)  (0) 2018.07.31
codewars #88 Which are in? (6kyu)  (0) 2018.07.30

 #90 Cure Cancer


Now you are a doctor.


You are working with a patient's body which has many cells.


The patient's body is a matrix where every row represents a cell.


Each cell contains just uppercase and lowercase letters,


and every cell in the body should be the same.


Oh no! It seems that one of the cells have mutated!


It is your job to locate the mutation so that the chemo specialists can fix it!


return the location [i,j] within the matrix...


before it's too late! :(


example:


cellscellscellscodecodecells

cellscellscellscodecodecells

cellscellscellscodecodecells

cellscellscellscodecodecells

cellscellscellscodecodecells

cellscellscellscodecodecells

cellscellscellscodecodecells

cellscellscellscodecodecells

cellscellscellscodecodecells

cellscellscellscodecadecells <- here it is! [9, 20]

cellscellscellscodecodecells

cellscellscellscodecodecells

cellscellscellscodecodecells

cellscellscellscodecodecells

no bodies will have less than 3 cells.

if the diagnose was a false alarm, return an empty array.




+ Recent posts