home bbs files messages ]

Forums before death by AOL, social media and spammers... "We can't have nice things"

   comp.programming      Programming issues that transcend langua      57,431 messages   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]

   Message 56,762 of 57,431   
   Dmitry A. Kazakov to Richard Heathfield   
   Re: Another little puzzle   
   14 Dec 22 14:35:19   
   
   From: mailbox@dmitry-kazakov.de   
      
   On 2022-12-14 14:10, Richard Heathfield wrote:   
   > On 14/12/2022 1:06 pm, Dmitry A. Kazakov wrote:   
   >> On 2022-12-14 13:24, Stefan Ram wrote:   
   >>>    Given n times of the 24-hour day, print their average.   
   >>>   
   >>>    For example, the average of "eight o'clock" and   
   >>>    "ten o'clock" (n=2) would be "nine o'clock".   
   >>   
   >> You probably missed to require the interesting part: doing all that in   
   >> the modular type (modulo 24) arithmetic:   
   >>   
   >>     20 + 5 = 1 (mod 24)   
   >   
   > ...which will give you the wrong answer. Chase that goose!   
      
   Right, you must count the wrap-ups. E.g.   
      
   for I in Times'Range loop   
       New_Sum := Old_Sum + Times (I);   
       if Old_Sum > New_Sum then -- Wrapped   
          Count := Count + 1;   
          if Count = n then -- Each n wrap-ups can be discarded   
             Count := 0;   
          end if;   
       end if;   
       Old_Sum := New_Sum;   
   end loop;   
      
   In the end you have to evaluate   
      
       (Old_Sum + Count * 24) / n  (mod 24)   
      
   where Count < n is the wrap-ups count.   
      
   --   
   Regards,   
   Dmitry A. Kazakov   
   http://www.dmitry-kazakov.de   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   

[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]


(c) 1994,  bbs@darkrealms.ca