Question Does C# contain methods that calculate number properties?

glasswizzard

Well-known member
Joined
Nov 22, 2019
Messages
126
Programming Experience
Beginner
Does C# have methods that will tell me about certain properties of numbers?
I'd like to know four things about a number, if it's prime (if so what prime number), if its triangular (if so what triangular number), if it's square (and what square it is) and also the same for the Fibonacci sequence.
 
No. Not built into the C# language. It also is not built into the .NET Framework. You could use Math.Sqrt() and check to see if the result is an integer, but nothing that will check if a number is square.
 
Is this a stupid plan?

What I'm gonna do is write a small program that will test the numbers from 0 to I don't know, something high anyway. When it finds a prime number it will write "PrimesList.Add(#);" to a text file (# will be whatever prime it found). This way I can paste that huge list into my other program without having to manually write it (and without having to have it calculated every time the program starts).

It will give me the functionality I'm looking for, not only can I easily test if a number is prime (by finding it in the list) I can also use it's index in the list to get what prime number it is.

I'm gonna do this also for the tris, squares, and fibs. Am I doing something stupid here? Or is there a better way?
 
Unless you are doing the sequence generation to exercise your programming skills, why not just download the list of sequences you are interested in from The On-Line Encyclopedia of Integer Sequences® (OEIS®) and truncating the list down to what fits into the C# native numeric types range?

Next, how will you ensure that list always travels with your code? I suggest storing the lists as resources in your assembly. This is to avoid having a bunch of large C# files that just declare arrays of integers. Granted, that the latter idea follows the KISS principle, the former idea is what resources streams in the .NET Framework are for: holding data that is static, read-only, and potentially only accessed occasionally. (Resources are also meant to handle localized data, but no one said that is the only thing they should be used for. Notice that program icons are stored as resources.)

Welcome to programming. You are now discovering the time vs. space trade-offs.
 
Personally, I think that the only one that maybe worth downloading a list for are the primes. Checking for perfect squares, triangular numbers, and Fibonacci numbers could be checked algebraically.
 
Thanks for the advice, it would definitely be better to download a list. Are you sure that website you linked can provide a big list? I searched for a sequence of the first ten primes but it only shows me a small list, up to 271.

Edit: It would be very simple to write a program that would give me this list so I'll do that since I can't figure out how to use that site properly.
 
Last edited:
After playing around with this some more, it's taking my laptop less than a millisecond to test if a number is prime. It was taking longer to load a list of prime numbers from a file and to check to see if the number is in the list.

Consider the following code:
C#:
class Program
{
    bool IsPrime(int n)
    {
        if (n % 2 == 0)
            return false;

        int maxFactor = (int) Math.Sqrt(n);
        for(int i = 3; i <= maxFactor; i += 2)
        {
            if (n % i == 0)
                return false;
        }
        return true;
    }

    void Run()
    {
        var stopwatch = new Stopwatch();
        stopwatch.Start();
        bool isPrime = IsPrime(int.MaxValue);
        stopwatch.Stop();
        Console.WriteLine(stopwatch.ElapsedMilliseconds);
        Console.ReadKey();
    }

    static void Main()
    {
        new Program().Run();
    }
}
 
Last edited:
Back
Top Bottom