ok i have this project that i have been working on to fin harmonic,powerful,amicable and perfect numbers

i have a few but im at a road block right now and would love some assisstance.

POWERFUL NUMBERS

``````#include <iostream>
using namespace std;

int main() {
int endRange = 500, var1, var2, var3, finalVar;
for (int counter = 4; counter < endRange = 500; counter++) {
var1 = counter;
var2 = 3;
var3 = 0;
finalVar = var1;
while (var1 % 2 == 0) {
var1 /= 2;
var3 += 1;
}

while ((var2 <= var1) && (var3 != 1)) {
var3 = 0;
while (var1 % var2 == 0) {
var1 /= var2;
var3 += 1;
}
var2 += 2;
}

if (var3 > 1)
cout << finalVar << ' ';
}
}``````

AMICABLE NUMBERS

``````int endnum = 2000;
struct amicable *ami;
ami = getAmicablePairs(startnum, endnum);
if(ami!=NULL)
{
printf("\n They are:");
printf("{");
for(i = 0; i<ami->size; i++)
{
printf("{%d, %d}",ami->amicablepair[i][0], ami->amicablepair[i][1]);
}
printf("}");
}
else
return 0;

}

struct amicable *getAmicablePairs(int startnum, int endnum)
{
int i,k=0,sums1=0,sums2=0;
struct amicable *ami;
ami->size=0;
if(startnum<0 || endnum<0 || endnum>15000 || startnum>endnum)
return NULL;
else
{
for(i=startnum;i<=endnum;i++)
{
sums1=sumdivisors(i);
sums2=sumdivisors(sums1);
if(sums2==i && i<sums1)
{
ami->amicablepair[k][0]=i;
ami->amicablepair[k][1]=sums1;
k++;
}
sums2=0;
sums1=0;
ami->size=k;
}
if(ami->size==0)
return NULL;
else
return(ami);
}
}
int sumdivisors(int num)
{
int i,sum=0;
for(i=1;i<num;i++)
{
if(num%i==0)
sum+=i;
}
return(sum);
}``````

HAMONIC NUMBERS

``````#include <stdio.h>

double seq(double x);

double seq(double x) {

double temp=0;

if (x==0)
return 0;

return 1/x + seq(x-1);
}

int main() {

double x, y;

while (x != 0) {

scanf("%lf", &x);

y = seq(x);

printf("The sequence of that number is: %lf\n", y);

}

system("PAUSE");
return 0;
}``````

PERFECT NUMBERS

``````#include <limits.h>

#include <iostream>

using namespace std;

int main()

{

char buf[4096]; //where to store the numbers and than print only primes

char *cp; //char pointer

bool is_prime; //clearly a bool variable

unsigned num, i, sum; //unsigned max is around 4 billion

for (num = 1; num <= UINT_MAX; num++)

{

cp = buf;

is_prime = true;

cp += sprintf(cp ,"%u: ", num) ;//just naming the line with a number "num"

for(i = 2; i < num ; i++)

{

if(0 == (num % i))//if num evenly divides i, add it to print line

{

is_prime = false;

cp += sprintf(cp, "not prime\n");//writes everything that is prime

break;

cp += sprintf(cp, "%u ", i);//print the factors

}//close if

}//close for

cp += sprintf(cp, "\n");

if (is_prime)

printf("%s", buf);

}//close for

return 0;

}//main``````

can someone check if these are actually running correctly please. i was trying at school and it worked but my laptop is 64bit and the os is also 64bit so i dnt have a proper compiler.

anyone can recommend a decent compiler as well one that can work on a 64 bit os

Edited by WaltP: Added CODE Tags

5
Contributors
22
Replies
24
Views
7 Years
Discussion Span
Last Post by WaltP
Featured Replies
• 1
WaltP 2,905   7 Years Ago

Compilers [url=http://www.codeblocks.org/downloads.shtml]Code::Blocks[/url] [url=http://msdn.microsoft.com/vstudio/express/visualc/]MSVC++ Express[/url] [url=http://www.openwatcom.org/index.php/Main_Page]Open Watcom C++/Fortran[/url] [url=http://cc.codegear.com/Free/turbocpp]Turbo C++ Explorer 2006[/url] [url=http://www.cygwin.com/]cygwin[/url]

You don't need a 64-bit compiler for 64-bit operating systems (unless of course you want the compiler to generate 64-bit code). 32-bit compilers work great. I have 64-bit Windows 7 and use vc++ 2008 Express and Code::Blocks with MinGW. Both both without a problem.

Edited by Ancient Dragon: n/a

no its not really that. i was using borland but it dnt work on my windows 7 64bit.

toss out that old 16-bit compiler and get a modern one. Either of the two I posted or some others too. Old compilers don't work well, or not at all, on Windows 7.

thank you very much. i downloaded code:blocks seems to b working so far.

can u help me with those codes please? if ur not too busy

My first question is who wrote these programs for you? They are all written by different people. Is your project supposed to copy code from someone else?

And you need to tell us what help you need, otherwise you'll just get more code written by more people. Not a good way to become good at programming. Management, yes. Programming, no.

this is C++ code, so it's not going to work on a C compiler. You may already be aware of this, but I'm just sayin'

yeah im aware of the jephthah...thanx tho.

@ Waltp the codes r from different persons yes....on i actually got from a site and the other 2 r work of me and my group member so whats you point really?

so whats you point really?

actually, that's what they're trying to figure out from you.

you have yet to ask a question.

My point is

...
And you need to tell us what help you need, otherwise you'll just get more code written by more people.

And we're not going to write your code.

And let me guess, you wrote AMICABLE and HAMONIC...

for such a passive-aggressive snark, you need to step off my nuts.

@Waltp u know i dnt need ur insults....ok i think its about time i delete my account from this site.....i wasnt looking for anyone to write my code

Edited by mauryoung: n/a

unnecessary negative rep
Insult? Where? Keep up the attitude and we can show you an insult.

no ones insulting you dude. in fact, we'd really love to help you. Don't worry about Walt, he's often cranky, it's not personal.

but think about it. you've come into a C forum, and dumped a pile of C++ code, with a vague comment for "someone check this out" or "give assistance"

i mean, what do you want us to do for you? you haven't asked a question that can be answered. where are you stuck? what problem are you having? what is not working? what are you trying to do?

and anyhow, you cant delete accounts here. they will last into perpetuity. ;)

