[英]Number of NonTrivial Factors in Java
我有一個問題要檢查總計數是否等於該數字的任何因素。我正處於JAVA編程的學習階段。 問題如下:
*
Bishal數是一個數字,使得非平凡因子的數量是該數字的一個因子。 例如,6是Bishal數,因為6具有兩個非平凡因子:2和3.(非平凡因子是1以外的因子和數字)。 因此,6有兩個非平凡因素。 現在,2是因子6.因此,非平凡因子的數量是因子6.因此,6是Bishal數。 另一個Bishal數是30,因為30有2,3,5,6,10,15作為非平凡因素。 因此30有6個非平凡因素。 注意6是因子30.所以30是Bishal數。 然而,21不是Bishal數字。 21的非平凡因子是3和7.因此,非平凡因子的數量是2.注意,2不是21的因子。因此,21不是Bishal數。 編寫一個名為isBishal的函數,如果其整數參數是Bishal數,則返回1,否則返回0。
函數的簽名是int isBishal(int n)
*
我可以創建一個函數。 但我不知道如何用因子檢查總數。 我的解決方案的某些部分如下:
public static int isBishal(int n){
int count=0; //for number of factor of entered number n
for (int i=2; i<n; i++){ //for excluding 1 and itself in factors list
double result=(double)n/i;
if(result==Math.ceil(result)){
int factor=(int) result; //to check factor(one can use reminder 0 case)
count++;
} //closing if clause
} //closing for loop
我在哪里可以將最終計數(即因子總數)與任何因子進行比較? 如果我使用因子等於計數,則計數從1,2,3開始,依此類推。 並且它可以將計數1,2,3左右與因子進行比較。 我需要比較最終計數。 所以我把數量排除在外。 但是因子的范圍恰好在if子句中。 它無法在外循環中進行比較。
有誰請在這個程序中讓我明白.PS: 這個程序不完整,因為我無法比較。
您必須存儲您找到的因子,以檢查非平凡因素的數量是否是一個因素。
例如,您可以使用HashSet
:
public static boolean isBishal(int n) { // I changed the return type to boolean
int count=0;
Set<Integer> factors = new HashSet<>();
for (int i=2; i<n; i++){
if (n % i == 0) { // note this is a simpler way to check if i is a factor of n
factors.add(i);
count++;
}
}
return factors.contains(count);
}
編輯:正如khelwood所建議的那樣,存儲因子的另一種方法是在循環結束時檢查如果count
是n
的因子:
public static boolean isBishal(int n) {
int count=0;
for (int i=2; i<n; i++){
if (n % i == 0) {
count++;
}
}
return (count > 1) && (n % count == 0);
}
我會盡力指導你正確的方向。
public static int isBishal(int n){
int count = 0;
for (int i = 2; i < n; i++){
if(n % i == 0) {
// TODO: We found a factor, insert it in some data structure e.g. in stack or (resizable) array
count++;
}
}
// TODO: loop through the array (or data structure you used) where you stored factors, and check if any of them matches count.
// If none of them match count, return 0, otherwise 1.
}
你開始的方式有兩點。
第一:不要使用double
來操作整數。 要知道一個數是否是另一個的除數,請使用余數運算符%
(如果A % B == 0
, B
是A
的除數)。
第二:你不需要只知道一個數字是否為除數,但你也需要保持它。 您可以使用Set
來保存所有除數。 將其添加到if子句中。
第三:如果將除數保存在Set中,則不需要計數變量來知道存在多少除數。 只需計算除數集中的元素。
第四:一旦你得到了除數,你就可以簡單地遍歷除數,檢查是否有一個除數,其值等於除數的數量。
這是代碼:
public static int isBishal(int n){
Set<Integer> factors = new HashSet<>(); // Create a Set for factors
for (int i = 2; i < n; i++) {
if (n % i == 0) { // CHeck if i is factor of n using %
factors.add(i); // If i is a factor add it to factors
}
}
if (factors.contains(factors.size())) {
return 1; // If a factor is equal to the number of factors return 1
}
return 0; // If none factor equals number of divisors return 0
}
附加說明:可以進行其他優化。 例如,不需要從2循環到n - 1.在n / 2 + 1和n - 1之間不能存在除數,因此可以使用以下條件重寫第一個循環:
for (int i = 2; i <= (n / 2) + 1; i++) {
public static boolean isBishal(int n) {
long factors = IntStream.rangeClosed(2, n/2)
.filter(x -> n % x == 0)
.count();
return factors != 0
&& n % factors == 0;
}
n
所有非平凡因子都在[2, n/2]
范圍內。 這里我們從范圍中按謂詞filter
整數,然后count
它們。
public class Demo{
public static void main(String[] args) {
System.out.println("Result: " + isBishal(21));
}
public static int isBishal(int n) {
int countFactors = 0;
int flag = 0;// False case
for (int i = 2; i < n; i++) {
if (n % i == 0) {
countFactors++;
}
}
if (n % countFactors == 0) {
flag = 1;// True case
}
return flag;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.