#include <iostream>
using namespace std;
long int math;
long int check;
long int divider = 1;
long int inputnum;
void dividerp();
void counter();
void inputp();

void dividerp(){
     math = inputnum/divider;
     if (math == 1 or inputnum == 2 or inputnum == 1){
              cout<<"You do have a prime number there!"<<endl;
              system("PAUSE");
              system("CLS");
              math = 0;
              inputp();
              }
     else if (math == inputnum){
          counter();
          }
     else if ( inputnum % divider != 0 ){
          counter();
          }

     else if (math >= 1){
              cout<<"You do not have a prime number!"<<endl;
              system("PAUSE");
              system("CLS");
              math = 0;
              inputp();
              }
     else{
              counter();
              }
     }
     
void counter(){
     divider = divider + 1;
     dividerp();
     }
     
void inputp(){
     divider = 1;
     cout<< "Please type in a number."<<endl;
     cout<< "I will check to see if it is a prime number."<<endl;
     cin>> inputnum;
     if (inputnum >= 1){
               dividerp();
               }
     else {
          cout<<"Sorry you have not typed in an accurate number."<<endl;
          system("PAUSE");
          system("CLS");
          inputnum = 0;
          inputp();
     }
     }
     
int main(){
    cout<< "Hello and welcome to the program."<<endl;
    inputp();
}

I'm still learning, (Went back from allegrogl, totally forgot how to do this stuff) Is there a way to improve this code, or types of functions that can improve performance (Not like there is a problem with it). And how can I increase the number range that I can use to find more prime numbers? I can't use floats/doubles due to the fact that they for some reason do not like to have math applied to them, is there a way to fix that?

EDIT: Optimizing code, that's the word i'm looking for. How can I optimize code?

Dani AI

Generated

Quick diagnostic and plan for improvement (for ): the original program works but is brittle and slow for larger inputs because it relies on globals and recursive calls to walk a divider. Avoid platform-specific calls like system("PAUSE")/system("CLS"), avoid global state, validate input explicitly, and use integer types for primality checks — floating-point types can do arithmetic but lose exactness for large integers, so they aren’t safe for exact divisibility tests.

A compact, safer pattern is to write a small, iterative test that:

  • returns early for small cases,
  • rejects even numbers quickly,
  • checks only odd divisors up to floor(sqrt(n)).
#include <iostream>
#include <cmath>
#include <cstdint>

bool is_prime(std::uint64_t n) {
    if (n < 2) return false;
    if ((n & 1) == 0) return n == 2;
    auto limit = static_cast<std::uint64_t>(std::sqrt(static_cast<long double>(n)));
    for (std::uint64_t d = 3; d <= limit; d += 2)
        if (n % d == 0) return false;
    return true;
}

int main() {
    std::uint64_t n;
    while (std::cin >> n)
        std::cout << n << (is_prime(n) ? " is prime\n" : " is composite\n");
}

Notes on optimization and scaling: trial division to sqrt(n) is O(sqrt(n)). If many primes up to N are needed, use a sieve (memory/time tradeoff) or a segmented sieve for large N. For very large single-number tests (hundreds or thousands of bits), use a probabilistic test (Miller–Rabin) with a big-integer library (Boost.Multiprecision or GMP). To increase range safely, use fixed-width integer types (std::uint64_t) for up to 64-bit values; beyond that, switch to multiprecision types.

Practical tips: remove recursion and globals, drop system calls, compile with optimizations (e.g., -O2), time functions with std::chrono and profile hotspots. correctly pointed toward sieves; pointed at reducing the test range — take that further by testing only to sqrt(n) and skipping even divisors; ’s sieve reference is aligned with that path.

Recommended Answers

All 6 Replies

you could use a simple for loop in your main function and do a brute force method like your functions do or you could use the Sieve of Eratosthenes method which works very well. http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

also on line 13 in your code you are using or which is incorrect in c++. || = or and && = and in c++

When testing for primes, you only need to test half the range:

bool is_prime = true;

cout << "Enter a numeral: ";
cin >> number;

if(number == 1 || number == 3)
{
     cout << "Number is prime.";
     exit(0);
}

for(int i=2; i<number/2; i++)
{
     //If there is any case of no remainder, then number is not prime
     if(number%i)
     {
          is_prime = false;
          break;
     }
}

if(is_prime)

     cout << number << " is a prime number. ";

else 

     cout << number << " is not a prime number. ";

There might be easier ways to do this.. this is just something I came up with off the top of me' head.

also on line 13 in your code you are using or which is incorrect in c++. || = or and && = and in c++

I'm using Dv-C++ writer and compiler and it said that "or" was ok when it was compiling, was just having a brain fart :P

i bow before your compiler then ;). seriously i would use the standard operators since i doubt it would work on older compilers.

Other than one post which could clean up my code, I have gotten no useful replies. I just want to improve my skill and learn new functions that might be helpful, for I have not any other means of doing so.

You can see this post if you wish.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.