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
printf("Not Found:");
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) { 

printf("Please enter a number:\n"); 
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

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.

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...

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

@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

commented: Insult? Where? Keep up the attitude and we can show you an insult. -2
commented: unnecessary negative rep +7

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?

we're not mindreaders.

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

.

commented: I wasn't cranky, I was stating a fact, and a conjecture. -2

help please...

#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

commented: 19 posts, code tags FAIL! -4

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


.

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

.

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. :)
.

Member Avatar for JohnApps

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
printf("Not Found:");
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]

>>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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.