Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
October 5th, 2004, 06:09 AM
 huimin0116
Registered User

Join Date: Sep 2004
Posts: 14
Time spent in forums: < 1 sec
Reputation Power: 0
Twin Primes in C Program

Hi all,
i have designed a program that detects the prime numbers
Eg: if number is 15
Primes are 3,5,7,11,13
However i need to group the primes i have into twin primes.
and the OUTPUT I should get is:

3 and 5 are twin primes
5 and 7 are twin primes
11 and 13 are twin primes
3 pairs of twin primes have bee found under 15.
End of output

Kindly advise me how to write command to group the primes into twin primes.
Below is my source code:
[CODE]
PHP Code:
``` #include <stdio.h> main (){     int n, possible_prime,divisor,theprime;     FILE * fout;     fout = fopen("prime.txt", "w");     printf("What number do we want to find all the twin primes under?");     scanf("%d", &n);     for (possible_prime=3; possible_prime <=n; possible_prime++){         for (divisor=2; divisor < possible_prime; divisor++)             if (possible_prime % divisor == 0)                 break;             if(divisor == possible_prime)             fprintf(fout, "%d \n", possible_prime);     } }  ```

#2
October 5th, 2004, 07:53 AM
 Druce
Contributing User

Join Date: Sep 2004
Posts: 36
Time spent in forums: 8 m 6 sec
Reputation Power: 9
What you needed to say was that twin primes differ by two. If you store the previous prime, when you find another one you can check if they differ by two, and if they do print them out, then update your previous prime.

PHP Code:
``` #include <stdio.h>  void main (void){      int n, possible_prime,divisor,previousPrime;      FILE * fout;      fout = fopen("prime.txt", "w");      printf("What number do we want to find all the twin primes under?");      scanf("%d", &n);      previousPrime = 0;     for (possible_prime=3; possible_prime <=n; possible_prime++){          for (divisor=2; divisor < possible_prime; divisor++)          {             if (possible_prime % divisor == 0)                  break;          }         if (divisor == possible_prime)          {             if(possible_prime-previousPrime == 2)                 fprintf(fout, "%d and %d are twin primes\n", previousPrime, possible_prime);              previousPrime = possible_prime;         }     }  }  ```

#3
October 5th, 2004, 08:17 AM
 mitakeet
I'm Baaaaaaack!

Join Date: Jul 2003
Location: Maryland
Posts: 5,538
Time spent in forums: 2 Weeks 4 Days 2 h 38 m 46 sec
Reputation Power: 242
I modified my code from http://forums.devshed.com/t125318/s.html to make this:

Code:
```#include <iostream>

class BitMap{
unsigned int m_intHowHigh2Go, m_intEntries, m_intBitsPerEntry;
public:
BitMap(unsigned int HowHighToLook){
m_intHowHigh2Go = HowHighToLook;
m_intBitsPerEntry = sizeof(unsigned long) * 8;
m_intEntries = m_intHowHigh2Go / m_intBitsPerEntry;
m_intEntries++;
for (unsigned int i=0; i<m_intEntries; i++)
};
~BitMap(){
};
void setBit(unsigned int bit2set, bool set){//if set = true, bit = 1
if (bit2set > m_intHowHigh2Go)
throw std::runtime_error("Trying to set a bit that is out of range");
int entry = bit2set / m_intBitsPerEntry;
int offset = bit2set % m_intBitsPerEntry;
unsigned long setBit = 1;
setBit <<= offset - 1;
if (set){
}else{
}
};
bool getBit(unsigned int bit2get){//return true if 1
if (bit2get > m_intHowHigh2Go)
throw std::runtime_error("Trying to get a bit that is out of range");
int entry = bit2get / m_intBitsPerEntry;
int offset = bit2get % m_intBitsPerEntry;
int check = 1;
check <<= offset-1;
if (m_lptrBitMask[entry] & check) return true;
return false;
};
};

using namespace std;

int main(void) {
int i, j;
const int Max2Check = 32000;
BitMap myBitMap(Max2Check);
bool isPrime;

try {
//set 2 manually, less logic this way
myBitMap.setBit(2, true);
cerr << "Calculating primes...\n";
for (i=1; i<=Max2Check; i++){
isPrime = true;
if (i%2 == 0) continue;//no need to check even numbers
for (j=2; j<i; j++){
if (myBitMap.getBit(j)){
if (i%j == 0){
isPrime = false;
break;
}
}
}
if (isPrime)
myBitMap.setBit(i, true);
}

cerr << "Print out twin prime numbers:\n";
for (i=3; i<=Max2Check; i++){
if (myBitMap.getBit(i) && myBitMap.getBit(i-2))
cout << i-2 << "\t" << i << endl;
}
}catch (exception &e){
cerr << "Exception: " << e.what() << endl;
}

return 0;
}```

