코딩테스트
나머지가 1이 되는 수 찾기
한우종
2025. 2. 22. 19:01
문제 설명
자연수 n이 매개변수로 주어집니다. n을 x로 나눈 나머지가 1이 되도록 하는 가장 작은 자연수 x를 return 하도록 solution 함수를 완성해주세요. 답이 항상 존재함은 증명될 수 있습니다.
제한사항
3 ≤ n ≤ 1,000,000
나의풀이
C#
using System;
using System.Collections.Generic;
public class Solution {
public int solution(int n) {
List<int> nums = new List<int>();
for(int x = 1 ; x<n ; x++){
if(n%x == 1){
nums.Add(x);
}
}
Console.WriteLine(nums[0]);
return nums[0];
}
}
JS
function solution(n) {
for(let i = 2; i<n;i++){
if(n%i === 1){
return i
}
}
}