Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ Prime Number Algorithm

Name: Anonymous 2008-12-08 21:47

/prog/ I'm very deeply stressed at the moment. I have an assignment due tomorrow morning, a prime number algorithm. I know I screwed it up badly, and I'm not even sure if I am on the right track. If there are any capable c++ programmers on /prog/ right now, think you could lend a gal a hand?

Here is a copy of my current code:

#include <iostream>
using namespace std;

int main()
{
    int s;
    int p;
    long PrimeStatus[100001];
    PrimeStatus[s] = 1;
    long MAX_PRIME_CHECK = 100000;

    for(int s = 1; s < MAX_PRIME_CHECK; s++)
    {
        if(p > 2)
        {
            PrimeStatus[p] = 1;
            return 0;
        }
       
        else if(p <= 2)
        {
            if(PrimeStatus[p] = 1)
            {
                PrimeStatus[p] = 1;
                return 0;
            }
           
            else if(PrimeStatus[p] = 0)
            {
                if(p%(p-1) == 0)
                {
                    PrimeStatus[p] = 0;
                    return 0;
                }
               
                else while(p%(p-1) != 0)
                {
                    p = p-1;
                }
               
                if(p%(p-1) == 0)
                {
                    PrimeStatus[p] = 0;
                    return 0;
                }
            }
        }
    for(p = 1; s = 0; p++)
    {
        cout << PrimeStatus[p] << endl;
    }
    return 0;
    }
}

Name: Anonymous 2008-12-09 3:18

Yuck, OP's code hurts to look at.

(define (square x) (* x x))

(define (miller-rabin-expmod base exp m)
  (define (squaremod-with-check x)
    (define (check-nontrivial-sqrt1 x square)
      (if (and (= square 1)
               (not (= x 1))
               (not (= x (- m 1))))
          0
          square))
    (check-nontrivial-sqrt1 x (remainder (square x) m)))
  (cond ((= exp 0) 1)
        ((even? exp) (squaremod-with-check
                      (miller-rabin-expmod base (/ exp 2) m)))
        (else
         (remainder (* base (miller-rabin-expmod base (- exp 1) ))
                    m))))
(define (miller-rabin-test n)
  (define (try-it a)
    (define (check-it x)
      (and (not (= x 0)) (= x 1)))
    (check-it (miller-rabin-expmod a (- n 1) n)))
  (try-it (+ 1 (random (- n 1)))))

(define (fast-prime? n times)
  (cond ((= times 0) true)
        ((miller-rabin-test n) (fast-prime? n (- times 1)))
        (else false)))

(define (prime? n) 
    ; Perform the test how many times? 
    ; Use 100 as an arbitrary value. 
    (fast-prime? n 100))

(define (prime-status n)
 (define (prime-status-iter m n)
  (cond ((> m n) '())
        (else (begin
                (fast-prime? m 100)
                (prime-status-iter (+ m 1) n)))))
 (prime-status-iter 1 n))

(prime-status 100000)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List