Although the question doesn't ask for a function to help, using a function will be easier. Here's a complete program listing. There are many other ways of doing this task.
count starts at
0 instead of 1 (and the loop test compensates for this).
Because count is incremented before
the value of product is updated (in contrast to
the loop in fact.cpp) the value returned is correct.
A good invariant is product == (count-1)! This is true the first time the loop test is evaluated since count is 1, and 0! (zero factorial) is 1, the value of count.
When the loop terminates, we konw count == num + 1 because the loop increments count by one. This means that product == [(num + 1) - 1]! by the invariant, or num!, the desired answer.
divisor += 2 is changed to
divisor += 1 the function IsPrime will still
return the correct values. However, it will test many more
divisors than necessary since it will now test all numbers
between 3 and limit rather than just odd numbers. Since even
numbers have already been checked before the loop, checking
even divisors is not necessary.
IsPrime(1) evaluates to
false (the first if statement takes care of this, checking for
numbers less than 2. This is correct, 1 is not considered
prime. The first (and only even) prime number is 2.
divisor <= limit and
n % divisor is false.
if (n % divisor == 0)
{
return false;
}
else
{
return true;
}
This can be shorted considerably to
return ! (n % divisor = 0);
You should think about this; the expresion n % divisor == 0
is either true or false. If it is true, then n has
a divisor so IsPrime should return false. Hence the use of !. The
expression could be written as n % divisor != 0 but
this is hard to think about (at least for me it is).
1024 512 256 128 64 32 16 8 4 2 1 0
If expo starts at 1,000, it has the values (in order)
1000 500 250 125 62 31 15 7 3 1 0
When divisor divides n evenly, the function returns false, which is correct since a divisor is found. If the loop terminates because the loop test is false, then the invariant tells us there are no divisors in the interval [2..divisor). Since divisor > limit when the loop terminates, all numbers up to the square root of n have been checked -- since none divide n, it must be prime.
num and
count . Since both are incremented by one each time through the
loop, one of them is superfluous. Defining and using both is ok, but
it's possible to dispense with count and use a test of
numLines*10+ 10
so that this isn't recalculated everytime through
the loop. Smart compilers can ``optimize'' this kind of recomputation
away, however.
Output (one number per line):
1024 512 256 128 64 32 16 8 4 2 1 0 0 0 0 0 0 0 0 ...
This is an infinite loop since 0/2 == 0, so the once k becomes zero, it remains zero forever (until the user stops the program).
const int FEET_PER_MILE = 5280;
const int OUNCES_PER_LB = 16;
const double E = 2.718;
const double GRAMS_PER_POUND = 453.59;
const double FT_LBS_PER_ERG = 1.356e7;
Most people try to find the fourth Thursday using a loop, this is trickier. It's trickier because of initialization of the Date object used, if it's initialized to November 1, getting the code right is tricky (use an invariant and try).
Alternatively, you could find the number of days in a month, and loop that way:
In either case, to find the every month in a year, just call MoreFridays in a loop:
Since all of these must be true (and) when the loop terminates, the negation of the conjunction yields a disjunction (use OR).