Except for calling 1 a prime (haven't tried to figure out why), the values I spot checked match what I find at http://www.utm.edu/research/primes/...all/1ktwins.txt.
__________________

My blog, The Fount of Useless Information http://sol-biotech.com/wordpress/
Free code: http://sol-biotech.com/code/.
Secure Programming: http://sol-biotech.com/code/SecProgFAQ.html.
Performance Programming: http://sol-biotech.com/code/PerformanceProgramming.html.

It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
--Me, I just made it up

The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
--George Bernard Shaw

Last edited by mitakeet : October 5th, 2004 at 08:26 AM. Reason: Changed malloc to new; free to delete.

#4
October 7th, 2004, 02:07 AM
 huimin0116
Registered User

Join Date: Sep 2004
Posts: 14
Time spent in forums: < 1 sec
Reputation Power: 0
counter to be included

Hi,

i need to include a counter to count the number of pairs of twin primes. When i enter n=15, the counter register some weird readings. Kindly let me know what went wrong.

thanks!
PHP Code:
``` #include <stdio.h> main () {     int n, possible_prime,divisor,previousPrime,count;     printf("What number do we want to find all the twin primes under?");     scanf("%d", &n);     previousPrime = 0;     for (possible_prime=3; possible_prime <=n; possible_prime++){         for (divisor=2; divisor < possible_prime; divisor++)         {             if (possible_prime % divisor == 0)                 break;         }         if (divisor == possible_prime)         {             if(possible_prime-previousPrime == 2)                 count++;                 printf("%d and %d are twin primes\n", previousPrime, possible_prime);                 previousPrime = possible_prime;         }     }             printf("%d pairs of twin primes", count); }  ```

#5
October 7th, 2004, 06:43 AM
 Druce
Contributing User

Join Date: Sep 2004
Posts: 36
Time spent in forums: 8 m 6 sec
Reputation Power: 9
Doesn't look like you initialized your counter:

count = 0;

So, count would start with some garbage... maybe for example, -858993460, so you would be incrementing that garbage number.

#6
October 7th, 2004, 06:48 AM
 Druce
Contributing User

Join Date: Sep 2004
Posts: 36
Time spent in forums: 8 m 6 sec
Reputation Power: 9
Also, an if only applies to the first statement below it unless you use a block ( {....}), in this case you want to print the statement and increase the count only when the if statement is true. But, you want to always update the previous prime when you find one.

Your code should be something like...

PHP Code:
``` if (divisor == possible_prime)          {              if(possible_prime-previousPrime == 2)              {                 count++;                  printf("%d and %d are twin primes\n", previousPrime, possible_prime);              }             previousPrime = possible_prime;          }  ```

Sorry, i forgot something when i posted my code, your for loop should start at 2, not 3, since 2 is the first prime number. It won't affect your output though, since it only cares about twin primes.

for (possible_prime=2; possible_prime <=n; possible_prime++)

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Twin Primes in C Program