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 - #include <iostream>
-
-
using namespace std;
-
-
#include <cmath>
-
-
void calculateNumber (double);
-
double mypow (double, int);
-
-
int main ()
-
{
-
-
double x = 2 ;
-
int y = 2;
-
double myNumber= 0;
-
-
-
myNumber = mypow ( x , y );
-
-
calculateNumber(myNumber);
-
-
return 0;
-
}
-
-
-
double mypow ( double x1, int y1)
-
{
-
-
double x = 2;
-
-
int perfectNumber = 0;
-
-
-
-
-
for (; perfectNumber <=1000; perfectNumber++)
-
{
-
for (int z = 2; z <perfectNumber; z++)
-
{
-
-
for (int y = 2 ; y < perfectNumber; y++)
-
{
-
if (perfectNumber == ((pow (x,z - 1)) * ((pow (x, y)) - 1)))
-
cout<< " " <<perfectNumber<<endl<<endl;
-
}
-
}
-
}
-
-
-
-
-
return perfectNumber;
-
}
-
-
void calculateNumber (double perfectNumber)
-
{
-
//double perfectNumber;
-
cout << " " << perfectNumber;
-
-
-
}
thanks for your help.. the program outputs many numbers that arent actually perfect numbers.. :S
4 4277
What do you think is not a perfect number but the program lists it as such?
It's probably here: - 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.
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.
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)
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
|
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
|
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++?
|
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>
|
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,...
| |
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.
|
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...
|
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...
|
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,...
|
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...
|
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,...
| |
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...
|
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...
|
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();...
|
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...
|
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
|
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...
| |