给定数字N,找到除数超过N的第一个三角数。三角数是自然数之和,即x *(x + 1)/ 2的形式。前几个三角形数字是1、3、6、10、15、21、28 …
例子:
Input: N = 2
Output: 6
6 is the first triangular number with more than 2 factors.
Input: N = 4
Output: 28
一个幼稚的解决方案是对每个三角数进行迭代,并使用Sieve方法计算除数。在任何时候,如果除数的数量超过给定的数量N,我们就会得到答案。如果具有大于N个除数的三角数为X,则时间复杂度将为O(X * sqrt(X)),因为在较大的三角数的情况下无法对素数进行预处理。为了更有效地解决问题,天真的解决方案很重要。
一个有效的解决方案是利用三角数的公式为x *(x + 1)/ 2的事实。我们将使用的属性是k和k + 1是互质数。我们知道两个互素有一组独特的素因。当X为偶数和奇数时,将有两种情况。
- 当X为偶数时,则将X / 2和(X + 1)视为要找出其素因数分解的两个数字。
- 当X为奇数时,则X和(X + 1)/ 2将被视为要找出其素因数分解的两个数字。
因此,该问题已减少到仅找出较小数字的质因数分解上,从而显着降低了时间复杂度。我们可以在后续迭代中为x + 1重用素数分解,因此在每次迭代中分解一个数字就可以。迭代直到除数的数量超过N并考虑偶数和奇数的情况将为我们提供答案。
下面是上述方法的实现。
C++
// C++ efficient program for counting the
// number of numbers <=N having exactly
// 9 divisors
#include
using namespace std;
const int MAX = 100000;
// sieve method for prime calculation
bool prime[MAX + 1];
// Function to mark the primes
void sieve()
{
memset(prime, true, sizeof(prime));
// mark the primes
for (int p = 2; p * p < MAX; p++)
if (prime[p] == true)
// mark the factors of prime as non prime
for (int i = p * 2; i < MAX; i += p)
prime[i] = false;
}
// Function for finding no. of divisors
int divCount(int n)
{
// Traversing through all prime numbers
int total = 1;
for (int p = 2; p <= n; p++) {
if (prime[p]) {
// calculate number of divisor
// with formula total div =
// (p1+1) * (p2+1) *.....* (pn+1)
// where n = (a1^p1)*(a2^p2)....
// *(an^pn) ai being prime divisor
// for n and pi are their respective
// power in factorization
int count = 0;
if (n % p == 0) {
while (n % p == 0) {
n = n / p;
count++;
}
total = total * (count + 1);
}
}
}
return total;
}
// Function to find the first triangular number
int findNumber(int n)
{
if (n == 1)
return 3;
// initial number
int i = 2;
// initial count of divisors
int count = 0;
// prestore the value
int second = 1;
int first = 1;
// iterate till we get the first triangular number
while (count <= n) {
// even
if (i % 2 == 0) {
// function call to count divisors
first = divCount(i + 1);
// multiply with previous value
count = first * second;
}
// odd step
else {
// function call to count divisors
second = divCount((i + 1) / 2);
// multiply with previous value
count = first * second;
}
i++;
}
return i * (i - 1) / 2;
}
// Driver Code
int main()
{
int n = 4;
// Call the sieve function for prime
sieve();
cout << findNumber(n);
return 0;
}
Java
// Java efficient program for counting the
// number of numbers <=N having exactly
// 9 divisors
public class GFG {
final static int MAX = 100000;
// sieve method for prime calculation
static boolean prime[] = new boolean [MAX + 1];
// Function to mark the primes
static void sieve()
{
for(int i = 0 ; i <= MAX ; i++)
prime[i] = true;
// mark the primes
for (int p = 2; p * p < MAX; p++)
if (prime[p] == true)
// mark the factors of prime as non prime
for (int i = p * 2; i < MAX; i += p)
prime[i] = false;
}
// Function for finding no. of divisors
static int divCount(int n)
{
// Traversing through all prime numbers
int total = 1;
for (int p = 2; p <= n; p++) {
if (prime[p]) {
// calculate number of divisor
// with formula total div =
// (p1+1) * (p2+1) *.....* (pn+1)
// where n = (a1^p1)*(a2^p2)....
// *(an^pn) ai being prime divisor
// for n and pi are their respective
// power in factorization
int count = 0;
if (n % p == 0) {
while (n % p == 0) {
n = n / p;
count++;
}
total = total * (count + 1);
}
}
}
return total;
}
// Function to find the first triangular number
static int findNumber(int n)
{
if (n == 1)
return 3;
// initial number
int i = 2;
// initial count of divisors
int count = 0;
// prestore the value
int second = 1;
int first = 1;
// iterate till we get the first triangular number
while (count <= n) {
// even
if (i % 2 == 0) {
// function call to count divisors
first = divCount(i + 1);
// multiply with previous value
count = first * second;
}
// odd step
else {
// function call to count divisors
second = divCount((i + 1) / 2);
// multiply with previous value
count = first * second;
}
i++;
}
return i * (i - 1) / 2;
}
public static void main(String args[])
{
int n = 4;
// Call the sieve function for prime
sieve();
System.out.println(findNumber(n));
}
// This Code is contributed by ANKITRAI1
}
Python3
# Python 3 efficient program for counting the
# number of numbers <=N having exactly
# 9 divisors
from math import sqrt
MAX = 100000
prime = [ True for i in range(MAX + 1)]
# Function to mark the primes
def sieve():
# mark the primes
k = int(sqrt(MAX))
for p in range(2,k,1):
if (prime[p] == True):
# mark the factors of prime as non prime
for i in range(p * 2,MAX,p):
prime[i] = False
# Function for finding no. of divisors
def divCount(n):
# Traversing through all prime numbers
total = 1
for p in range(2,n+1,1):
if (prime[p]):
# calculate number of divisor
# with formula total div =
# (p1+1) * (p2+1) *.....* (pn+1)
# where n = (a1^p1)*(a2^p2)....
# *(an^pn) ai being prime divisor
# for n and pi are their respective
# power in factorization
count = 0
if (n % p == 0):
while (n % p == 0):
n = n / p
count += 1
total = total * (count + 1)
return total
# Function to find the first triangular number
def findNumber(n):
if (n == 1):
return 3
# initial number
i = 2
# initial count of divisors
count = 0
# prestore the value
second = 1
first = 1
# iterate till we get the first triangular number
while (count <= n):
# even
if (i % 2 == 0):
# function call to count divisors
first = divCount(i + 1)
# multiply with previous value
count = first * second
# odd step
else:
# function call to count divisors
second = divCount(int((i + 1) / 2))
# multiply with previous value
count = first * second
i += 1
return i * (i - 1) / 2
# Driver Code
if __name__ == '__main__':
n = 4
# Call the sieve function for prime
sieve()
print(int(findNumber(n)))
# This code is contributed by
# Surendra_Gangwar
C#
// C# efficient program for counting the
// number of numbers <=N having exactly
// 9 divisors
using System;
public class GFG {
static int MAX = 100000;
// sieve method for prime calculation
static bool[] prime = new bool [MAX + 1];
// Function to mark the primes
static void sieve()
{
for(int i = 0 ; i <= MAX ; i++)
prime[i] = true;
// mark the primes
for (int p = 2; p * p < MAX; p++)
if (prime[p] == true)
// mark the factors of prime as non prime
for (int i = p * 2; i < MAX; i += p)
prime[i] = false;
}
// Function for finding no. of divisors
static int divCount(int n)
{
// Traversing through all prime numbers
int total = 1;
for (int p = 2; p <= n; p++) {
if (prime[p]) {
// calculate number of divisor
// with formula total div =
// (p1+1) * (p2+1) *.....* (pn+1)
// where n = (a1^p1)*(a2^p2)....
// *(an^pn) ai being prime divisor
// for n and pi are their respective
// power in factorization
int count = 0;
if (n % p == 0) {
while (n % p == 0) {
n = n / p;
count++;
}
total = total * (count + 1);
}
}
}
return total;
}
// Function to find the first triangular number
static int findNumber(int n)
{
if (n == 1)
return 3;
// initial number
int i = 2;
// initial count of divisors
int count = 0;
// prestore the value
int second = 1;
int first = 1;
// iterate till we get the first triangular number
while (count <= n) {
// even
if (i % 2 == 0) {
// function call to count divisors
first = divCount(i + 1);
// multiply with previous value
count = first * second;
}
// odd step
else {
// function call to count divisors
second = divCount((i + 1) / 2);
// multiply with previous value
count = first * second;
}
i++;
}
return i * (i - 1) / 2;
}
public static void Main()
{
int n = 4;
// Call the sieve function for prime
sieve();
Console.Write(findNumber(n));
}
}
PHP
Javascript
输出:
28
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。