본문 바로가기
알고리즘/Codewars

[Codewars] Wilson primes (8 kyu) / JavaScript

by fluss 2022. 10. 3.

https://www.codewars.com/kata/55dc4520094bbaf50e0000cb

 

Codewars - Achieve mastery through coding practice and developer mentorship

Coding practice for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

DESCRIPTION:

Wilson primes satisfy the following condition. Let P represent a prime number.

Then,

((P-1)! + 1) / (P * P)

should give a whole number.

Your task is to create a function that returns true if the given number is a Wilson prime.

 
 

설명:

윌슨 소수는 다음과 같은 조건을 만족합니다. P를 소수라고 합니다.

그렇다면,

((P-1)! + 1) / (P * P)

은 정수여야 합니다.

주어진 숫자가 윌슨 소수라면 true를 반환하는 함수를 만드세요.

 

풀이

function amIWilson(p) {
  if(p === 5 || p === 13 || p ===563) return true;
  else return false;
}

계속 곱셈을 하다 보니 숫자가 너무 커져서 답이 나오지 않았는데 다르게 생각하면 되는 문제였다. 글에 달려있는 링크로 들어가 보면 지금까지 발견된 윌슨 소수는 5, 13, 563 뿐이라고 한다. BigInt를 써서 푸는 방법도 있었는데 8 kyu이고 직접 링크까지 단 것을 보면 이렇게 푸는 것이 출제자의 의도가 맞지 않나 싶다.

댓글