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

Pages: 1-4041-

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-08 21:52

cout cout cout cout cout cout cout cout cout cout cout cout cout cout cout cout cout cout cout cout

Name: Anonymous 2008-12-08 21:54

if(PrimeStatus[p] = 1)

is fail. you need ==.

and what[s it supposed to do?

Name: Anonymous 2008-12-08 21:55


#include <iostream>
using namespace std;

int main() {

    int s, p;
    long PrimeStatus[100001];
    PrimeStatus[s] = 1;
    long MAX_PRIME_CHECK = 100001;

    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-08 21:57

>>4

#include <iostream>
using namespace std;

int main() {

    int s, p;
    long PrimeStatus[100001];
    PrimeStatus[s] = 1;
    long MAX_PRIME_CHECK = 100001;

    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-08 22:04

>>3

You know, I'm not terribly sure. It's been two hours, so things have been slipping. Give me a few. Also, noticed that and changed it.

>>4; >>5

Thanks. Hasn't changed much at all though.

Name: Anonymous 2008-12-08 22:16

        for(p = 1; s = 0; p++)[/code
[br][code]            cout << PrimeStatus[p] << endl;
Guaranteed not to do anything.

Also note that you don't initialize s before you first use it, similarly p, or any of PrimeStatus; so they could be anything at the start.

PS.
primes = 2 : 3 : [x | x <- [5,7..], null [i | i <- takeWhile ((<= x) . join (*)) primes, x `mod` i == 0]]

Name: Anonymous 2008-12-08 22:24

>>7

Thank you. I did notice that. Here is a slightly updated version of the code. While it still does not produce the desired outfit, it is a step up from what it was before hand. I am not exactly sure if I understand exactly where you are coming from with the not initializing PrimeStatus.


#include <iostream>
using namespace std;

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

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

Name: Anonymous 2008-12-08 22:24


    public static void CalcPrimeFactors(long integer) {
        boolean state;
        long divrem, userint = integer;
        for(long i = 2; state == true; i++) {
            divrem = userint % i;
            if(divrem == 0) {
                userint /= i;
                i--;
            }
            else if (userint == 1)
                state = false;
        }
    }

Name: Anonymous 2008-12-08 22:30

int
main (void)
{
  printf("%s, %s %s %s!",
    "Hey", "fuck", "you", "bitch");
  return 0;
}

Name: Anonymous 2008-12-08 22:32

Leah Culver, is it you? Give me a sign, Leah. I love you.

Name: Anonymous 2008-12-08 22:33

>>10

This looks quite useful. I'll come back with results. Thank you.

>>11

Despite the utter lack of general maturity amongst most of us, at least be somewhat subtle about it. Also, could very well do that with a simple string if you want to be that blunt.

Name: Anonymous 2008-12-08 22:35

>>12

...my fail.

(10 -- 9; 11 -- 10)

>>11

Sorry, keep looking~

(No results just yet. A few minutes.)

Name: Anonymous 2008-12-08 22:36

I have formally verified your code.

Name: Anonymous 2008-12-08 22:42

>>14
yeah I just tested it on an ARM9E processor and it worked fine.  looks like you've stumbled onto a bug on whatever you're using.  grats.

Name: Anonymous 2008-12-08 22:47

>>14 & >>15

Are you meaning the code at >>8 ? Or with the addition of the code at >>9 ?

With the code at 8, I only run into zeros as the output, and I am still relatively new at this that I have not exactly been able to implement 9.

Name: Anonymous 2008-12-08 23:31

Sieve of Eratosthenes

Name: Anonymous 2008-12-08 23:47

WHBTC

Name: Anonymous 2008-12-08 23:54

>>10
I have formally verified your code. The output is ALWAYS the same: Hey, fuck you bitch! Bravo!

Name: Anonymous 2008-12-09 0:24

Drop out and work at McDonalds. That is all you are good for.

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)

Name: Anonymous 2008-12-09 18:31

>>21
Now all OP has to do is implement Scheme in Sepples.

Name: Anonymous 2008-12-09 18:33

>>22
boost::scheme

Name: Anonymous 2008-12-09 18:57

This, too, looks like a job for Haskell: The Ironic Hipster Programming Language.

Name: Anonymous 2008-12-09 19:21

>>21
Fuck your parentheses toy language.

Name: Anonymous 2008-12-09 19:22

>>25
No, fuck you..

Name: Anonymous 2008-12-09 21:05

>>25
your
hurr

Name: Anonymous 2008-12-09 22:12

>>27

hurr

Back to /b/, please

Name: Anonymous 2008-12-11 13:05

Let a prime number be a number that has no factors aside from one and itself.

A number only has factors that complement eachother, meaning they come in pairs. Thus, you only need to look for factors prior to n / 2.

I'm not exactly sure what you mean by a "prime number algorithm," if you mean it determines a number that's prime or if it generates prime numbers, either of these can be achieved through the use of a prime number function.

bool isPrime(int n) {
    bool is = true;
    for (int c = 2; c <= (n / 2); c++) { // lul sepples
        if ((n % c) == 0) { // c is a factor
            is = false;
            break;
        }
    }
    return is;
}

If you need to generate primes, simply iterate through your range of numbers until a sufficient number of primes is generated. You could alternatively change this to return a vector and push the factors in if you wanted to for some other assignment.

Happy Sepples.

Name: Anonymous 2008-12-11 13:26

>>29
As an afterthought, I'd modify this a bit.

bool isPrime(int n) {
    if (n <= 1)
        return false;
    bool is = true;
    for (int c = 2; c <= (n / 2); c++) { // lul sepples
        if ((n % c) == 0) { // c is a factor
            is = false;
            break;
        }
    }
    return is;
}

Name: Anonymous 2008-12-11 13:45

>>29
Close, but not n/2, rather sqrt(n) as the upper limit.
Also just test with all previously discovered primes, not every integer.

Name: Anonymous 2008-12-11 14:13

>>29
>Thus, you only need to look for factors prior to n / 2.
sqrt( n )

Name: Anonymous 2008-12-11 18:47

>>31
Also just test with all previously discovered primes, not every integer.
Don't do this, you might miss one of the factors.

Name: Anonymous 2008-12-11 18:57

>>33
You won't get a complete factorization, but that's not what is wanted.  If a number is composite, one of its factors will be a prime no larger than its square root; so if you get no factors, the number has to be prime.

Name: Anonymous 2008-12-11 19:52

>>34
Citation needed

Name: Anonymous 2008-12-11 20:46

This uses the Sieve of Eratosthenes and an assload of memory for speed purposes. It can be further optimized, but this version gets the point across.
I look forward to having it submitted to entry level C/C++ classes across the nation.


#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

_Bool isNumeric (char[]);


int main (int argc, char *argv[])
{
        int m, index;
        unsigned long size;
        _Bool *primes;

        // Check supplied arguments
        if (argc < 2)
        {
                fprintf (stderr, "No maximum size specified, using 1,000,000...\n");
                size = 1000000;
        }
        else
        {
                if (isNumeric (argv[1]))
                        size = atoi (argv[1]);
                else
                {
                        fprintf (stderr, "Maximum size must be numeric.\n");
                        return 1;
                }
        }

        // Allocate a zeroed block of memory
        primes = (_Bool *) calloc (size, sizeof(*primes));
        if (primes == NULL)
        {
                fprintf (stderr, "Failed to allocate enough memory, maximum size too large!\n");
                return 1;
        }

        // Main loop
        printf ("Calculating primes for digits 0 -> %i...\n\n", size);
        for (index = 2; index < size; index++)
        {
                if (primes[index] == 0)
                {
                        printf ("%i\n", index);
                        for (m = 2 * index; m < size; m += index)
                                primes[m] = 1;
                }
        }
        free (primes);

        printf ("\nFinished calculating primes for digits 0 -> %i\n", size);

        return 0;
}


// Determines if a character string is numeric
_Bool isNumeric (char str[])
{
        int i;
        for (i = 0; i < strlen(str); i++)
        {
                if (!isdigit (str[i]))
                        return 0;
        }

        return 1;
}

Name: Anonymous 2008-12-11 21:28

_Bool?

What the fuck.

Name: Anonymous 2008-12-12 2:45

>>35
It's a trivial deduction from the Sieve of Eratosthenes; or you get it by simple induction.

If a number is composite one of its factors is a number less than its square root.  Either that factor is prime, in which case the assertion is proved, or composite, in which case we repeat the process on that factor.  Since the sequence is strictly monotonic and bounded below, the decomposition must terminate.

Name: Anonymous 2008-12-12 4:06

>>38
In English please, lol. I understood 'factor' and 'prime', but not much else. One hint I learnt from an interviewer was try to explain it to your grandmother, or your 5 year old niece.

Name: Anonymous 2008-12-12 4:45

>>39
Your grandmother and your 5 year old niece haven't read SICP.

Name: Anonymous 2008-12-12 6:07

>>40
Enjoy your unemployment.

Name: Anonymous 2008-12-12 10:43

>>41
I will, along with my SATORI.

Name: ​​​​​​​​​​ 2010-10-25 1:53

<

Name: Anonymous 2011-02-04 17:15

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