.

Edited by jephthah: n/a

I wasn't cranky, I was stating a fact, and a conjecture.

``````#inlcude <stdio.h>
main()
{
double sum;
int i,n;
printf("Please enter the value of n.\n\n\n");
scanf("%d",&n);

sum=0.0;

for(i=1;i<=n+1;i++)
sum=sum+1/i;
printf("The n value for harmonic series is %f",sum);

return(0);
}
``````

wen i run this i only get 0.000000 can someone tell me whats wrong? this one is harmonic series

Edited by pritaeas: Fixed formatting

19 posts, code tags FAIL!

try changing your summation line to ensure a floating point value is being calculated correctly

``sum = sum + 1.0 / i;``

the way it is, "1/i" will always evaluate as an int, and then be added to the double. Although i'm not certain why your result is zero, because 1/1 = 1...

anyhow, i notice you've spelled "include" wrong in your `#include <stdio.h>` statement. probably that's just a typo when you posted it here and not in your actual code.

ok i ran it after changing to sum = sum + 1.0 / i; and got 2.450000 but on the paper that was given to us states that the answer should be 2.283 would you know where i went wrong?

because you're calculating the harmonic series incorrectly.

when you try to calculate the finite summation of 1/n for all n from 1 to N, you need to only go to N... not N+1.

when you try N=5 the result should be 2.283333 but you're getting the result for N=6 which is 2.450

.

Edited by jephthah: n/a

ok thanx. it worked so would the process be similar for amicable & powerful numbers?
just wondering

well, they'll have different algorithms of course, but yes, you have to pay attention to your loops so that they iterate the proper number of times.

also, make sure any constant numbers found in a floating point formula have a decimal point in it even if it's a round number to be sure that it's not calculated as an integer and drop the fractional part. like using 1.0/i instead of 1/i

.

Edited by jephthah: n/a

thank you very much. I'm gonna take a good look at the others and then post my results.

I apologize for my behavior today

don't worry about it, it's all good. :)
.

Edited by jephthah: n/a

The following code does not compile and I am wondering where you call it from?
The other routines compile and execute correctly.

AMICABLE NUMBERS

``````int endnum = 2000;
struct amicable *ami;
ami = getAmicablePairs(startnum, endnum);
if(ami!=NULL)
{
printf("\n They are:");
printf("{");
for(i = 0; i<ami->size; i++)
{
printf("{%d, %d}",ami->amicablepair[i][0], ami->amicablepair[i][1]);
}
printf("}");
}
else
return 0;

}

struct amicable *getAmicablePairs(int startnum, int endnum)
{
int i,k=0,sums1=0,sums2=0;
struct amicable *ami;
ami->size=0;
if(startnum<0 || endnum<0 || endnum>15000 || startnum>endnum)
return NULL;
else
{
for(i=startnum;i<=endnum;i++)
{
sums1=sumdivisors(i);
sums2=sumdivisors(sums1);
if(sums2==i && i<sums1)
{
ami->amicablepair[k][0]=i;
ami->amicablepair[k][1]=sums1;
k++;
}
sums2=0;
sums1=0;
ami->size=k;
}
if(ami->size==0)
return NULL;
else
return(ami);
}
}
int sumdivisors(int num)
{
int i,sum=0;
for(i=1;i<num;i++)
{
if(num%i==0)
sum+=i;
}
return(sum);
}
``````

can someone check if these are actually running correctly please. i was trying at school and it worked but my laptop is 64bit and the os is also 64bit so i dnt have a proper compiler.

anyone can recommend a decent compiler as well one that can work on a 64 bit os[/QUOTE]

Edited by Reverend Jim: Fixed formatting

>>but my laptop is 64bit and the os is also 64bit so i dnt have a proper compiler.

Like I told you before you can use any 32-bit compiler on your laptop. You don't have to have a 64-bit compiler. Just download one of the free compilers either code::blocks with MinGW or VC++ 2008 Express. Not having a compiler is a hell of a poor excuse.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.