C#,楔子数(Sphenic Number)的暴力算法与高效算法源代码

C#,楔子数(Sphenic Number)的暴力算法与高效算法源代码_第1张图片

楔子数(Sphenic Number)来自于一个题目:

Schoolboy Vasya is interested in the problem of distinguishing prime numbers. He has decided to develop his own testing method for it. Unfortunately, the new algorithm has one deficiency – it yields false positive outputs in case with so-called sphenic numbers. For those who does not know: sphenic number is the product of exactly three distinct prime numbers. Help Vasya write a program to distinguish sphenic numbers.

译文:

小学生瓦西亚对区分素数的问题很感兴趣。他决定开发自己的测试方法。不幸的是,新算法有一个缺陷——在使用所谓的sphenic数的情况下,它会产生假阳性输出。对于那些不知道的人来说:Sphenic Number是三个不同素数的乘积。帮助Vasya编写一个程序来计算(分解)sphenic number。

Define:

        Sphenic Number = Prime1 * Prime2 * Prime3

Where:

        Prime1 != Prime2

        Prime2 != Prime3

一、运算效果

C#,楔子数(Sphenic Number)的暴力算法与高效算法源代码_第2张图片

二、暴力求解算法源代码

/// 
/// 最大质数
/// 
private static int primeMax { get; set; } = 10467397 / 6;
/// 
/// 质数总数
/// 
private static int primeCount { get; set; } = 0;
/// 
/// 质数筛(数组)
/// 
private static int[] primeArray { get; set; } = new int[primeMax + 10];

/// 
/// 构造质数(筛)
/// 
public static void SieveInitialize()
{
	primeCount = 0;
	int[] visitArray = new int[primeMax + 10];
	for (int i = 2; i < primeMax; i++)
	{
		if (visitArray[i] == 0)
		{
			primeArray[primeCount++] = i;
			for (int j = 1; j * i <= primeMax; j++)
			{
				visitArray[j * i] = 1;
			}
		}
	}
}

/// 
/// 暴力求解 x 是不是 楔子数
/// 
/// 
/// 
public static bool IsSphenicOriginal(int x)
{
	int i = 0;
	int count = 0;
	for (i = 0; i < primeCount && x > 1; i++)
	{
		if ((x % primeArray[i]) == 0)
		{
			count++;
			x /= primeArray[i];
		}
		if ((x % primeArray[i]) == 0)
		{
			break;
		}
	}
	if (count == 3 && x == 1) // && !(i < cur && n > 1)
	{
		return true;
	}
	return false;
}

三、高效算法

/// 
/// 哈希表
/// 
private Hashtable sphenicHash { get; set; } = new Hashtable();
/// 
/// 保存全部楔子数的列表
/// 
private List sphenicNumbers { get; set; } = new List();
/// 
/// 表达式
/// 
private List sphenicExpressions { get; set; } = new List();

/// 
/// 构造函数
/// 
/// 
public SphenicNumber(int max = 100)
{
	List ps = PrimeList(max);
	for (int i = 0; i < ps.Count - 2; i++)
	{
		for (int j = i + 1; j < ps.Count - 1; j++)
		{
			for (int k = j + 1; k < ps.Count; k++)
			{
				int v = ps[i] * ps[j] * ps[k];
				sphenicNumbers.Add(v);
				sphenicExpressions.Add(ps[i] + "*" + ps[j] + "*" + ps[k]);
				if (!sphenicHash.ContainsKey(v))
				{
					sphenicHash.Add(v, sphenicHash.Count);
				}
			}
		}
	}
}

/// 
/// 总数
/// 
public int Count
{
	get
	{
		return sphenicNumbers.Count;
	}
}

/// 
/// 提取第 index 个楔子数
/// 
/// 
/// 
public int this[int index]
{
	get
	{
		return sphenicNumbers[index];
	}
}

/// 
/// 提取全部楔子数
/// 
/// 
public List ToList()
{
	return (sphenicNumbers);
}

/// 
/// 提取全部表达式列表
/// 
/// 
public List ToExpressionList()
{
	return (sphenicExpressions);
}

/// 
/// 构造不超过 x 的全部质数列表
/// 
/// 
/// 
private List PrimeList(int x = 100)
{
	List list = new List();
	list.Add(2);
	list.Add(3);
	for (int j = 4; j <= x; j++)
	{
		bool isprime = true;
		for (int i = 2; (i * i) <= j; i++)
		{
			if ((j % i) == 0)
			{
				isprime = false;
				break;
			}
		}
		if (isprime)
		{
			list.Add(j);
		}
	}
	return list;
}

/// 
/// 检验 x 是不是楔子数?
/// 
/// 
/// 
public bool IsSphenic(int x)
{
	return sphenicHash.ContainsKey(x);
}

/// 
/// 返回计算公式
/// 
/// 
/// 
public string SphenicExpression(int x)
{
	if (sphenicHash.ContainsKey(x))
	{
		return sphenicExpressions[(int)sphenicHash[x]];
	}
	return "";
}

你可能感兴趣的:(C#算法演义,Algorithm,Recipes,c#,算法,质数)