-
[프로그래머스 알고리즘] 1~N까지 배열만들기, 제곱근의 갯수와 약수의 관계알고리즘 2023. 6. 21. 11:36
약수의 개수와 덧셈
function solution(left, right) { const arr = [...Array(right+1).keys()].slice(1); const newArr = arr.filter(e => e >= left) const answer = newArr.map(e => numYacksu(e)).reduce((a, b) => a += b) return answer; } const numYacksu = (n) => { let count = 0; for (let i = 1; i <= n; i++) { if (n % i === 0) {count++} } return count % 2 === 0 ? n: -n; }
이렇게 열심히 풀었건만 다른사람 풀이에는 훨씬 간단해서 난 천재가 아님을 느낀다.
처음에 arr를 생성할 떄 싸용한 방법은 Array가 가지고 있는 key의 특성을 통해서 1~N까지의 배열을 만드는 방법이다 혹시나 나중에 써먹을 수 있으니 기억은해두자
근데 실상은 그냥 for문에서 i에 left값 len에 right값 넣었으면 위에 두중은 안써도 됬다.
그리고 나는 배열을 구해서 배열을 다 더했는데 이놈 자식들은 값이 0인 변수를 선언하고 뺴기 더하기 하더라 굳이 불필요하게 배열을 계속 만들고 있는 이 로직을 고쳐야겠다.
Number.isInteger(Math.sqrt(i))
이 녀석은 제곱근을 구하고 이게 정수인지 아닌지를 묻는 자식인데 제곱근이 정수면 약수의 개수가 홀수란다... ㄷㄷ
'알고리즘' 카테고리의 다른 글
[프로그래머스 알고리즘] 2중반복문 (0) 2023.06.22 [프로그래머스 알고리즘] while과 reduce의 합작 (0) 2023.06.21 [프로그래머스 level.1] 3중반복문, 소수 판별식 (0) 2023.06.21 [프로그래머스 level.1] join(), split() (0) 2023.06.21 [프로그래머스 level.1] 내림차순정렬, 아스키코드 변환 (0) 2023.06.21