1. System.Numerics.BigInteger
在C# 4.0的基础类库中,终于有了大数BigInteger,这个大数与其他.net的数值类型不同,它没有最大值和最小值的限制,理论上它可以存无限大的数(取决于你的程序的寻址空间),它与我以前使用的F#的Microsoft.FSharp.Math.BigInt的方法基本一致,但更方便,它可以自动从小数造型成大数,而不需要一再呼叫FromInt32...等等系列方法。BigInteger还提供了很好的构造器,除了接受小数的参数外,还可以接受Byte[]阵列作为参数(在没有大数之前,很多程序就利用byte[]来保存大数),并依照下标顺序构造出一个大数。如
Byte array (lowest index first)
00 10 A5 D4 E8 00
将构造出
Hexadecimal string
E8D4A51000
其他运算方法与别的数值类型类似,请看代码
using System;
using System.Numerics;
using System.Threading;
namespace LucasLehmer
{
class Program
{
static void Main(string[] args)
{
int range = 1000;
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
Parallel.For(2, range, delegate(int i)
{
if (LucasLehmerTest(i))
{
System.Console.WriteLine("M({0})={1}", i, Mersenne(i));
}
});
sw.Stop();
System.Console.WriteLine("It take {0} seconds to caculate Mersenne in {1}", sw.ElapsedMilliseconds/1000, range);
}
public static BigInteger tcs(BigInteger n, BigInteger m, BigInteger result)
{
if(n.IsZero){
return result;
}
return tcs((n - BigInteger.One), m, ((BigInteger.Pow(result, 2) - 2 ) % m));
}
public static BigInteger seq(BigInteger n, BigInteger m)
{
return tcs(n, m, 4 % m);
}
public static BigInteger LucasLehmerSequence(BigInteger n, BigInteger m)
{
if (n==BigInteger.Zero)
return 4% m;
return (BigInteger.Pow(LucasLehmerSequence(n - BigInteger.One, m), 2) - 2) % m;
}
public static BigInteger Mersenne(int n)
{
return BigInteger.Pow(2,n)-BigInteger.One;
}
public static bool LucasLehmerTest(int n){
BigInteger m = Mersenne(n);
return (seq(n - 2, m).IsZero);
//return (LucasLehmerSequence(BigInt.FromInt32(n - 2), m) == BigInt.Zero);
}
}
}
分享到:
相关推荐
Beginning WF: Windows Workflow in .NET 4.0 By Mark Collins Publisher: Apress 2010 | 500 Pages | ISBN: 1430224851 | PDF | 4 MB Windows Workflow Foundation is a ground-breaking addition to the core ...
这两个重要的里程碑包括.NET Framework 2.0 和 .NET Framework 3.5,它们为.NET 4.0奠定了坚实的基础。 - **What is .NET?**:.NET Framework 主要由两大部分组成:公共语言运行时(CLR)和基类库。CLR 是 .NET 的...
You may know what’s happening in C#, but how about the Azure cloud? How is that going to affect your work? What are the limitations of the new pLINQ syntax? What you need is a roadmap. A guide to ...
ASP.NET技术架构和.NET Framework类库 ASP.NET是一种基于微软的.NET Framework的Web应用程序框架,旨在帮助开发者快速构建动态的Web应用程序。为了更好地理解ASP.NET的技术架构,我们需要了解.NET Framework的基本...
What You’ll Learn Know the technologies underpinning Industry 4.0 and AI 2.0 Develop real-time solutions using IoT in Azure Bring the smart capabilities of AI 2.0 into your application using a ...
The most up-to-date and comprehensive introductory ASP.NET book you’ll find on any shelf, Beginning ASP.NET 4 in VB 2010 guides you through Microsoft’s latest technology for building dynamic web ...
What’s in this guide Tools and downloads Learn where to acquire the tools used in this guide. Deploy to App Service Learn the various methods for deploying an ASP.NET Core app to Azure App ...
这一策略主要围绕四个关键点展开:Who(目标人群)、What(产品选择)、Where(营销平台)、How(内容创作)。在当前的市场环境中,社交媒体平台如抖音、快手、B站和小红书等扮演着越来越重要的角色。 首先,我们来...
具体到“种草4.0”的营销策略,报告提出了“人货场”的核心概念,并结合4W1H(What、Why、Who、Where、How)工作方法论来阐释一场成功的营销是如何实现人、货、场的统一融合的。在这一过程中,“人”指目标消费者,...
CN_What's new with vSphere 4 - Technical Training.pdf
CN_What's new with vSphere 4 - Sales Training.pdf
Completely up-to-date, this book fully reflects major language enhancements introduced with the new C# 4.0 and .NET 4.0. When time is of the essence, turn here first: Get answers you can trust and ...
AOP in .NET: Practical Aspect-Oriented Programming 296 pages Publisher: Manning Publications; Pap/Psc edition (June 25, 2013) Language: English ISBN-10: 1617291145 ISBN-13: 978-1617291142 ...
Summary Microservices in .NET Core provides a ...What's Inside Design robust and ops-friendly services Build HTTP APIs with Nancy Expose events via feeds with Nancy Use OWIN middleware for plumbing
what's-this-in-English学习课程.pptx
OWASP ASVS 4.0测试指南 介绍 OWASP ASVS 4.0测试指南是的非正式支持文档,该文档试图描述每个1级控件,不遵守该控件的后果是什么,如何使用已知的开源工具或手动对其进行测试-以及该控件有效的条件。 另外,对于...