B04f7f475867f6b47a59b49dfabc0daf

This is a simple program to determine whether or not a number is prime. I'm wondering if I could make the prime determination part more streamlined (the stuff inside the for loop). Also, the last part where it asks them to hit enter doesn't seem to be working. I'd like for it to pause until they hit any key, clear the screen, then start all over again.

#include <iostream.h>
#include <stdlib.h>
#include <math.h>

int main() {
	
	while(true) {

		int num;
		
		cout << "Give me a positive integer: ";
		cin >> num;
		
		while (num < 1 ) {
			cout << "Please enter a POSITIVE number: ";
			cin >> num;
		}
		
		int p = 0;

		for ( int i = 2; i <= sqrt(num); i++ ) {

			if ( num % i == 0 ) {
				p = 1;
			}

			if (p == 1 )
				break;
		}

		cout << num << (p == 1 ? " is NOT prime." : " IS prime.") << endl;
		
		cout << "\n\nHit ENTER to try another one";
		system("cls");
		
	}
	
	return 0;
}

Refactorings

No refactoring yet !

A8d3f35baafdaea851914b17dae9e1fc

Adam

October 8, 2008, October 08, 2008 15:50, permalink

No rating. Login to rate!

A refactoring with basic I/O. Use a library like ncurses if you want to be able to clear the screen, wait for user input, etc.

#include <iostream>
#include <openssl/bn.h>

class Bignum
{
public:
    Bignum(unsigned long number)
    {
        big_number = BN_new();
        BN_set_word(big_number, number);
    }
    
    ~Bignum()
    {
        BN_free(big_number);
    }
    
    int is_prime()
    {
        return BN_is_prime(big_number, 0, NULL, NULL, NULL);
    }
    
protected:
    BIGNUM *big_number;
};

int main(int argc, char **argv)
{
    unsigned long prime;
    const char *messages[] = { " is NOT prime.", " IS prime." };
    
    while (true) {
        std::cout << "Give me a positive integer: ";
        std::cin  >> prime;
  
        std::cout << prime
                  << messages[Bignum(prime).is_prime()]
                  << std::endl;
    }
    
    return 0;
}
B04f7f475867f6b47a59b49dfabc0daf

joshuamc

October 8, 2008, October 08, 2008 16:19, permalink

No rating. Login to rate!

This is beyond my knowledge. I'd like to just try and do it myself with a for loop.

A8d3f35baafdaea851914b17dae9e1fc

Adam

October 8, 2008, October 08, 2008 20:41, permalink

No rating. Login to rate!

Just playing around with a ncurses version.

#include <sstream>
#include <curses.h>
#include <openssl/bn.h>

class Bignum
{
public:
    Bignum(unsigned long number)
    {
        big_number = BN_new();
        BN_set_word(big_number, number);
    }
    
    ~Bignum()
    {
        BN_free(big_number);
    }
    
    int is_prime()
    {
        return BN_is_prime(big_number, 0, NULL, NULL, NULL);
    }
    
protected:
    BIGNUM *big_number;
};

class Screen
{
public:
    Screen()
    {
        initscr();
    }
    
    ~Screen()
    {
        endwin();
    }
    
    void write(const char *string)
    {
        addstr(string);
    }
    
    void write(const char *string, int y, int x)
    {
        move(y, x);
        write(string);
    }
    
    void write(std::string string)
    {
        write(string.c_str());
    }
    
    void write(std::string string, int y, int x)
    {
        write(string.c_str(), y, x);
    }
    
    void read(unsigned long *value)
    {
        scanw("%lu", value);
    }
    
    void wait()
    {
        getch();
    }
    
    void clear()
    {
        ::clear();
    }
};

int main(int argc, char **argv)
{
    Screen screen;
    unsigned long prime;
    const char *messages[] = { " is NOT prime.", " IS prime." };
    
    while (true) {
        screen.clear();
        screen.write("Give me a positive integer: ");
        screen.read(&prime);
        
        std::stringstream message;
        message << prime
                << messages[Bignum(prime).is_prime()]
                << std::endl;
        
        screen.write(message.str(), 1, 0);
        screen.write("Press any key to try another. ", 3, 0);
        screen.wait();
    }
    
    return 0;
}
3d920d12e54fc518dcacabbccb2e6138

Chris

October 15, 2008, October 15, 2008 00:37, permalink

No rating. Login to rate!

This is 2 easy.

B04f7f475867f6b47a59b49dfabc0daf

goodespeler.myopenid.com

October 15, 2008, October 15, 2008 11:10, permalink

No rating. Login to rate!

