Board index » delphi » "Odd" TP question

"Odd" TP question

Quote
- wrote:

> Hello
> I am new at programing and there is one review question in my TP manual that
> has been bothering me for the past couple of days, ive tried many different
> things and am wondering why they dont work. maybe someone can help:

> Question:

> Write a problem that finds the sum of all odd numbers between 1 and a number
> entered by the user. (Hint:  You will need a statement inside the loop to
> check each number, using MOD 2 to determine if it is odd.)

> would someone please solve this for me and explain it. Thank you.
> I am having trouble making the program recognize odd numbers, the output
> returned is usually 0 1 0 1 0 1 and so on...

The end result appears to request that you only provide one number.  While I
won't give away a solution, perhaps some hints will help.  First you need
three variables:  one to hold the sum, one to tell how high you want to count,
and one as an index for a loop.  Let's respectively name them sum, upper, and
index.

  {upper is entered from user}
  initialize sum to 0;
  for index := 1 to upper do
    if index is an odd number then
      add it to sum;
  report results of sum to user;

That's all you have to do.  The most difficult part here (which shouldn't even
be that difficult) is figuring out how to test for odd/even using "mod 2".
Bonus hint:  In case you didn't know, the "mod" operator returns the remainder
of division.  When you learned elementary math, what can you determine by
dividing a number by 2?

Quote
> wholyCow
> wholy...@tekkenking.com

--
Scott Earnest            | SPAM protection in effect. Remove  |
setech@_ix.netcom.com    | "_" as needed for true addresses.  |
earnests@_homenet.lm.com |    UIN:1136443  EFnet:pale_blue    |
sinykal@_{*word*104}space.org  | URL: http://www.netcom.com/~setech |
 

Re:"Odd" TP question


You can use the odd-command in pascal.

var
  cointer, sum, number : integer;

begin
  sum := 0;
  read (number);
  for cointer := 1 to number do
    begin
       if odd(cointer) do
         sum := sum + cointer;
    end;
write (sum);
end;

Re:"Odd" TP question


In article <364AEE61.749AE...@rug.ac.be>, Ike.Castel...@rug.ac.be says...

Quote

>You can use the odd-command in pascal.

>var
>  cointer, sum, number : integer;

>begin
>  sum := 0;
>  read (number);
>  for cointer := 1 to number do
>    begin
>       if odd(cointer) do
>         sum := sum + cointer;
>    end;
>write (sum);
>end;

You are absolutely correct.

But there is an alternative solution which is much faster when N is large.
Suppose there is a second problem which ask the sum of 1 to 100.
Instead of using a loop to sum that one by one, we can simply use
symmetry
   1  2  3    ........    98  99 100
There are 50 pairs of {[100 + 1], [99 + 2], [98 + 3], ...}.
So the answer will be 101*50=5050.

Return back to the original problem (i.e. sum odd from 1 to N).
Here is an alternative solution using similar symmetry trick, as below.

Function sum_odd_by_symmetry ( N : integer) : integer;
var
  tmp, M, sum : integer;
begin
  if odd(N) then
    begin
      tmp := (N + 1) div 2;   { N + 1 equals sum of such a pair}
                       { Divide it by 2 to count only half of odd & even}
      sum := tmp * tmp; { tmp also equal the number of such pairs}
    end
  else
    begin
      M := N - 1;      {if N is even, count it down by 1 to get the odd}
      tmp := N div 2;  { N  equals sum of such a pair}
                       { Divide it by 2 to count only half of odd & even}
      sum := tmp * tmp; { tmp also equal the number of such pairs}
    end;
  sum_odd_by_symmetry := sum;
end;

Check with both approaches, they both return the same answer :)
You can even do it by hand without a computer if you understand the trick.

Other Threads