473,467 Members | 1,612 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Code that is supposed to find perfect numbers

2 New Member
i dont know how to upload in a way that you can see it clearly.. so im just going to copy and paste what i have so far... sorry. :S

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. #include <cmath>
  6.  
  7. void calculateNumber (double);
  8. double mypow (double, int);
  9.  
  10. int main ()
  11. {
  12.  
  13.     double x = 2 ;
  14.     int y = 2;
  15.     double myNumber= 0; 
  16.  
  17.  
  18.     myNumber = mypow ( x , y );
  19.  
  20.     calculateNumber(myNumber);
  21.  
  22. return 0;
  23. }
  24.  
  25.  
  26. double mypow ( double x1, int y1)
  27. {
  28.  
  29.     double x = 2;
  30.  
  31.     int perfectNumber = 0;
  32.  
  33.  
  34.  
  35.  
  36.     for (; perfectNumber <=1000; perfectNumber++)
  37.     {
  38.         for (int z = 2; z <perfectNumber; z++)
  39.         {
  40.  
  41.             for (int y = 2 ; y < perfectNumber; y++)
  42.             {
  43.                 if (perfectNumber ==  ((pow (x,z - 1)) * ((pow (x, y)) - 1)))
  44.                  cout<< " " <<perfectNumber<<endl<<endl;
  45.             }
  46.         }
  47.     }
  48.  
  49.  
  50.  
  51.  
  52. return perfectNumber;
  53. }
  54.  
  55. void calculateNumber (double perfectNumber)
  56.     //double perfectNumber;
  57.     cout << " " << perfectNumber;
  58.  
  59.  
  60. }
thanks for your help.. the program outputs many numbers that arent actually perfect numbers.. :S
Nov 10 '09 #1
4 4256
newb16
687 Contributor
What do you think is not a perfect number but the program lists it as such?
Nov 10 '09 #2
weaknessforcats
9,208 Recognized Expert Moderator Expert
It's probably here:

Expand|Select|Wrap|Line Numbers
  1. if (perfectNumber ==  ((pow (x,z - 1)) * ((pow (x, y)) - 1))
The pow function returns a double. The compiler will promote perfectNumber from int to double and then do the ==. Unfortunately, you can't use the == operator with floating point.

A little Google on floating point arithmetic will give you a lot of info.
Nov 10 '09 #3
donbock
2,426 Recognized Expert Top Contributor
@Busgosu
I'm not familiar with this algorithm for finding perfect numbers. Where did you get it from? I suggest you do a little more research into the mathematics of perfect numbers before you worry about getting the source code right.

It looks to me like you are trying to make use of the relationship between Mersenne primes and perfect numbers known as the Euclid-Euler Theorem. However, be warned that this relationship only applies to even perfect numbers. You need to use some other technique to account for odd perfect numbers. (Or are you allowed to take advantage of the consensus that if there are any odd perfect numbers they must all be greater than 10^300?)

By the way, you don't need the pow() function to raise "2" to a power. It is much easier to do that with shift-left. By avoiding the pow() function you bypass the floating-point issues raised by weaknessforcats.

By the way, there are only three perfect numbers less than 1000.
Nov 10 '09 #4
Busgosu
2 New Member
my mistake is that i didnt take into account the Mersenne primes. The second part of the equation (((pow (2, y)) - 1))) has to be a prime number, since i did not tell the program that it had to be a prime number it outputs many values that are not perfect numbers.
( i know my english is not perfect, i am not english, i am from Spain)
Nov 11 '09 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: | last post by:
I have to write a program that displays all of the perfect numbers less than 1000. I know I need 2 for loops and at least one if statement. What I need to know is, is there any method in Java to...
242
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
16
by: Jason | last post by:
Hi, I need a way to use random numbers in c++. In my c++ project, when using the mingw compiler I used a mersenne twister that is publicly available and this did its job well. Now I have...
14
by: nullptr | last post by:
Hi, As an exercise, I've written a program implementing run-length encoding. I would be more that happy to hear your comments, suggestions, etc Thanks, --- #include <stdio.h> #include...
7
by: Trickynick1001 | last post by:
Hi, a newbie here. I don't have a real firm grasp on the idea of Javascript, as I'm used to programming in Qbasic and C. I'm not used to OOP. Anyway, I really don't have any idea what the...
19
by: gk245 | last post by:
Trying to write a program that will figure out if a number is perfect or not. Here is my logic: 1) Read in the number 2) Split it up (number - 1) 3) Put all the split up numbers into an...
9
by: shorti | last post by:
db2 V8.2 I have been looking for a good way to log the 'reason code' when we hit an sqlcode that uses them. From what I can tell the reason code is stored in sqlca.sqlerrmc. But, there seems...
2
by: wkid87 | last post by:
Using C# Microsft Visual C#, Console Application I'm needing help with the user entering in a number to tell if it perfect or not. I have the perfect number figure out. Here in the instruction: ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.