开发者

Highest value datatype can store in c#

I am writing a small program for my assignment to find the primitive roots of a prime number. So far, the program works for smaller prime numbers ti开发者_如何学编程ll 13 and gives correct number of roots. But for higher primes numbers, it is showing only fewer primitive roots. And now i got stuck for the prime number 41, shows no primitive roots for it. I used DOUBLE datatype for the calculation, and again tried with the datatype DECIMAL, but no luck. Does anyone know about this kind of problem??? Thank you.


If you're trying to find large integers, have you tried using BigInteger from .NET 4?

Note that storing integers in double is a bad idea - because not every double within its range can be stored exactly. IIRC, decimal can always store integers exactly, but you'll only get up to 28 or 29 digits... and it's still fundamentally a bad idea because you're trying to represent integers.

On the other hand, it's not really clear what you mean by "stuck for the prime number 41". Are you really sure it's a large integer datatype that you're after?


This is more likely an algorithm issue than a data size issue. Your algorithm should never need to do more than multiply two values less than the prime of which you're trying to get the root. Instead of raising the candidate directly to every power less than p, and then taking the result mod p, start with 1, then repeatedly multiply by the value you're testing, and take the result mod p after each step This means that you will never need to work with numbers bigger than p * candidate and will be able to handle quite large numbers using just int or long.


Primes are always whole integers, so there is no need to use a double. If your calculation is failing at 41 the problem has nothing to do with the size of the data type you are using. You'll need to post the code you are using.


WAY back when, I wrote my own prime number thing in C++. None of the available datatypes at the time where big enough - you might need to cruft your own. Which is easy if you grew up with bits, nybbles, bytes and words (hey, as an aside, am I the only one that remembers when a byte was the width of the acumulator and not 8 bits?) ... but crufting your own won't be so easy if you don't have a good grasp on memory and its usage.

Waffling post I know, but you would learn a lot about how computers work if you created your own datatype. Also a lot about C# with you overriding all the operators and such!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