Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags more
Archives
Today
Total
관리 메뉴

말하는 햄zzi

홀수/ 약수/소수 본문

Java/Java

홀수/ 약수/소수

대양파 2023. 5. 6. 04:01
728x90

 

홀수의 합 구하기 

 

-반복문인 for문과 조건문인 if문을 이용해 풀 수 있는 문제

  1. 반복문으로 1부터 10까지 출력합니다.  (1부터 10까지 출력, 초기화식은 i = 1 ,조건식은 i < 10)
  2. 홀수만 출력합니다.
  3. sum에 누적해서 홀수만 더한 결과 구합니다

홀수의 합 예제 )

public class SumOfOdds {
public static void main(String[] args) {
int answer = 0;
int n = 10;
for (int i = 1; i <= n; i++) {
if(i % 2 != 0){
answer += i;
}
}
System.out.println(answer);
}
}
더보기

출력 : 25

**1부터 10까지가 아닌 1부터 n까지 n을 입력받고 싶다면 int n = sc.nextInt();로 바꿔서

     사용자로부터 입력값을 입력받도록 할 수도 있습니다.

 

약수 구하기

  1. % 연산으로 나머지를 구합니다.
  2. 나머지가 0인 경우만 추려 봅니다.
  3. 약수만 출력합니다.

약수 출력 예제)

public class Factor3 {
    public static void main(String[] args) {
        int n = 36;
        for (int i = 1; i <= n; i++) {
            if (n % i == 0) {
                System.out.printf("%d ", i);
            }
        }
    }
}
더보기

출력 :1 2 3 4 6 9 12 18 36

 

 

약수들의 합 구하기 

-약수들의 합을 구하는 알고리즘

 

예제 )

public class 약수의합구하기 {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);


       int n = sc.nextInt();
       int divisorSum=0;


       for (int i=1; i<=n; i++){//1부터 n까지 n의 약수들의 합 구하기
           if(n%i==0){
               divisorSum+=i;
           }
       }


       System.out.println(divisorSum);
   }
}

** 1부터 n까지의 수 중에서 n의 약수들의 합을 구하는 알고리즘
for문을 이용해 1부터 n까지 탐색하면서 n을 i로 나누었을 때 나머지가 0이면 i는 n의 약수
divisorSum 변수에 n의 약수들의 합을 누적

 

약수의 합 예제2 )

public  class SumOfFactors {
   public static void main(String[] args) {
       int num=6;
       int answer =0;
      // System.out.printf("%d %d/n",num,i);
       for (int i = 1; i <=num ; i++) {
           if (num % i==0)answer+=i;
          
       }
       System.out.println(answer);
   }
}
더보기

출력 :6 1,6 2, 6 3, 6 4, 6 5 ,6 6 , 12

 

 

소수구하기

-소수 : 소수는 1보다 큰 자연수 중 1과 자기 자신만을 약수로 가지는 수

모든 1 이상의 정수는 소수의 곱(소인수분해)으로표현

 

 

소수인지 판단하는 예제)

 

public class IsPrime {
   public static void main(String[] args) {
       int num = 6;
       int factors = 0; // 약수의 개수
       for (int i = 2; i < num; i++) { // 2 ~ num까지의 숫자 중에서 약수 찾기
           if(num % i == 0) factors++; // num이 해당 숫자로 나누어 떨어지면 약수
       }
       System.out.printf("factors:%d\\n", factors);
       System.out.println(factors == 0 ? num + "은 소수입니다.": num + "은 소수가 아닙니다.");
   }
}
더보기

출력 : factors값  2 6은 소수가 아닙니다 출력

 

소수 판별 예제 2 ) -코드업 1274번

 

public class Main {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       int num = sc.nextInt();
       boolean isPrime = true;
       for (int i = 2; i <= num / 2; i++) {
           if (num % i == 0) {
               isPrime = false;
               break;
           }
       }
       if (isPrime) {
           System.out.println("prime");
       } else {
           System.out.println("not prime");
       }
   }
}
더보기

콘솔창 : 7대입시 prime 출력

 

728x90
반응형