Prime numbers

Reply
 
LinkBack Thread Tools
  #1 (permalink)  
Old 06-13-2005
Boerseun's Avatar
Holy cow!
Hypography Staff Member
Moderator

Join Date: May 2005
Location: Hartbeespoort, South Africa
Posts: 4,658
Blog Entries: 3
Boerseun has a reputation beyond reputeBoerseun has a reputation beyond reputeBoerseun has a reputation beyond reputeBoerseun has a reputation beyond reputeBoerseun has a reputation beyond reputeBoerseun has a reputation beyond reputeBoerseun has a reputation beyond reputeBoerseun has a reputation beyond reputeBoerseun has a reputation beyond reputeBoerseun has a reputation beyond reputeBoerseun has a reputation beyond repute
Prime numbers

Does anybody here have an algorythm/procedure for calculating primes?
I want to code an encryption thingy that's going to depend on me being able to figure out how to code primes into it.
__________________
Hypography Forums Moderator

IIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIII



Bovinely blessed be thee.
Reply With Quote
  #2 (permalink)  
Old 06-14-2005
Qfwfq's Avatar
Exhausted Gondolier
Hypography Staff Member
Administrator

Join Date: Feb 2005
Location: having a rest
Posts: 4,438
Qfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud of
Question Re: Prime numbers

How large?

I once posted and attachment that's very quick for unsigned longs in C, which means up to 4,294,967,295 but for encryption you might be needing larger values. What I posted can also be useful if you need to find the prime factors of larger numbers that don't have any higher prime factors.
__________________
Who's afraid of the Big Black Hole?????

Go Black Hole! W the Black Hole!

Hasta que el agujero negro nos traga, siempre!

Hypography Forum PITA...... er, Administrator.
Reply With Quote
  #3 (permalink)  
Old 06-14-2005
Qfwfq's Avatar
Exhausted Gondolier
Hypography Staff Member
Administrator

Join Date: Feb 2005
Location: having a rest
Posts: 4,438
Qfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud of
Re: Prime numbers

Here's my algorithm, with a little on-the-fly, untested change in the allocation scheme so that it always prompts for sieve size. This is a good choice if it's often used for not very large values. For use with the highest values one could also choose to save the sieve to a file for following processes.
Code:
#include "stdio.h"
#define lhFF  (0xFFFFFFFF)

typedef unsigned long uLong;

// Copyleft Qfwfq, all rights reversed.

bool nextPrime(uLong& n){
static uLong numFlgs, k, maxPrm = 1,* m_Flgs = 0,
	  I = 0, i = 1, J, j, Q, q, dwi, dwj, dwq, maski = 2, maskj;
 if(n <= 2){
  n = 2;
  return true;
 }
 (n == maxPrm)return true;
 fprintf(stderr, "type in a number of bytes, not more than 268435456:\n");
 while(!m_Flgs){
  scanf("%u", &k);
  numFlgs = k >> 2;
  (k & 3) && (numFlgs++, (k &= ~3), k += 4);
  (k <<= 3)--;
  if(m_Flgs = new uLong[numFlgs]){
   for(uLong ii = 0; ii < numFlgs; ii++)m_Flgs[ii] = lhFF;
   break;
  }
  fprintf(stderr, " couldn't allocate %u bytes!\n\ttype in a lower number:\n", numFlgs << 2);
 }
 if(n < maxPrm){
  uLong ii = (n >> 1), dwii, maskii = 1 << (ii%32), II = ii >> 5, max = (maxPrm >> 1);
  for(; ii < max; II++, maskii = 1){
   dwii = m_Flgs[II];
   for(; maskii && ii <= lhFF; ii++, maskii <<= 1){//		ii = 32II + ii%32
    if(dwii & maskii){
     n = 1 + (ii << 1);
     return true;
    }
   }
  }
 }
 uLong in = n >> 1;
 for(; i <= k; I++, maski = 1){
  dwi = m_Flgs[i];
  for(; maski && i <= k; i++, maski <<= 1){//		i = 32I + i%32
   if(dwi & maski){
    if(i >= in){
     n = maxPrm = 1 + (i << 1);
     return true;
    }
    for(j = (k - i)/((i << 1) + 1), J = (j >> 5), maskj = 1 << (j & 0x1F)
	; j >= i; J--, maskj = 0x80000000){
     dwj = m_Flgs[J];
     for(; maskj && j >= i; j--, maskj >>= 1)//		j = 32J + j%32
      if(dwj & maskj){
       q = ((i*j) << 1) + i + j;
       m_Flgs[Q = (q >> 5)] &= dwq = ~(1 << (q & 0x1F));
       (Q == I) && (dwi &= dwq);
      }
    }
   }
  }
 }
 return false;
}
__________________
Who's afraid of the Big Black Hole?????

