0

Let's say I have the number 3294830924.

I want to divide it by prime numbers, 2, 3, 5, 7, 11, etc..

And I want to know which operations gives me modulo 0.

something like:

int[] primeNumbers = ...;
var n = 3294830924;
return primeNumbers.Where(pn=> pn < n).Where(pn=> n % pn == 0)

is there any built in function or method to get the list of prime numbers in C# or a way to calculate this in a mathematical fashion?

Bart Calixto
  • 19,210
  • 11
  • 78
  • 114
  • 1
    Maybe this: http://stackoverflow.com/questions/1042902/most-elegant-way-to-generate-prime-numbers – MakePeaceGreatAgain Jul 17 '15 at 11:10
  • i don't want to generate prime numbers.... but that post give me some light on where to find a prime list :) – Bart Calixto Jul 17 '15 at 11:14
  • If what you search is a prime number list, check here: https://www.mathsisfun.com/numbers/prime-number-lists.html or even http://compoasso.free.fr/primelistweb/page/prime/liste_online_en.php – Bidou Jul 17 '15 at 11:18
  • *is there any built in function or method to get the list of prime numbers in C#* If you don't want to generate prime numbers, that sentence makes no sense. – Yuval Itzchakov Jul 17 '15 at 11:18
  • I'm wondering if there's a `bool IsPrime(int)` or a `int[] Math.Primes` – Bart Calixto Jul 17 '15 at 11:19
  • for example, a function like IsPrime with known of primers beforehand with a hash and some clever manipulation will fit in a framework and I can't find it :D – Bart Calixto Jul 17 '15 at 11:21
  • No, there isn't. But you can see [this](http://stackoverflow.com/questions/15743192/check-if-number-is-prime-number) for one. – Yuval Itzchakov Jul 17 '15 at 11:21
  • Given this sounds like it is just finding prime factors it might be a duplicate of http://stackoverflow.com/questions/5872962/prime-factors-in-c-sharp – Chris Jul 17 '15 at 11:28

2 Answers2

1

You could download, generate or connect to a database with a list of prime numbers and use this list every time you need to do the operation.

For example a list of the first 10,000 primes:

https://primes.utm.edu/lists/small/10000.txt

and first 50 million primes:

https://primes.utm.edu/lists/small/millions/

If these numbers were in a table in a relational database (such as SQL Server Express) then you could use linq to check if the number is prime more efficiently as you can have a clustered index for the prime number.

William Moore
  • 3,844
  • 3
  • 23
  • 41
  • perfect, I don't want to have all so I will have some and then i will generate for bigger numbers. thanks! – Bart Calixto Jul 17 '15 at 11:25
  • I would probably not worry about the space as 1 million primes (in integers) would only use ~4mb, it is the searching which could take some time (although much less than generating the primes). However searching for an element in a table over an index of a few million rows will complete in virtually no time. – William Moore Jul 17 '15 at 11:29
1

You can create a custom function to check Prime ,

private bool IsPrime(int number)
{
if (number < 2) return false;
if (number == 2) return true;
if (number % 2 == 0) return false;
for (int i = 3; i * i <= number; i += 2)
if (number % i == 0) return false;
return true;
}

Then calculate the primes as

var primes =
from number in Enumerable.Range(1, your_number)
where IsPrime(number)
select number;
Tushar Gupta
  • 15,504
  • 1
  • 29
  • 47