Is my friend cheating?

SungJunEun·2021년 11월 11일
0

Codewars 문제풀이

목록 보기
13/26
post-thumbnail

Description:

  • A friend of mine takes the sequence of all numbers from 1 to n (where n > 0).
  • Within that sequence, he chooses two numbers, a and b.
  • He says that the product of a and b should be equal to the sum of all numbers in the sequence, excluding a and b.
  • Given a number n, could you tell me the numbers he excluded from the sequence?

The function takes the parameter: n (n is always strictly greater than 0) and returns an array or a string (depending on the language) of the form:

[(a, b), ...] or [[a, b], ...] or {{a, b}, ...} or or [{a, b}, ...]

with all (a, b) which are the possible removed numbers in the sequence 1 to n.

[(a, b), ...] or [[a, b], ...] or {{a, b}, ...} or ... will be sorted in increasing order of the "a".

It happens that there are several possible (a, b). The function returns an empty array (or an empty string) if no possible numbers are found which will prove that my friend has not told the truth! (Go: in this case return nil).

Examples:

removNb(26) should return [(15, 21), (21, 15)]
or
removNb(26) should return { {15, 21}, {21, 15} }
or
removeNb(26) should return [[15, 21], [21, 15]]
or
removNb(26) should return [ {15, 21}, {21, 15} ]
or
removNb(26) should return "15 21, 21 15"

My solution:

function removeNb (n) {
  let a;
  let b;
  let result = [];
  const gaus = sum(n);

  for(a = 1; a<=n; a++) {
    b = (gaus-a) / (a+1) ;
    if(Number.isInteger(b) && b<=n) {
      result.push([a,b]);
    }
  }
  return result;
}

// sum function using reduce methodfunction sum(number) {
  let array=[];
  for(i = 1; i<=number ;i++) {
    array.push(i);
  }
  const sums = array.reduce(function(acc,cur){
    return acc+cur;
  });

  return sums;
}

Best solutions:

function removeNb (n) {
  // from the instruction:
  // a * b = S(n) - a - b
  
  // and the sum of all first n natural numbers is
  // S(n) = n * (n + 1) / 2
  
  // so we can refrase the first formula like this:
  // a * b = n * (n + 1) / 2 - a - b
  // a * b + b = n * (n + 1) / 2 - a
  // b * (a + 1) = n * (n + 1) / 2 - a
  // b = (n * (n + 1) / 2 - a) / (a + 1)
  
  // but a and b are natural and up to n
  
  var results = [];
  for (var a = 1; a <= n; a++) {
    var b = (n * (n + 1) / 2 - a) / (a + 1);
    if (b % 1 === 0 && b <= n) results.push([a, b]);
  }
  return results;
}

유일한 차이점은 'sum(1~n)을 구할 때 어느 방식을 사용했느냐'이다.

profile
블록체인 개발자(진)

2개의 댓글

comment-user-thumbnail
2023년 5월 6일

The https://www.airslate.com/workflows/document/shopping Academy guides each of you and your colleagues through a comprehensive certification programme that teaches you all you need to know about constructing any sort of workflow online, configuring Bots, and simulating real-world use cases.

답글 달기
comment-user-thumbnail
2023년 5월 18일

Determining whether your friend is cheating can be a complex situation. It's important to approach it with caution and gather evidence before jumping to conclusions. Trust and open communication are key. I say you can visit https://www.yellowbirdproject.com/how-to-spy-on-someones-text-messages-without-their-phone-for-free/ and gain skills about girlfriend chat. If you suspect something, have an honest conversation with your friend, expressing your concerns and seeking clarification. Remember to maintain a non-accusatory tone, as misunderstandings can often arise in relationships.

답글 달기