Go Black Hole! W the Black Hole!

Hasta que el agujero negro nos traga, siempre!

Hypography Forum PITA...... er, Administrator.
Reply With Quote
  #4 (permalink)  
Old 06-14-2005
Boerseun's Avatar
Holy cow!
Hypography Staff Member
Moderator

Join Date: May 2005
Location: Hartbeespoort, South Africa
Posts: 4,658
Blog Entries: 3
Boerseun has a reputation beyond reputeBoerseun has a reputation beyond reputeBoerseun has a reputation beyond reputeBoerseun has a reputation beyond reputeBoerseun has a reputation beyond reputeBoerseun has a reputation beyond reputeBoerseun has a reputation beyond reputeBoerseun has a reputation beyond reputeBoerseun has a reputation beyond reputeBoerseun has a reputation beyond reputeBoerseun has a reputation beyond repute
Re: Prime numbers

Quote:
Originally Posted by Qfwfq
How large?

I once posted and attachment that's very quick for unsigned longs in C, which means up to 4,294,967,295 but for encryption you might be needing larger values. What I posted can also be useful if you need to find the prime factors of larger numbers that don't have any higher prime factors.
Doesn't really matter, I suppose.

All I want is the standard logic being used - I want to see if it will be useful. Doesn't even need to be in code. I suppose the standard being used is something like:

1) Add 2 to variable
2) Exclude all even integers
3) See if you can divide variable by x (x being 3)
4) Add 2 to x
5) repeat 3 & 4 until x=variable
6) Go back to 1

This seems like it will take a hell of a long time, but how can you speed this up?
__________________
Hypography Forums Moderator

IIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIII



Bovinely blessed be thee.
Reply With Quote
  #5 (permalink)  
Old 06-14-2005
C1ay's Avatar
¿42?
Hypography Staff Member
Administrator
Senior Editor
Editor

Join Date: Feb 2005
Location: 33.78N 84.66W
Posts: 5,755
C1ay has a brilliant futureC1ay has a brilliant futureC1ay has a brilliant futureC1ay has a brilliant futureC1ay has a brilliant futureC1ay has a brilliant futureC1ay has a brilliant futureC1ay has a brilliant futureC1ay has a brilliant futureC1ay has a brilliant future
Re: Prime numbers

Quote:
Originally Posted by Boerseun
Doesn't really matter, I suppose.

All I want is the standard logic being used - I want to see if it will be useful. Doesn't even need to be in code. I suppose the standard being used is something like:

1) Add 2 to variable
2) Exclude all even integers
3) See if you can divide variable by x (x being 3)
4) Add 2 to x
5) repeat 3 & 4 until x=variable
6) Go back to 1

This seems like it will take a hell of a long time, but how can you speed this up?
Actually, you only have to divide the test number by each of the primes that are less than the square root of the number itself to test for primality. Many algorithms do just that after building an initial set of primes using a sieve. There are several tests explained here that can be used for testing primality. GIMPS (site was down when I wrote this) has a downloadable program for searching out primes as part of their effort to find Mersenne primes and I believe the source code is available.
__________________
Clay

Editor and Forum Administrator
stego anyone?
Add yourself to Hypography's Frappr.
"There are only 10 kinds of people in the world --
.....Those who understand binary, and those who don't."
"Draw no conclusions before their time."
Reply With Quote
  #6 (permalink)  
Old 06-14-2005
Qfwfq's Avatar
Exhausted Gondolier
Hypography Staff Member
Administrator

Join Date: Feb 2005
Location: having a rest
Posts: 4,438
Qfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud of
Re: Prime numbers

Quote:
Originally Posted by Boerseun
This seems like it will take a hell of a long time, but how can you speed this up?
Indeed, this would be slow! Mine is an example of Eratosthenes' sieve which works by casting out multiples, rather than by trial division. You can find many hits with Google, I tied just now and got more than 30 thousand so I'll let you do some reading instead of posting too much detail here. Basically, with an array of flags, you hardly need to even multiply.

My sample uses a flag bit for each odd number starting from 3 and fairly standard arithmetic tricks but it has a non-standard iteration that I devised years ago, which seems to help in speed even in RAM and should save more time in an implementation using a file as a large sieve. I never got around to writing such an implementation for larger primes though. As it is, it doesn't perform well if the sieve in RAM gets swapped more than a bit, the iteration fiercely disagrees with the OS swapping algorithm.
__________________
Who's afraid of the Big Black Hole?????

Go Black Hole! W the Black Hole!

Hasta que el agujero negro nos traga, siempre!

Hypography Forum PITA...... er, Administrator.
Reply With Quote
  #7 (permalink)  
Old 06-14-2005
Chacmool's Avatar
Explaining
Hypography Staff Member
Senior Moderator
Editor

