would you mind to check this world?
public static void main(String[] args) {
int[] divisors;
int number = 3,
divisibilityCount = 0;
/**
* before we assign the values in an array, lets count
* first how many numbers should WE ASSIGN in an array,
* it wil server as the length of the new array
*/
for (int x = 1; x <= number; x++) {
if ((number % x) == 0) {
divisibilityCount++;
}
}
// instantiate the new array
divisors = new int [divisibilityCount];
int count = 0;
// do the math again, but now we will assign the values in the new array
for (int x = 1; x <= number; x++) {
if ((number % x) == 0) {
divisors[count] = x;
count++;
}
}
System.out.print("The Number: " + number + " Is A Prime Because It Is" +
" Only Divisible By: ");
// display the values
for (int x = 0; x <= divisors.length - 1; x++) {
System.out.print(divisors[x]);
if (x < divisors.length - 1) {
System.out.print(", ");
}
}
System.out.print("\n");
}
i have two loops, one that will count all the needed values,, then that count will be the new length of
the new allocation of my array.
then on my second loop the values will be assigned in the new allocated array..
is this an efficient way to do my program...?
anyway this is what i want in my program regarding with the array..
1.) the array will not be instantiated if the values are not yet counted
2.) the array length will depend on the number of the values that will be generated..
ofcourse we know that prime numbers are ony divisible by two.. so why didnt just make an array with length of two? i'm just following the case that has given to me,.. and i want to learn this logic...
and i can use this algorithm for some future purposes