C# 找到第一个有50个因子的三角形数?
-----要求修改代码-------- 问题:计算具有50个因子的快速三角级数 详细说明:假设有一个系列C# 找到第一个有50个因子的三角形数?,c#,algorithm,number-theory,C#,Algorithm,Number Theory,-----要求修改代码-------- 问题:计算具有50个因子的快速三角级数 详细说明:假设有一个系列 1 : 1 3 : 1+2 6 : 1+2+3 10 : 1+2+3+4 15 : 1+2+3+4+5 21 : 1+2+3+4+5+6 28 : 1+2+3+4+5+6+7 这里1,3,6,10,15,21,28是三角形级数下的数字 让我们看看这个数字的系数 Number factors Count 1
1 : 1
3 : 1+2
6 : 1+2+3
10 : 1+2+3+4
15 : 1+2+3+4+5
21 : 1+2+3+4+5+6
28 : 1+2+3+4+5+6+7
这里1,3,6,10,15,21,28是三角形级数下的数字
让我们看看这个数字的系数
Number factors Count
1 : 1 1
3 : 1,3 2
6 : 1,2,3,6 4
10 : 1,2,5,10 4
15 : 1,3,5,15 4
21 : 1,3,7,21 4
28 : 1,2,4,7,14,28 6
这里6是第一个三角形数,它有4个因子。
即使10、15、21也有4个因素,但它们不是第一个因素。
这样,让我们把一个数取为2,它有两个因子1和2
数字3相同,也有2个因子,如1和3
但根据问题3,答案不是2,因为2不在三角形序列号列表下,即使它比3快
解决方案:
让我把这个问题分为多个模块
1) 找到三角形级数直到一个数字
2) 将所有标识的数字存储在整数列表中
3) 查找特定数字的因子数
4) 循环遍历三角级数的每一项,找出每个数的因子数
5) 检查计数为50的第一个,然后显示值
6) 写入break语句以仅显示前50个数字
节目
使用系统;
使用System.Collections.Generic;
使用系统集合;
使用System.Linq;
使用系统文本;
命名空间为NumbertRingularSeriesConsolleApp
{
班级计划
{
///
///列出的所有数字都属于三角形系列。
///
///
///
静态列表GetTriangularNumber(整数)
{
List lstTriangularNumber=新列表();
int i;
整数和=0;
int triangularNumber=0;
对于(i=1;i
三角形数#12375=76576500是第一个具有至少500个因子(实际上是576个因子)的数:1、2、3、4、5、6、7、9、10、11、…、19144125、25525500、38288250、76576500
三角形数#156975=12314697300是第一个正好有500个因子的数
解决方案代码本身非常简单,前提是您可以获得除数:
public static long Solution(int factorsCount) {
for (long i = 1; ; ++i) {
long n = i * (i + 1) / 2;
IList<long> factors = GetDivisors(n);
// This code tests if a triangle number has exactly factorsCount factors
// if you want to find out a triangle number which has at least factorsCount factors
// change "==" comparison to ">=" one:
// if (factors.Count >= factorsCount)
if (factors.Count == factorsCount)
return n;
}
}
...
long solution = Solution(50);
公共静态长解决方案(int-factorsCount){
用于(长i=1;;++i){
长n=i*(i+1)/2;
IList因子=GetDivisors(n);
//这段代码测试一个三角形数是否有精确的factorsCount factors
//如果你想找出一个至少有因子的三角形数count factors
//将“==”比较更改为“>=”比较:
//如果(factors.Count>=factorsCount)
如果(factors.Count==factorsCount)
返回n;
}
}
...
长溶液=溶液(50);
如果您没有获取数字因子的例程,可以使用此例程:
// Get prime divisors
private static IList<long> CoreGetPrimeDivisors(long value, IList<int> primes) {
List<long> results = new List<long>();
int v = 0;
long threshould = (long) (Math.Sqrt(value) + 1);
for (int i = 0; i < primes.Count; ++i) {
v = primes[i];
if (v > threshould)
break;
if ((value % v) != 0)
continue;
while ((value % v) == 0) {
value = value / v;
results.Add(v);
}
threshould = (long) (Math.Sqrt(value) + 1);
}
if (value > 1)
results.Add(value);
return results;
}
/// <summary>
/// Get prime divisors
/// </summary>
public static IList<long> GetPrimeDivisors(long value, IList<int> primes) {
if (!Object.ReferenceEquals(null, primes))
return CoreGetPrimeDivisors(value, primes);
List<long> results = new List<long>();
while ((value % 2) == 0) {
results.Add(2);
value = value / 2;
}
while ((value % 3) == 0) {
results.Add(3);
value = value / 3;
}
while ((value % 5) == 0) {
results.Add(5);
value = value / 5;
}
while ((value % 7) == 0) {
results.Add(7);
value = value / 7;
}
int v = 0;
long n = (long) (Math.Sqrt(value) / 6.0 + 1);
long threshould = (long) (Math.Sqrt(value) + 1);
for (int i = 2; i <= n; ++i) {
v = 6 * i - 1;
if ((value % v) == 0) {
while ((value % v) == 0) {
results.Add(v);
value = value / v;
}
threshould = (long) (Math.Sqrt(value) + 1);
}
v = 6 * i + 1;
if ((value % v) == 0) {
while ((value % v) == 0) {
results.Add(v);
value = value / v;
}
threshould = (long) (Math.Sqrt(value) + 1);
}
if (v > threshould)
break;
}
if (value > 1) {
if (results.Count <= 0)
results.Add(value);
else if (value != results[results.Count - 1])
results.Add(value);
}
return results;
}
/// <summary>
/// Get all divisors
/// </summary>
public static IList<long> GetDivisors(long value, IList<int> primes) {
HashSet<long> hs = new HashSet<long>();
IList<long> divisors = GetPrimeDivisors(value, primes);
ulong n = (ulong) 1;
n = n << divisors.Count;
for (ulong i = 1; i < n; ++i) {
ulong v = i;
long p = 1;
for (int j = 0; j < divisors.Count; ++j) {
if ((v % 2) != 0)
p *= divisors[j];
v = v / 2;
}
hs.Add(p);
}
List<long> result = new List<long>();
result.Add(1);
var en = hs.GetEnumerator();
while (en.MoveNext())
result.Add(en.Current);
result.Sort();
return result;
}
/// <summary>
/// Get all divisors
/// </summary>
public static IList<long> GetDivisors(long value) {
return GetDivisors(value, null);
}
//获取素数因子
专用静态IList CoreGetPrimeDivisor(长值,IList素数){
列表结果=新列表();
int v=0;
长阈值=(长)(数学Sqrt(值)+1);
对于(int i=0;i阈值)
打破
如果((值%v)!=0)
继续;
而((值%v)==0){
值=值/v;
结果:添加(v);
}
threshold=(长)(数学Sqrt(值)+1);
}
如果(值>1)
结果:增加(价值);
返回结果;
}
///
///得到素因子
///
公共静态IList GetPrimeDivisors(长值,IList素数){
如果(!Object.ReferenceEquals(null,primes))
返回CoreGetPrimeDivisors(值,素数);
列表结果=新列表();
而((值%2)==0){
结果:增加(2);
值=值/2;
}
而((值%3)==0){
结果:增加(3);
值=值/3;
}
而((值%5)=0){
结果:增加(5);
数值=数值/5;
}
而((值%7)=0){
结果:增加(7);
数值=数值/7;
}
int v=0;
长n=(长)(数学Sqrt(值)/6.0+1);
长阈值=(长)(数学Sqrt(值)+1);
for(int i=2;i阈值)
打破
}
如果(值>1){
如果(results.Count,这是我的答案
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TriangularSeries
{
class MyClass
{
static void Main(string[] args)
{
int result;
TriangularSeries aSeries = new TriangularSeries();
result = aSeries.TSeries();
Console.WriteLine("The first Triangular Series number that has 50Factors is : " + result);
Console.Read();
}
}
//Find the Triangular Series numbers
class TriangularSeries
{
public int TSeries()
{
int fCount = 0, T1 = 1, i = 1, T2 = 0, fval = 0;
while (fCount != 50)
{
i += 1;
T2 = T1 + i;
fCount = CalcFactors(T1);
fval = T1;
T1 = T2;
}
return fval;
}
public int CalcFactors(int num1)
{
List<int> factors = new List<int>();
int max = (int)Math.Sqrt(num1); //round down
for (int factor = 1; factor <= max; ++factor)
{
//test from 1 to the square root, or the int below it, inclusive.
if (num1 % factor == 0)
{
factors.Add(factor);
if (factor != num1 / factor)
{
// Don't add the square root twice!
factors.Add(num1 / factor);
}
}
}
return factors.Count;
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间三角网格
{
类MyClass
{
静态void Main(字符串[]参数)
{
int结果;
TriangularSeries aSeries=新的TriangularSeries();
结果=aSeries.TSeries();
Console.WriteLine(“第一个有50个因子的三角形序列号是:“+result”);
Console.Read();
}
}
//求三角级数
类三角网
{
公共int系列()
{
int fCount=0,T1=1,i=1,T2=0,fval=0;
while(fCount!=50)
{
i+=1;
T2=T1+i;
fCount=计算因子(T1);
fval=T1;
T1=T2;
}
返回fval;
}
公共int计算器(int nu)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TriangularSeries
{
class MyClass
{
static void Main(string[] args)
{
int result;
TriangularSeries aSeries = new TriangularSeries();
result = aSeries.TSeries();
Console.WriteLine("The first Triangular Series number that has 50Factors is : " + result);
Console.Read();
}
}
//Find the Triangular Series numbers
class TriangularSeries
{
public int TSeries()
{
int fCount = 0, T1 = 1, i = 1, T2 = 0, fval = 0;
while (fCount != 50)
{
i += 1;
T2 = T1 + i;
fCount = CalcFactors(T1);
fval = T1;
T1 = T2;
}
return fval;
}
public int CalcFactors(int num1)
{
List<int> factors = new List<int>();
int max = (int)Math.Sqrt(num1); //round down
for (int factor = 1; factor <= max; ++factor)
{
//test from 1 to the square root, or the int below it, inclusive.
if (num1 % factor == 0)
{
factors.Add(factor);
if (factor != num1 / factor)
{
// Don't add the square root twice!
factors.Add(num1 / factor);
}
}
}
return factors.Count;
}
}
}
#include<stdio.h>
int i;
int num1=0,num2=1;
int a[3000];
int tri_series() //This function finds the Triangular series numbers
{
for(i=0;num2<=3000;i++)
{
num1=num1+num2;
a[i]=num1;
num2++;
}
}
int main()
{
tri_series(); //Calling the function tri_series
int num,count;
for(i=0;i<=3000;i++)
{
count=0;
for(num=1;num<=a[i];num++)
{
if(a[i]%num==0) //Finds the factors of each Triangular Series Number
count=count+1;
}
if(count==50) //Break at the first Triangular Series Number having 50 factors
{
printf("%d:%d\t",a[i],count);
break;
}
}
}