Join Date: May 2005
Posts: 823
Chacmool is a splendid one to beholdChacmool is a splendid one to beholdChacmool is a splendid one to beholdChacmool is a splendid one to beholdChacmool is a splendid one to beholdChacmool is a splendid one to beholdChacmool is a splendid one to beholdChacmool is a splendid one to behold
Re: Prime numbers

Quote:
Originally Posted by Boerseun
Does anybody here have an algorythm/procedure for calculating primes?
I want to code an encryption thingy that's going to depend on me being able to figure out how to code primes into it.
What is a "thingy"? It sounds highly scientific to me...
Reply With Quote
  #8 (permalink)  
Old 06-14-2005
Turtle's Avatar
Pasquinader
Latest blog: Meh
Platinum Subscription
Sponsor
Exclamation Re: Prime numbers

___GETE Where to start? How about the thingy? The question then is who do you intend to keep secrets from? The more powerful the adversary, the more powerful the encryption needed.
___While some "shortcuts" exist for finding primes, it is essentially a factoring problem & moreover it remains a "hard" problem in mathematics. This is a fancy way to say there exist no real shortcuts. The best minds take a crack at this problem regularly, as they have for millenia, & yet it remains hard.
___On Eratosthenes' sieve, I reiterate what Q touched on in reference to his code; you must set your upper search limit before you begin.
___All in all, your thingy question is rather silly Boerseun. Why do you tease us so?
__________________
Nemo me impune lacesset. ~Unattested
Reply With Quote
  #9 (permalink)  
Old 06-14-2005
Turtle's Avatar
Pasquinader
Latest blog: Meh
Platinum Subscription
Sponsor
Arrow Re: Prime numbers

___I have some additional thoughts on this topic As if! Anyway, it is the study of the primes under Katabataks.
http://hypography.com/forums/physics-and-mathematics/1343-katabatak-math-an-exploration-in-pure.html

___Staying in base ten, the K(prime) never has transforms of K(3), K(6), or K(9). Since the Katabatak function base ten is congruent modulo 9, one has only to recursively sum the digits of a suspected prime (which you have already visually sieved to include only those with ending digits 1, 3, 7, or 9), & so exclude those with K's 3, 6, or 9 as composite numbers.
___This procedure is sufficient for pencil & paper using numbers as large as a hundred digits or more; note that the K function does not exclude all composites under any one base; employing succesive bases is necessary for that.
___Finally, the analysis of the Katabatak transsforms of primes reveals patterns that further illuminate their nature; it is a rather detailed study however that is for later exposition in the Katabatak thread here exclusively at Hypography.
__________________
Nemo me impune lacesset. ~Unattested

Last edited by Turtle; 06-14-2005 at 03:13 PM. Reason: add link
Reply With Quote
  #10 (permalink)  
Old 06-15-2005
Qfwfq's Avatar
Exhausted Gondolier
Hypography Staff Member
Administrator

Join Date: Feb 2005
Location: having a rest
Posts: 4,438
Qfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud ofQfwfq has much to be proud of
Lightbulb Re: Prime numbers

Quote:
Originally Posted by Turtle
This procedure is sufficient for pencil & paper using numbers as large as a hundred digits or more; note that the K function does not exclude all composites under any one base; employing succesive bases is necessary for that.
To actually compute the digits of a given number in different bases would take computation time itself, but you have given me an idea that could save some trial divisions in primality testing!
__________________
Who's afraid of the Big Black Hole?????

Go Black Hole! W the Black Hole!

Hasta que el agujero negro nos traga, siempre!

Hypography Forum PITA...... er, Administrator.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Prime Suspects Ninja2789 Physics and Mathematics 4 06-10-2005
Factoring alexander Physics and Mathematics 67 05-02-2005
Prime Density Thelonious Physics and Mathematics 3 04-05-2005
Math problem: two unknown numbers, known product and sum MortenS Physics and Mathematics 10 03-10-2005
Prime numbers robi Physics and Mathematics 0 01-26-2004

» Current Poll
Favorite James Bond?
Sean Connery - 63.64%
7 Votes
George Lazenby - 0%
0 Votes
David Niven - 9.09%
1 Vote
Roger Moore - 9.09%
1 Vote
Timothy Dalton - 9.09%
1 Vote
Pierce Brosnan - 0%
0 Votes
Daniel Craig - 9.09%
1 Vote
Hate 'em all - 0%
0 Votes
Who's James Bond? - 0%
0 Votes
Total Votes: 11
You may not vote on this poll.

All times are GMT -8. The time now is 04:50 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 ©2008, Crawlability, Inc.
Copyright © 2000-2008 Hypography
Part of the Hypography - Science for Everyone Network