μΌ | μ | ν | μ | λͺ© | κΈ | ν |
---|---|---|---|---|---|---|
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 |
Tags
- 첫κΈμλλ¬Έμ
- λΆνΈμΊ ν
- μΈν 리μ μ΄
- java
- μλ£κ΅¬μ‘°
- κ±°λμ κ³±
- νκ³
- μ λ€λ¦μ€
- μ€νλ§
- fibonacci
- Spring Data JDBC
- μλ°
- μκ³ λ¦¬μ¦
- λ°μΌλ¦¬μ½λ©
- CLIλͺ λ Ήμ΄
- 컬λ μ νλ μμν¬
- CSS
- testing
- λ°±μ€μκ³ λ¦¬μ¦
- κΉνλΈ
- FilterChain
- λ¬Έμμ΄λ€μ§κΈ°
- 그리λ
- Publishing
- spring data jpa
- κ³μ°κΈ°λ§λ€κΈ°
- νμ΄νλ‘κ·Έλλ°
- HTML
- λ°±μλ
- Spring Security
Archives
- Today
- Total
λμ λͺ¨μ
[Algorithm] μλΌν μ€ν λ€μ€μ 체 λ³Έλ¬Έ
π μλΌν μ€ν λ€μ€μ 체?
- κ³ λ κ·Έλ¦¬μ€ μνμ μλΌν μ€ν λ€μ€κ° λ°κ²¬ν μμ νλ³ μκ³ λ¦¬μ¦
π€ μ½μ(Divisor)?
- 1κ³Ό μκΈ° μμ μΈ μ½μλ₯Ό κ°μ§μ§ μλ 1λ³΄λ€ ν° μμ°μ
π€ μμ(Prime Number)?
- μ΄λ€ μλ₯Ό λλ λ¨μ΄μ§κ² νλ μ
π μκ³ λ¦¬μ¦
- 2λΆν° μμλ₯Ό ꡬνκ³ μ νλ ꡬκ°μ λͺ¨λ μ λμ΄ (맨 μ²μ νμ)
- 2 == μμ (λΉ¨κ°μ)
- Prime numbers: 2
- μκΈ° μμ μ μ μΈν 2μ λ°°μ μ κ±°
- λ¨μμλ μ μ€, 3 == μμ (μ΄λ‘μ)
- Prime numbers: 2 3
- μκΈ° μμ μ μ μΈν 3μ λ°°μ μ κ±°
- λ¨μμλ μ μ€, 5 == μμ (νλμ)
- Prime numbers: 2 3 5
- μκΈ° μμ μ μ μΈν 5μ λ°°μ μ κ±°
- λ¨μμλ μ , 7 == μμ (λ
Έλμ)
- Prime numbers: 2 3 5 7
- μκΈ° μμ μ μ μΈν 7μ λ°°μ μ κ±°
- μμ κ³Όμ μ λ°λ³΅νλ©΄ ꡬνλ ꡬκ°μ λͺ¨λ μμκ° λ¨κ² λ¨
π μ½λ ꡬν
π€ λ¬Έμ : μμ νλ³
- 1 μ΄μμ μμ°μλ₯Ό μ λ ₯λ°μ
- μμμ΄λ©΄ true, μλλ©΄ false 리ν΄
π€ λ°λ³΅λ¬Έ μ¬μ©
public class Eratosthenes1 {
public boolean isPrime(int num) {
if(num == 1)
return false;
for(int divisor = 2; divisor < num; divisor++) { // λλ λ¨μ΄μ§λ μκ° μλμ§ μλμ§ νμΈ
// μ΄κΈ°νλ₯Ό 2λΆν° νλ건 1μ΄λ μκΈ° μμ μΈ λλ μ§λκ² μμ΄μ
if(num % divisor == 0)
return false;
}
return true;
}
}
π€ Math ν΄λμ€ μ¬μ©
public class Eratosthenes2 {
public boolean isPrime(int num) {
// Math ν΄λμ€ μ¬μ©
int sqrt = (int)Math.sqrt(num);
if(num == 1)
return false;
for(int i = 2; sqrt >= i; i++) {
if(num % i == 0)
return false;
}
return true;
}
}
π Ref.
μλΌν μ€ν λ€μ€μ 체 - μν€λ°±κ³Ό, μ°λ¦¬ λͺ¨λμ λ°±κ³Όμ¬μ
ko.wikipedia.org
'Algorithm' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
[μλ°] λ°±μ€ 11721 | μ΄ κ°μ© λμ΄ μΆλ ₯νκΈ° (0) | 2022.08.09 |
---|---|
SEB_BE_40 | 021 | λ°μΌλ¦¬μ½λ©, μ¬κ·ν¨μ (0) | 2022.07.21 |
Comments