给定一个数字N和一个数字K。任务是找到可被K整除的N个除数。这里K是一个始终小于或等于√(N)的数字。
例子:
Input: N = 12, K = 3
Output: 3
Input: N = 8, K = 2
Output: 3
简单方法:一种简单方法是检查从1到N的所有数字,并检查是否有任何数字是N的除数并且可以被K整除。计算满足两个条件的小于N的数字。
下面是上述方法的实现:
C++
// C++ program to count number of divisors
// of N which are divisible by K
#include
using namespace std;
// Function to count number of divisors
// of N which are divisible by K
int countDivisors(int n, int k)
{
// Variable to store
// count of divisors
int count = 0, i;
// Traverse from 1 to n
for (i = 1; i <= n; i++) {
// increase the count if both
// the conditions are satisfied
if (n % i == 0 && i % k == 0) {
count++;
}
}
return count;
}
// Driver code
int main()
{
int n = 12, k = 3;
cout << countDivisors(n, k);
return 0;
}
Java
// Java program to count number of divisors
// of N which are divisible by K
import java.io.*;
class GFG {
// Function to count number of divisors
// of N which are divisible by K
static int countDivisors(int n, int k)
{
// Variable to store
// count of divisors
int count = 0, i;
// Traverse from 1 to n
for (i = 1; i <= n; i++) {
// increase the count if both
// the conditions are satisfied
if (n % i == 0 && i % k == 0) {
count++;
}
}
return count;
}
// Driver code
public static void main (String[] args) {
int n = 12, k = 3;
System.out.println(countDivisors(n, k));
}
}
// This code is contributed by shashank..
Python3
# Python program to count number
# of divisors of N which are
# divisible by K
# Function to count number of divisors
# of N which are divisible by K
def countDivisors(n, k) :
# Variable to store
# count of divisors
count = 0
# Traverse from 1 to n
for i in range(1, n + 1) :
# increase the count if both
# the conditions are satisfied
if (n % i == 0 and i % k == 0) :
count += 1
return count
# Driver code
if __name__ == "__main__" :
n, k = 12, 3
print(countDivisors(n, k))
# This code is contributed by ANKITRAI1
C#
// C# program to count number
// of divisors of N which are
// divisible by K
using System;
class GFG
{
// Function to count number
// of divisors of N which
// are divisible by K
static int countDivisors(int n, int k)
{
// Variable to store
// count of divisors
int count = 0, i;
// Traverse from 1 to n
for (i = 1; i <= n; i++)
{
// increase the count if both
// the conditions are satisfied
if (n % i == 0 && i % k == 0)
{
count++;
}
}
return count;
}
// Driver code
public static void Main ()
{
int n = 12, k = 3;
Console.WriteLine(countDivisors(n, k));
}
}
// This code is contributed by Shashank
PHP
C++
// C++ program to count number of divisors
// of N which are divisible by K
#include
using namespace std;
// Function to count number of divisors
// of N which are divisible by K
int countDivisors(int n, int k)
{
// integer to count the divisors
int count = 0, i;
// Traverse from 1 to sqrt(N)
for (i = 1; i <= sqrt(n); i++)
{
// Check if i is a factor
if (n % i == 0)
{
// increase the count if i
// is divisible by k
if (i % k == 0)
{
count++;
}
// (n/i) is also a factor
// check whether it is divisible by k
if ((n / i) % k == 0)
{
count++;
}
}
}
i--;
// If the number is a perfect square
// and it is divisible by k
if ((i * i == n) && (i % k == 0))
{
count--;
}
return count;
}
// Driver code
int main()
{
int n = 16, k = 4;
// Function Call
cout << countDivisors(n, k);
return 0;
}
Java
// Java program to count number of divisors
// of N which are divisible by K
import java.io.*;
class GFG {
// Function to count number of divisors
// of N which are divisible by K
static int countDivisors(int n, int k)
{
// integer to count the divisors
int count = 0, i;
// Traverse from 1 to sqrt(N)
for (i = 1; i <= Math.sqrt(n); i++)
{
// Check if i is a factor
if (n % i == 0)
{
// increase the count if i
// is divisible by k
if (i % k == 0)
{
count++;
}
// (n/i) is also a factor
// check whether it is divisible by k
if ((n / i) % k == 0)
{
count++;
}
}
}
i--;
// If the number is a perfect square
// and it is divisible by k
if ((i * i == n) && (i % k == 0))
{
count--;
}
return count;
}
// Driver code
public static void main (String[] args)
{
int n = 16, k = 4;
System.out.println( countDivisors(n, k));
}
}
//This Code is Contributed by akt_mit
Python 3
# Python 3 program to count number of
# divisors of N which are divisible by K
import math
# Function to count number of divisors
# of N which are divisible by K
def countDivisors(n, k):
# integer to count the divisors
count = 0
# Traverse from 1 to sqrt(N)
for i in range(1, int(math.sqrt(n)) + 1):
# Check if i is a factor
if (n % i == 0) :
# increase the count if i
# is divisible by k
if (i % k == 0) :
count += 1
# (n/i) is also a factor check
# whether it is divisible by k
if ((n // i) % k == 0) :
count += 1
# If the number is a perfect square
# and it is divisible by k
# if i is sqrt reduce by 1
if ((i * i == n) and (i % k == 0)) :
count -= 1
return count
# Driver code
if __name__ == "__main__":
n = 16
k = 4
print(countDivisors(n, k))
# This code is contributed
# by ChitraNayal
C#
// C# program to count number of divisors
// of N which are divisible by K
using System;
class GFG
{
// Function to count number of divisors
// of N which are divisible by K
static int countDivisors(int n, int k)
{
// integer to count the divisors
int count = 0, i;
// Traverse from 1 to sqrt(N)
for (i = 1; i <= Math.Sqrt(n); i++)
{
// Check if i is a factor
if (n % i == 0)
{
// increase the count if i
// is divisible by k
if (i % k == 0)
{
count++;
}
// (n/i) is also a factor check
// whether it is divisible by k
if ((n / i) % k == 0)
{
count++;
}
}
}
i--;
// If the number is a perfect square
// and it is divisible by k
if ((i * i == n) && (i % k == 0))
{
count--;
}
return count;
}
// Driver code
static public void Main ()
{
int n = 16, k = 4;
Console.WriteLine( countDivisors(n, k));
}
}
// This code is contributed by ajit
PHP
输出
3
时间复杂度:O(N)
高效的方法:想法是从1到<√(N)循环运行,并检查数字是否为N的因数并且可以被K整除,我们还将检查(N / i)是否可以被K整除。 。如果i是N的因数,则(N / i)也将是N的因数。
下面是上述方法的实现:
C++
// C++ program to count number of divisors
// of N which are divisible by K
#include
using namespace std;
// Function to count number of divisors
// of N which are divisible by K
int countDivisors(int n, int k)
{
// integer to count the divisors
int count = 0, i;
// Traverse from 1 to sqrt(N)
for (i = 1; i <= sqrt(n); i++)
{
// Check if i is a factor
if (n % i == 0)
{
// increase the count if i
// is divisible by k
if (i % k == 0)
{
count++;
}
// (n/i) is also a factor
// check whether it is divisible by k
if ((n / i) % k == 0)
{
count++;
}
}
}
i--;
// If the number is a perfect square
// and it is divisible by k
if ((i * i == n) && (i % k == 0))
{
count--;
}
return count;
}
// Driver code
int main()
{
int n = 16, k = 4;
// Function Call
cout << countDivisors(n, k);
return 0;
}
Java
// Java program to count number of divisors
// of N which are divisible by K
import java.io.*;
class GFG {
// Function to count number of divisors
// of N which are divisible by K
static int countDivisors(int n, int k)
{
// integer to count the divisors
int count = 0, i;
// Traverse from 1 to sqrt(N)
for (i = 1; i <= Math.sqrt(n); i++)
{
// Check if i is a factor
if (n % i == 0)
{
// increase the count if i
// is divisible by k
if (i % k == 0)
{
count++;
}
// (n/i) is also a factor
// check whether it is divisible by k
if ((n / i) % k == 0)
{
count++;
}
}
}
i--;
// If the number is a perfect square
// and it is divisible by k
if ((i * i == n) && (i % k == 0))
{
count--;
}
return count;
}
// Driver code
public static void main (String[] args)
{
int n = 16, k = 4;
System.out.println( countDivisors(n, k));
}
}
//This Code is Contributed by akt_mit
的Python 3
# Python 3 program to count number of
# divisors of N which are divisible by K
import math
# Function to count number of divisors
# of N which are divisible by K
def countDivisors(n, k):
# integer to count the divisors
count = 0
# Traverse from 1 to sqrt(N)
for i in range(1, int(math.sqrt(n)) + 1):
# Check if i is a factor
if (n % i == 0) :
# increase the count if i
# is divisible by k
if (i % k == 0) :
count += 1
# (n/i) is also a factor check
# whether it is divisible by k
if ((n // i) % k == 0) :
count += 1
# If the number is a perfect square
# and it is divisible by k
# if i is sqrt reduce by 1
if ((i * i == n) and (i % k == 0)) :
count -= 1
return count
# Driver code
if __name__ == "__main__":
n = 16
k = 4
print(countDivisors(n, k))
# This code is contributed
# by ChitraNayal
C#
// C# program to count number of divisors
// of N which are divisible by K
using System;
class GFG
{
// Function to count number of divisors
// of N which are divisible by K
static int countDivisors(int n, int k)
{
// integer to count the divisors
int count = 0, i;
// Traverse from 1 to sqrt(N)
for (i = 1; i <= Math.Sqrt(n); i++)
{
// Check if i is a factor
if (n % i == 0)
{
// increase the count if i
// is divisible by k
if (i % k == 0)
{
count++;
}
// (n/i) is also a factor check
// whether it is divisible by k
if ((n / i) % k == 0)
{
count++;
}
}
}
i--;
// If the number is a perfect square
// and it is divisible by k
if ((i * i == n) && (i % k == 0))
{
count--;
}
return count;
}
// Driver code
static public void Main ()
{
int n = 16, k = 4;
Console.WriteLine( countDivisors(n, k));
}
}
// This code is contributed by ajit
的PHP
输出
3
时间复杂度:O(√(n))