Created
March 13, 2019 14:45
-
-
Save developer-sdk/b9722a50349f601ecfb632524f0e2925 to your computer and use it in GitHub Desktop.
커플링을 이용한 자연수 구현, 소수 구현, 명령형 프로그래밍
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package functional.stage2; | |
| import java.util.Iterator; | |
| /** | |
| * 커플링을 이용한 자연수 분류기 | |
| * | |
| * @author whitebeard-k | |
| * | |
| */ | |
| public class ClassifierAlpha extends FactorsBeta { | |
| public ClassifierAlpha(int number) { | |
| super(number); | |
| } | |
| public int sum() { | |
| Iterator<Integer> it = factors().iterator(); | |
| int sum = 0; | |
| while (it.hasNext()) | |
| sum += (Integer) it.next(); | |
| return sum; | |
| } | |
| public boolean isPerfect() { | |
| return sum() - number == number; | |
| } | |
| public boolean isAbundant() { | |
| return sum() - number > number; | |
| } | |
| public boolean isDeficient() { | |
| return sum() - number < number; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package functional.stage2; | |
| import java.util.HashSet; | |
| import java.util.Set; | |
| /** | |
| * 커플링 슈퍼 클래스 | |
| * | |
| * @author whitebeard-k | |
| * | |
| */ | |
| public class FactorsBeta { | |
| protected int number; | |
| public FactorsBeta(int number) { | |
| this.number = number; | |
| } | |
| public boolean isFactor(int potential_factor) { | |
| return number % potential_factor == 0; | |
| } | |
| public Set<Integer> factors() { | |
| HashSet<Integer> factors = new HashSet<>(); | |
| for (int i = 1; i <= Math.sqrt(number); i++) | |
| if (isFactor(i)) { | |
| factors.add(i); | |
| factors.add(number / i); | |
| } | |
| return factors; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package functional.stage2; | |
| import java.util.HashSet; | |
| import java.util.Set; | |
| /** | |
| * 커플링을 이용한 소수 구현 | |
| * | |
| * @author whitebeard-k | |
| * | |
| */ | |
| public class PrimeAlpha extends FactorsBeta { | |
| public PrimeAlpha(int number) { | |
| super(number); | |
| } | |
| public boolean isPrime() { | |
| Set<Integer> primeSet = new HashSet<Integer>() { | |
| private static final long serialVersionUID = -8476421042584796116L; | |
| { | |
| add(1); | |
| add(number); | |
| } | |
| }; | |
| return factors().equals(primeSet); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment