আর্মস্ট্রং নাম্বার (Armstrong Number) কী?
আর্মস্ট্রং নাম্বার (Narcissistic Number) হলো এমন একটি সংখ্যা যা নিজের অঙ্কগুলোর ঘনফলের সমষ্টির সমান। n-অঙ্কের সংখ্যার জন্য: প্রতিটি অঙ্কের n-তম ঘাতের যোগফল = সংখ্যাটি নিজে।
৩-অঙ্কের উদাহরণ:
- ১৫৩ = 1³ + 5³ + 3³ = 1 + 125 + 27 = ১৫৩ ✓
- ৩৭০ = 3³ + 7³ + 0³ = 27 + 343 + 0 = ৩৭০ ✓
- ৩৭১ = 3³ + 7³ + 1³ = 27 + 343 + 1 = ৩৭১ ✓
- ৪০৭ = 4³ + 0³ + 7³ = 64 + 0 + 343 = ৪০৭ ✓
ইনপুট ও আউটপুট উদাহরণ
ইনপুট:
The two inputs : 1 and 500
আউটপুট:
The Armstrong numbers are: 153 370 371 407
অ্যালগরিদম
- প্রোগ্রাম শুরু করুন
- তিনটি ইন্টিজার ভেরিয়েবল ঘোষণা করুন: my_low, my_high, i
- ব্যবহারকারী থেকে লোয়ার এবং আপার লিমিট ইনপুট নিন
- IsArmstrong(n) ফাংশন ডিফাইন করুন যা বুলিয়ান রিটার্ন করে
- ফাংশনে: n-এর প্রতিটি অঙ্কের ঘনফলের যোগফল বের করুন এবং n-এর সাথে তুলনা করুন
- ফর লুপে my_low+1 থেকে my_high-1 পর্যন্ত IsArmstrong কল করুন
- যদি true রিটার্ন হয়, তবে সংখ্যাটি প্রিন্ট করুন
- প্রোগ্রাম শেষ করুন
উদাহরণ ১: ব্যবহারকারী ইনপুট নেওয়া
import java.util.Scanner;
public class ArmstrongNumbers {
public static void main(String[] args) {
int my_low, my_high, i;
Scanner my_scanner = new Scanner(System.in);
System.out.println("Required packages have been imported");
System.out.println("A scanner object has been defined");
System.out.println("Enter the first number :");
my_low = my_scanner.nextInt();
System.out.println("Enter the limit :");
my_high = my_scanner.nextInt();
System.out.println("The Armstrong numbers are :");
for (i = my_low + 1; i < my_high; ++i) {
if (IsArmstrong(i))
System.out.print(i + " ");
}
}
public static boolean IsArmstrong(int i) {
int check, my_rem, my_sum;
my_sum = 0;
check = i;
while (check != 0) {
my_rem = check % 10;
my_sum = my_sum + (my_rem * my_rem * my_rem);
check = check / 10;
}
return my_sum == i;
}
}
উদাহরণ ২: পূর্বনির্ধারিত মান ব্যবহার
public class ArmstrongNumbers {
public static void main(String[] args) {
int my_low = 1, my_high = 500, i;
System.out.println("The starting and ending numbers are defined as " + my_low + " and " + my_high);
System.out.println("The Armstrong numbers are :");
for (i = my_low + 1; i < my_high; ++i) {
if (IsArmstrong(i))
System.out.print(i + " ");
}
}
public static boolean IsArmstrong(int i) {
int check, my_rem, my_sum;
my_sum = 0;
check = i;
while (check != 0) {
my_rem = check % 10;
my_sum = my_sum + (my_rem * my_rem * my_rem);
check = check / 10;
}
return my_sum == i;
}
}
কোড ব্যাখ্যা
- IsArmstrong ফাংশন: পুনরাবৃত্তি ব্যবহার করে (Reusability) - ক্লিন কোড
- while (check != 0): সব অঙ্ক প্রসেস হওয়া পর্যন্ত লুপ
- my_rem * my_rem * my_rem: Math.pow() এর চেয়ে ফাস্ট (ইন্টিজার অপারেশন)
- লুপ রেঞ্জ: my_low+1 থেকে my_high-1 (এন্ডপয়েন্ট এক্সক্লুসিভ)
সাধারণীকৃত আর্মস্ট্রং (n-অঙ্কের জন্য)
public static boolean isArmstrongGeneral(int num) {
String str = String.valueOf(num);
int n = str.length();
int sum = 0;
int temp = num;
while (temp != 0) {
int digit = temp % 10;
sum += Math.pow(digit, n);
temp /= 10;
}
return sum == num;
}
ইটারেটিভ ভার্শন (মেইন মেথডে ইনলাইন)
public class ArmstrongNumbersInline {
public static void main(String[] args) {
int low = 1, high = 500;
System.out.println("Armstrong numbers between " + low + " and " + high + ":");
for (int i = low; i < high; i++) {
int sum = 0, check = i;
while (check != 0) {
int rem = check % 10;
sum += rem * rem * rem;
check /= 10;
}
if (sum == i) System.out.print(i + " ");
}
}
}
জানা আর্মস্ট্রং নাম্বার (১০,০০০ পর্যন্ত)
০, ১, ২, ৩, ৪, ৫, ৬, ৭, ৮, ৯, ১৫৩, ৩৭০, ৩৭১, ৪০৭, ১৬৩৪, ৮২০৮, ৯৪৭৪
পারফরম্যান্স তুলনা
| পদ্ধতি | রেঞ্জ ১-১০০০০ | রেঞ্জ ১-১০০০০০০ |
|---|---|---|
| সিম্পল ফাংশন (O(n·d)) | ~৫ ms | ~৫০০ ms |
| প্রি-কম্পিউটড ঘন (লুকআপ টেবিল) | ~১ ms | ~৫০ ms |
| String কনভার্সন | ~৮ ms | ~৮০০ ms |
লুকআপ টেবিল অপ্টিমাইজেশন
// ০-৯ এর ঘন প্রি-কম্পিউট
static final int[] CUBES = {0, 1, 8, 27, 64, 125, 216, 343, 512, 729};
public static boolean isArmstrongFast(int num) {
int sum = 0, temp = num;
while (temp != 0) {
sum += CUBES[temp % 10];
temp /= 10;
}
return sum == num;
}
Stream API ব্যবহার (Java 8+)
IntStream.rangeClosed(low + 1, high - 1)
.filter(ArmstrongNumbers::isArmstrong)
.forEach(n -> System.out.print(n + " "));