Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package armstrongnumbers;
- import java.util.Objects;
- final class IntegerHolder {
- private Integer x;
- public IntegerHolder() {
- this.x = 0;
- }
- public IntegerHolder(Integer x) {
- this.x = x;
- }
- public Integer getX() {
- return this.x;
- }
- public void setX(Integer x) {
- this.x = x;
- }
- }
- final class ArmstrongNumber {
- private IntegerHolder value;
- private Integer sum;
- public ArmstrongNumber(IntegerHolder value) {
- this.value = value;
- }
- public IntegerHolder getValue() {
- return value;
- }
- public Integer getSum() {
- return sum;
- }
- public void setValue(IntegerHolder value) {
- this.value = value;
- }
- public void findSum(final Integer base) {
- Integer tmp = this.value.getX(), tmp_sum = 0;
- do {
- tmp_sum += (int) Math.pow(tmp % base, 3);
- tmp /= base;
- } while (tmp != 0);
- this.sum = tmp_sum;
- }
- public Boolean isArmstrongNumber() {
- return Objects.equals(this.getValue().getX(), this.getSum());
- }
- }
- /**
- *
- * @author Aidan
- */
- public class ArmstrongNumbers {
- private static final int LOOPS = 1000, BASES = 20;
- public static void main(String[] args) {
- IntegerHolder base = new IntegerHolder();
- for (Integer i = 2; i <= LOOPS; i++) {
- ArmstrongNumber tmp = new ArmstrongNumber(base);
- base.setX(i);
- for (Integer j = 2; j <= BASES; j++) {
- tmp.findSum(j);
- if (tmp.isArmstrongNumber()) {
- System.out.println("In base " + j + ", " + i + " is an Armstrong number.");
- System.out.flush();
- }
- }
- if (tmp.isArmstrongNumber()) {
- System.out.println();
- System.out.flush();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement