![]() |
|
|
#34 | |
|
Just call me Henry
"David"
Sep 2007
Cambridge (GMT/BST)
133768 Posts |
Quote:
is there a .exe for his program |
|
|
|
|
|
|
#35 | ||
|
Dec 2005
22×23 Posts |
Quote:
I misunderstood what needed to be NOTed. That will work. As for XAND, that evaluates to 1 if 0 & 0 or 1 & 1 and 0 if 0 & 1 or 1 & 0; In C, the equivalent logical expression is ~(x ^ y). Quote:
Code:
for ( i = num * num >> 1; (i >> 5) < b_num; i += num)
{
arr[i >> 5] &= ~(0x80000000 >> (i & 0x1F));
}
Code:
/* prime.c */
/* Oct 17, 2007. */
/* <my name was here> */
/* Program to find prime numbers */
#include <stdio.h>
#include <stdlib.h>
unsigned int blocks (const unsigned int len);
int bin_arr_prime_fill (unsigned int * const arr, const unsigned int len);
int main (void)
{
unsigned int len, num, bitVal, *arr = NULL;
printf ("\nFind prime numbers smaller than \n");
scanf("%i", &len);
if (len < 1)
{
return 0;
}
arr = (unsigned int*)calloc (blocks(len), sizeof(unsigned int));
bin_arr_prime_fill(arr, len);
if (len >= 2)
{
printf("Number 2 is a prime number\n");
}
/* Loop through Prime Array and Output Primes */
for ( num = 1; num <= len ; ++arr )
{
for ( bitVal = 0x80000000; bitVal > 0 && num <= len; bitVal >>= 1,
num += 2 )
{
if ((*arr & bitVal) == bitVal)
{
printf("Number %i is a prime number\n", num);
}
}
}
free(arr - blocks(len));
main();
return 0;
}
unsigned int blocks (const unsigned int len)
{
return ( len % (sizeof(unsigned int) << 4) == 0 ) ?
(len / (sizeof(unsigned int) << 4)) :
(len / (sizeof(unsigned int) << 4) + 1);
}
int bin_arr_prime_fill (unsigned int * const arr, const unsigned int len)
{
unsigned int num, i, b_num = blocks(len);
for ( num = 0 ; num < b_num ; ++num )
{
arr[num] = 0xFFFFFFFF;
}
arr[0] &= ~0x80000000;
/* num is the number being checked for primiality */
for ( num = 3; num * num <= len ; num += 2 )
{
if ((arr[num >> 6] & (0x80000000 >> ((num >> 1) & 0x1F))) == 0)
{
continue;
}
for ( i = num * num >> 1 ; (i >> 5) < b_num ; i += num )
{
arr[i >> 5] &= ~(0x80000000 >> (i & 0x1F));
}
}
return 0;
}
|
||
|
|
|
|
|
#36 | |
|
Feb 2006
Denmark
2×5×23 Posts |
Quote:
Computing billions of primes from 0 is so easy that it's redone by many people each time the primes are used for something. If you really want a program which just computes primes without doing anything other than writing them then there is a .exe in http://hjem.get2net.dk/jka/math/primeprint.zip. The used sieve is relatively slow and only works to 10^12 but it probably won't take long to create as large a file as you want to use space on. |
|
|
|
|
![]() |
| Thread Tools | |
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Finding VERY large primes | c10ck3r | Information & Answers | 34 | 2012-08-29 16:47 |
| Why arent there many softwares for finding Huge Primes | blistervol | Math | 2 | 2012-08-20 17:26 |
| Best Work for Finding Primes | Unregistered | Information & Answers | 9 | 2012-06-24 13:50 |
| Finding primes using modular stacking | goatboy | Math | 1 | 2007-12-07 12:30 |
| Finding primes with a PowerPC | rogue | Lounge | 4 | 2005-07-12 12:31 |