deciding if a number is perfect or prime
Asked Answered
S

4

2

the problem is : "Write a function to find out if a number is a prime or perfect number."

so far i have worked on the perfect part first and this is what i have:

#include <iostream>
using namespace std;
bool perfectNumber(int);
int main()
{
 int number;

 cout<<"Please enter number:\n";
 cin>>number;
 bool perfectNumber(number);

 return 0;
}
bool perfectNumber(int number)
{
 int i;

 int sum=0;
 for(i=1;i<=number/2;i++)
 {
  if(number%i==0)
  {
   sum+=i;
  }
 }
 if (sum==number)
  return i;
 else
  return 0;
}

HOWEVER, there seems to be errors on this code. I have looked over the book but nothing talks about this topic. i would like to get advice on how to fix this code.

thanks!

Schoolteacher answered 12/12, 2010 at 21:1 Comment(4)
% means remainder of number/i as 4%2=0Epimenides
Sounds like one of Project Euler's. But maybe it's just me ;)Harriot
What errors are you getting. Looking at the code sample you are defining perfectNumber(int) as a bool but you are returning an int.Quintie
@Ash Burlaczenko: There's nothing wrong with that. The problem is in line 10 (see Answer from James McNellis).Teresitateressa
F
7
bool perfectNumber(number);

This does not call the perfectNumber function; it declares a local variable named perfectNumber of type bool and initializes it with the value of number converted to type bool.

In order to call the perfectNumber function, you need to use something along the lines of:

bool result = perfectNumber(number);

or:

bool result(perfectNumber(number));

On another note: if you are going to read input from a stream (e.g. cin>>number), you must check to be sure that the extraction of the value from the stream succeeded. As it is now, if you typed in asdf, the extraction would fail and number would be left uninitialized. The best way to check whether an extraction succeeds is simply to test the state of the stream:

if (cin >> number) {
    bool result = perfectNumber(number);
}
else {
    // input operation failed; handle the error as appropriate
}

You can learn more about how the stream error states are set and reset in Semantics of flags on basic_ios. You should also consult a good, introductory-level C++ book for more stream-use best practices.

Fleeting answered 12/12, 2010 at 21:8 Comment(0)
E
1
void primenum(long double x) {
    bool prime = true; 
    int number2;
    number2 = (int) floor(sqrt(x));// Calculates the square-root of 'x'

    for (int i = 1; i <= x; i++) {
        for (int j = 2; j <= number2; j++) {
            if (i != j && i % j == 0) {
                prime = false;
                break;
            }
        }
        if (prime) {
            cout << " " << i << " ";
            c += 1;
        }
        prime = true;
    }
}
Epimenides answered 12/12, 2010 at 21:11 Comment(2)
bool prime = true; // Calculates the square-root of 'x' Really? it's also important to place comments correctly.Rejuvenate
This is so wrong. If you are testing if x is prime, get rid of the outer loop. If you are testing for all primes in [1,x] then the bound on the inner loop should be sqrt(i) not sqrt(x). And in that case this is a ridiculously inefficient way to test for primes in that range. See en.wikipedia.org/wiki/Sieve_of_Eratosthenes for a much better method.Illyria
V
1
 bool isPerfect(  int number){
     int i;
     int sum=0;
     for(i=1;i<number ;i++){
         if(number %i == 0){
             cout<<"  " << i ;
             sum+=i;
         }
     }

     if (sum == number){
         cout<<"\n \t\t THIS NUMBER  >>>  "<<  number <<"   IS    PERFECT \n\n";
         return i;
     }else if (sum |= number) {
               cout<<"\nThis number >>> " <<  number <<"   IS  NOT  PERFECT \n\n";
               return 0;
     }
 }
Varia answered 30/11, 2013 at 17:33 Comment(0)
A
-1
#pragma hdrstop

#include <tchar.h>
#include <stdio.h>
#include <conio.h>
//---------------------------------------------------------------------------


bool is_prim(int nr)
{

    for (int i = 2; i < nr-1; i++) {

    if (nr%i==0) return false;

    }

    return true;

}

bool is_ptr(int nr)
{
    int sum=0;

    for (int i = 1; i < nr; i++) {

    if (nr%i==0) {
        sum=sum+i;
    }

    }

    if (sum==nr) { return true;

    }
    else return false;
}
#pragma argsused
int _tmain(int argc, _TCHAR* argv[])
{
    int numar;

    printf ("Number=");scanf("%d",&numar);
    if (is_prim(numar)==true) { printf("The number is prime");

    }
    else printf("The number is not prime");

    if (is_ptr(numar)==true) { printf(" The number is perfect");

    }
    else printf(" The number is not perfect");
    getch();
    return 0;
}
Announcer answered 12/12, 2010 at 21:21 Comment(4)
is_ptr returns false or true for a perfect number and is_prim returns true or false for a prime numberAnnouncer
Please format your code. Didn't you look at the preview and notice "this is unreadable" before posting the answer? In the editor, select the source code, then click the 101010 button.Carapace
@jalf 101010 means fine ? I didn't know :-)Birdwatcher
funny. ;) btw, to avoid confusion if others read this, the { } button used to show something like 10010 instead.Carapace

© 2022 - 2024 — McMap. All rights reserved.