Question :
Given a positive integer k, another positive integer n is said to have k-small factors if n can be written as a product u*v where u and v are both less than k. For instance, 20 has 10-small factors since both 4 and 5 are less than 10 and 4*5 = 20. (For the same reason, it is also true to say that 20 has 6-small factors, 7-small factors, 8-small factors, etc). However, 22 does not have 10-small factors since the only way to factor 22 is as 22 = 2 * 11, and 11 is not less than 10.
Write a function hasKSmallFactors with signatuare
boolean hasKSmallFactors(int k, int n)
which returns true if n has k-small factors. The function should return false if either k or n is not positive.
Examples:
hasKSmallFactors(7, 30) is true (since 5*6 = 30 and 5 < 7, 6 < 7).
hasKSmallFactors(6, 14) is false (since the only way to factor 14 is 2*7 = 14 and 7 not less than 6)
hasKSmallFactors(6, 30) is false (since 5*6 = 30, 6 not less than 6; 3 * 10 = 30, 10 not less than 6; 2 * 15 = 30, 15 not less than 6)
Solution :
Given a positive integer k, another positive integer n is said to have k-small factors if n can be written as a product u*v where u and v are both less than k. For instance, 20 has 10-small factors since both 4 and 5 are less than 10 and 4*5 = 20. (For the same reason, it is also true to say that 20 has 6-small factors, 7-small factors, 8-small factors, etc). However, 22 does not have 10-small factors since the only way to factor 22 is as 22 = 2 * 11, and 11 is not less than 10.
Write a function hasKSmallFactors with signatuare
boolean hasKSmallFactors(int k, int n)
which returns true if n has k-small factors. The function should return false if either k or n is not positive.
Examples:
hasKSmallFactors(7, 30) is true (since 5*6 = 30 and 5 < 7, 6 < 7).
hasKSmallFactors(6, 14) is false (since the only way to factor 14 is 2*7 = 14 and 7 not less than 6)
hasKSmallFactors(6, 30) is false (since 5*6 = 30, 6 not less than 6; 3 * 10 = 30, 10 not less than 6; 2 * 15 = 30, 15 not less than 6)
Solution :
public static int hasKSmallFactors(int k, int n) { int result = 0; int u = 1, v = 1; for (int i = 2; i < n; i++) { if (n % i == 0) { u = v; v = i; if (u * v == n) { if (u < k && v < k) { result = 1; break; } } } } return result; }
static boolean hasKSmallFactors(int k, int n)
ReplyDelete{
for(int i=k;i>=2;i--)
{
int r=n%i;
int q=n/i;
if(r==0 && q<k && i<k)
{
return true;
}
}
return false;
}
static boolean haskSmallFactor(int k,int n){
ReplyDeleteint rest=0;
boolean isSmalFactor=false;
for(int i=1;i<=n;i++){
if(n%i==0){
rest=n/i;
if(rest<k && i<k){
isSmalFactor=true;
break;
}else
isSmalFactor=false;
}
}
return isSmalFactor;
}
// O(K) Time | O(1) Space
ReplyDeletestatic boolean hasKSmallFactors(int k, int n) {
if (k < 0 || n < 0)
return false;
boolean result = false;
for (int i = 2; i < k; i++) {
if (n % i == 0) {
if ((n / i) < k)
result = true;
}
}
return result;
}
static int hasKSmallFactors(int k, int n) {
ReplyDeletefor(int i=1; i<n; i++)
{
if(n%i==0){
for(int j=i+1; j<n; j++){
if(n%j==0){
if(i<k && j<k){
if(i*j==n)
return 1;
} }
else break;
} } }
return 0;
}
public static boolean hasKSmallFactors(int k, int n){
ReplyDeleteint factors_count = 0, prev_factor=0;
for(int i=2;i<k;i++){
if(n%i == 0){
factors_count ++;
if(factors_count == 1){
prev_factor = i;
}
else{
if(prev_factor * i == n){
return true;
}
else{
prev_factor = i;
}
}
}
}
return false;
}