473,770 Members | 4,419 Online
Bytes | Software Development & Data Engineering Community
+ 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 4277
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
14735
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 test if a number is perfect? Thanks.
242
13458
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 comments on past experience, research articles, comments on the matter would be much appreciated. I suspect something like C would be the best based on comments I received from the VB news group. Thanks for the help in advance James Cameron
16
4228
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 shelled out on VC++ 6.0 compiling that same code is proving difficult. I am not too worried how I generate random numbers in c++, as long as it is sufficiently random. Can anybody help me out in getting what I want from VC++?
14
9930
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 <string.h>
7
1780
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 problem is with this code, it just simply won't work properly. Some of the functions aren't done, but the main one gives me a Not a Number message in the text box where the calculations are supposed to come up. I tried to use a parseInt on my stuff,...
19
4454
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 array 4) Figure out if the original number is evenly divisible by any of the numbers in the array.
9
6709
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 to be other information stored in this string besides the reason code. The DB2 Info Center website states that sqlca.sqlerrml identifies whether sqlerrmc contains valid data and the length of the data so I added code that would copy the content...
2
12688
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. An integer number is said to be a perfect number if its factors, including 1 (but not the number itself), sum to the number. For example, 6 is a perfect number, because 6 = 1 + 2 + 3. Write a method Perfect that determines whether parameter...
0
9439
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10071
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8905
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6690
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5326
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3987
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 we have to send another system
3
2832
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.