This assignment has come to pass, but the we were only allowed to use a while loop, for loop, if/else, and the libraries I included.

D41d8cd98f00b204e9800998ecf8427e

santa

October 18, 2008, October 18, 2008 18:03, permalink

No rating. Login to rate!

since it's ran on multiple big nums, adam, why not store a list of primes encountered by then and only check vs them and normal numbers above them (updating the list) - this would complicate the process but would save time when ran on big numbers... <- of course computers aren't as weak today but had it had to run on large numbers and in quantities...

A8d3f35baafdaea851914b17dae9e1fc

Adam

October 20, 2008, October 20, 2008 01:44, permalink

No rating. Login to rate!

@santa

1. Quality of code is more important than performance, *unless* a bottleneck is identified. For my purposes, the implementation was more than adequate performance wise. No need to degrade the code any further with added complication.

2. I'm not exactly keen on the BigNum, but it was a necessary evil to utilize the prime function of the OpenSSL library. There is no point in reinventing the wheel here; using an existing implementation is a no brainer. I could have evaluated another library that operated on floats directly, and probably would have for my own code. But for the purposes of refactormycode I wanted to use a library that was relatively ubiquitous. OpenSSL fit that bill.

08aede0393b91ecfa58cbf53663a70d8

isaac

April 23, 2009, April 23, 2009 12:08, permalink

No rating. Login to rate!

This is my version, of which I cannot test, since I do not have access to a compiler.

#include <iostream.h>
#include <stdlib.h>
#include <math.h>

int isNotPrime(int x)
	{
		if (x < 2) return 1;
		if (x == 2) return 0;
		if ((x & 1) == 0) return 1;
		int z = 1;
		do
		{
			z += 2;
			if ((x % z) == 0) return 1;
		} while ((x / z) > z);
		return 0;
	}

int main() {
	
	while(true) {

		int num;
		
		cout << "Give me a positive integer: ";
		cin >> num;
		
		while (num < 1 ) {
			cout << "Please enter a POSITIVE number: ";
			cin >> num;
		}
		
		int p = isNotPrime(num);

		cout << num << (p == 1 ? " is NOT prime." : " IS prime.") << endl;
		
		cout << "\n\nHit ENTER to try another one";
               cin.get();
		system("cls");
		
	}
	
	return 0;
}
Db5f98fa348bd1bbcbb8d044fa16115a

DH

October 28, 2009, October 28, 2009 19:34, permalink

No rating. Login to rate!

This is a simple implementation. We can pull the even numbers out of the for loop, and then just increment by two over every odd number.

#include <iostream>

bool isprime(int num)
{
    if(num % 2 == 0) return false;
    for(int i=3; i<sqrt(num); i+=2)
        if(num % i == 0) return false;
    return true;
}


int main(int argc, char* argv[])
{
    int num;
    while(std::cin >> num)
    {
        if(isprime(num)) std::cout << num << " is prime.\n";
        else std::cout << num << " is not prime.\n";
    }

}
D41d8cd98f00b204e9800998ecf8427e

tardface

December 27, 2009, December 27, 2009 03:14, permalink

No rating. Login to rate!

Not the shortest, but probably one of the fastest. :)

#include <cstdlib>
#include <iostream>
#include <vector>

static std::vector<int> primes;

static void fill_primes() {
    int up_to = 50000;
    int limit = up_to/2;
    bool *flags = new bool[up_to];

    for (int i = 0; i < up_to; i++) flags[i] = false;
    primes.push_back(2);

    int i = 0;
    for (; i * i < limit; i++) {
        while (flags[i]) i++;

        int n = 2*i + 3;
        primes.push_back(n);

        for (int w = i+n; w < limit; w += n) flags[i] = true;
    }
    for (; i < limit; i++) 
        if (!flags[i]) primes.push_back(2 * i + 3);

    free(flags);
}

static bool is_prime(int n) {
    if (n < 2) return false;

    for (int i = 0; i < primes.size(); i++) {
        int val = primes[i];
        if (val * val > n) break;
        if (n % val == 0) return false;
    }
    return true;
}



int main() {
    fill_primes();

    while (true) {
        int num;

        std::cout << "Give me a positive integer: ";
        std::cin >> num;

        while (num < 1 ) {
            std::cout << "Please enter a POSITIVE number: ";
            std::cin >> num;
        }

        std::cout << num;
        std::cout << (!is_prime(num) ? " is NOT prime." : " IS prime.");
        std::cout << std::endl;
    }
    return 0;
}

Your refactoring





Format Copy from initial code

or Cancel