Also, this inline thingy is a bit confusing, but I'll get around to what it does and means lol
Inline avoids a number of steps that modular programming necessitates but which hurt performance. It does this by directly substituting the function with its body where it would otherwise be called (if possible). It's sort of like a macro, except the compiler chooses between substitution or using the usual call. If there isn't a function call, otherwise necessary stack popping and pushing is avoided, as well as jumps which would cause a pipeline stall*. What exactly inline avoids is not really as important as the instructions per iterations you save going through the loop. If each function call adds, let's say, 10 instructions, then inline allows you to avoid that overhead by not calling. If you have only a function call in your loop and your function has 5 instructions, this cuts out (10 call inst) / (15 inst total) = ~66% of your instruction time through the loop- theoretically, your program will run in 33% of the time, or 200% faster! (It's not this simple in practice.)
However, inline is kind of pointless for this use- since the function is "called" (it's not really called, since it's inline) only once, you might as well eliminate the IsPrime function entirely and move its body to where it's being called in a functionally equivalent way. This would also force the substitution instead of hoping the compiler will be able to do it. Basically, you're doing the inlining yourself.
#include<stdio.h>
#include<Math.h>
#define MAX 20
double Primes[MAX];
int main()
{
int o = 0, num = 2, input;
while(MAX > o)
{
int i = 2;
if (2 == num) //Simple enough, 2 is prime, so yeah
{
Primes[o] = num;
o++;
}
while (sqrt(num) <= i)
{
if (num % i == 0)
{
break;
}
i++;
}
Primes[o] = num;
o++;
num++;
}
//Off to the equation
printf("Type a number within 1 and %i: ", MAX);
scanf("%i", &input);
printf("The %i perfect number is: %.0lf", input,
(pow(2, Primes[input-1]-1) * (pow(2, Primes[input-1])-1))); //Equation for perfect numbers
return 0;
}
I haven't tested this, but I hope it gives you the gist of what inline does. Also, a little C tidbit- notice I flipped some of the conditions around. I try to write my conditions with the constant on the left, so if you forget a "=", it doesn't try to assign instead of comparing. If you write if(2 = num), you'll get a clear compiler error. If you write if(num = 2), the problem will be harder to figure out.
*
Modern CPUs step different parts of an instruction through each part of the CPU concurrently, like you step your clothing through the wash-dry-fold process of doing laundry. A pipeline stall caused by a jump (function call) is roughly equivalent in this analogy to having to pull out all your clothing and run them through again, so you lose the time you invested in the two loads that were running consecutively with the one that caused the jump. Since modern CPUs have a bunch of stages, the instruction time lost from these cancelled stages can be costly.