IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Zeller's congruence - How does it get 4


IBM Mainframe Forums -> Mainframe Interview Questions
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
ramsri

Active User


Joined: 18 Oct 2008
Posts: 380
Location: India

PostPosted: Sat Nov 10, 2012 1:03 pm
Reply with quote

Hi,

was working out this example given in Zeller's congruence page in wikipedia but don't understand it. icon_confused.gif

Quote:

However, for March 1, 2000, the date is treated as the 3rd month of 2000, so the values become
q = 1
m = 3
K = 0
J = 20
so the formula evaluates as (1 + 10 + 0 + 0 + 5 − 40) mod 7 = −24 mod 7 = 4 = Wednesday


24 mod 7 produces 3 remainder but how and why it is calculated to 4 and becomes Wednesday icon_question.gif

How to write code in COBOL or Easytrieve to achieve "something mod something" icon_question.gif

Please help. Thanks.
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Sat Nov 10, 2012 2:58 pm
Reply with quote

did You care to scroll down a few line and NOTICE
the difference between the mathematical definition of MODULO
the quirks of some software implementations
and the workaround?

if You had You would not have asked the question

icon_cool.gif

as far as COBOL is concerned no need to write anything
search the forum for may posts on integer of date
for example
here
www.ibmmainframes.com/viewtopic.php?t=49988&highlight=integerofdate

and here a simple REXX snippet to show what is going on


Code:

say "*** remainder about REXX integer division and modulo operators"
integer division 7 %  4 " 7 %  4
say "reminder         7 // 4 " 7 // 4
say ""
q   = 1 ;
say "q   = " q

m   = 3 ;
say "m   = " m

K   = 00;
say "K   = " K

J   = 20;
say "J   = " J

v1  = 13 * ( m + 1 ) %  5
say "v1  = " v1

v2  = K
say "v2  = " v2

v3  = K % 4
say "v3  = " v3

v4  = J % 4
say "v4  = " v4


h0  = q + v1 + v2 + v3 + v4 + 5*J
say "h0  = " h0

h   = h0 // 7
say "h   = " h


to get


Code:

*** remainder about REXX integer division and modulo operators
integer division 7 %  4  1
reminder         7 // 4  3

q   =  1
m   =  3
K   =  00
J   =  20
v1  =  10
v2  =  00
v3  =  0
v4  =  5
h0  =  116
h   =  4
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Sat Nov 10, 2012 5:15 pm
Reply with quote

Quote:
so the formula evaluates as (1 + 10 + 0 + 0 + 5 − 40) mod 7 = −24 mod 7 = 4 = Wednesday


24 mod 7 produces 3 remainder but how and why it is calculated to 4 and becomes Wednesda
Details are INCREDIBLY significant in IT (and in mathematics). Yes, 24 mod 7 is 3 -- but NEGATIVE 24 mod 7 is 4. This is based upon the standard definition of modular arithmetic n=pq + r where n is -24, q is 7 so p must be -4 and r becomes 4.

COBOL supports use of REMAINDER so modular arithmetic is actually very simple in COBOL. If you don't know about REMAINDER, there's a link to IBM Manuals at the top of the page and the Enterprise COBOL Language Reference manual would be the one to start reading in.
Back to top
View user's profile Send private message
Bill Woodger

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Sat Nov 10, 2012 6:25 pm
Reply with quote

Ramsri,

Is this somehow to do with your SyncSort question? If so, why didn't you answer all the questions asked of you there so you'd not need all this stuff? If you are proceeding with Cobol, re-read what enrico mentioned about integer-of-date and associated LE callable functions.
Back to top
View user's profile Send private message
ramsri

Active User


Joined: 18 Oct 2008
Posts: 380
Location: India

PostPosted: Mon Nov 12, 2012 8:56 am
Reply with quote

Hi Enrico, Robert, Bill......thanks a lot. Yes....It is related to my SORT question which I thought to workout in COBOL or Easytrieve (beginner) !

Thanks.
Back to top
View user's profile Send private message
ramsri

Active User


Joined: 18 Oct 2008
Posts: 380
Location: India

PostPosted: Mon Nov 12, 2012 5:09 pm
Reply with quote

Hi,

Please help me why I get wrong answer with Gaussian's algorithm !! For date 13th Feb 2016 I should get Saturday but it shows Friday.

Code:

********************************* Top of Data **********************************
       IDENTIFICATION DIVISION.                                                 
       PROGRAM-ID. PGM00001.                                                   
       ENVIRONMENT DIVISION.                                                   
       DATA DIVISION.                                                           
       WORKING-STORAGE SECTION.                                                 
       01  WS-DATE.                                                             
           05 WS-CC            PIC 9(02).                                       
           05 WS-YY            PIC 9(02).                                       
           05 WS-MM            PIC 9(02).                                       
           05 WS-DD            PIC 9(02).                                       
       01  WS-INT-YY           PIC 9(03).                                       
       01  WS-INT-MM           PIC 9(03).                                       
       01  WS-DOW1             PIC 9(03).                                       
       01  WS-DOW2             PIC 9(01).                                       
       01  WS-SHIFT-MONTH      PIC 9(02) VALUE ZERO.                           
           88 WS-MAR-CODE                VALUE 01.                             
           88 WS-APR-CODE                VALUE 02.                             
           88 WS-MAY-CODE                VALUE 03.                             
           88 WS-JUN-CODE                VALUE 04.                             
           88 WS-JUL-CODE                VALUE 05.                             
           88 WS-AUG-CODE                VALUE 06.                             
           88 WS-SEP-CODE                VALUE 07.                             
           88 WS-OCT-CODE                VALUE 08.                             
           88 WS-NOV-CODE                VALUE 09.                             
           88 WS-DEC-CODE                VALUE 10.                             
           88 WS-JAN-CODE                VALUE 11.                             
           88 WS-FEB-CODE                VALUE 12.                             
       01  WS-DAY-OF-WEEK      PIC 9(01) VALUE ZERO.                           
           88 WS-SUN-CODE                VALUE 0.                               
           88 WS-MON-CODE                VALUE 1.                               
           88 WS-TUE-CODE                VALUE 2.                               
           88 WS-WED-CODE                VALUE 3.                               
           88 WS-THU-CODE                VALUE 4.                               
           88 WS-FRI-CODE                VALUE 5.                               
           88 WS-SAT-CODE                VALUE 6.                               
       PROCEDURE DIVISION.                                                     
           PERFORM 01-MAIN-PARA.                                               
           STOP RUN.                                                           
       01-MAIN-PARA.                                                           
           ACCEPT WS-DATE.                                                                   
           IF   WS-DATE IS NUMERIC                                             
                PERFORM 02-CALC-PARA                                           
           ELSE                                                                 
                CONTINUE                                                       
           END-IF.                                                             
       02-CALC-PARA.                                                           
           EVALUATE WS-MM                                                       
              WHEN 01 SET WS-JAN-CODE TO TRUE                                   
              WHEN 02 SET WS-FEB-CODE TO TRUE                                   
              WHEN 03 SET WS-MAR-CODE TO TRUE                                   
              WHEN 04 SET WS-APR-CODE TO TRUE                                   
              WHEN 05 SET WS-MAY-CODE TO TRUE                                   
              WHEN 06 SET WS-JUN-CODE TO TRUE                                   
              WHEN 07 SET WS-JUL-CODE TO TRUE                                   
              WHEN 08 SET WS-AUG-CODE TO TRUE                                   
              WHEN 09 SET WS-SEP-CODE TO TRUE                                   
              WHEN 10 SET WS-OCT-CODE TO TRUE                                   
              WHEN 11 SET WS-NOV-CODE TO TRUE                                   
              WHEN 12 SET WS-DEC-CODE TO TRUE                                   
           END-EVALUATE.                                                       
           IF   WS-JAN-CODE OR WS-FEB-CODE                                     
                COMPUTE WS-INT-YY = WS-YY - 1                                   
           ELSE                                                                 
                MOVE WS-YY TO WS-INT-YY                                         
           END-IF.                                                             
           DISPLAY 'MONTH ' WS-SHIFT-MONTH                                     
           COMPUTE WS-INT-MM = ((2.6 * WS-SHIFT-MONTH) - 0.2).                 
           DISPLAY 'INT MONTH ' WS-INT-MM                                       
           COMPUTE WS-DOW1 = (WS-DD + WS-INT-MM + WS-INT-YY +                   
                          (WS-INT-YY / 4) + (WS-CC / 4) - (2 * WS-CC))         
           DISPLAY 'INT RESULT ' WS-DOW1                                       
           COMPUTE WS-DOW2 = FUNCTION MOD (WS-DOW1, 7).                         
           DISPLAY 'RESULT ' WS-DOW2                                           
           EVALUATE WS-DOW2                                                     
              WHEN 0 DISPLAY 'DAY OF WEEK FOR GIVEN DATE: SUNDAY'               
              WHEN 1 DISPLAY 'DAY OF WEEK FOR GIVEN DATE: MONDAY'               
              WHEN 2 DISPLAY 'DAY OF WEEK FOR GIVEN DATE: TUESDAY'             
              WHEN 3 DISPLAY 'DAY OF WEEK FOR GIVEN DATE: WEDNESDAY'           
              WHEN 4 DISPLAY 'DAY OF WEEK FOR GIVEN DATE: THURSDAY'             
              WHEN 5 DISPLAY 'DAY OF WEEK FOR GIVEN DATE: FRIDAY'               
              WHEN 6 DISPLAY 'DAY OF WEEK FOR GIVEN DATE: SATURDAY'             
           END-EVALUATE.                           


Output:
Code:

DAY OF WEEK FOR GIVEN DATE: FRIDAY


Thanks.
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Mon Nov 12, 2012 5:27 pm
Reply with quote

Do all the other dates come out right ?
Back to top
View user's profile Send private message
ramsri

Active User


Joined: 18 Oct 2008
Posts: 380
Location: India

PostPosted: Mon Nov 12, 2012 5:50 pm
Reply with quote

Enrico,

These are the dates I tried and its results:

Quote:

20000301 = THURSDAY (WRONG)
20040305 = TUESDAY (WRONG)
19711202 = THURSDAY (RIGHT)
20160213 = SATURDAY (RIGHT)
20121212 = WEDNESDAY (RIGHT)


Thanks.
Back to top
View user's profile Send private message
Bill Woodger

Moderator Emeritus


Joined: 09 Mar 2011
Posts: 7309
Location: Inside the Matrix

PostPosted: Mon Nov 12, 2012 6:07 pm
Reply with quote

Ramsri, third and last time. I'll repeat the question. What do you want to do when the "should be Sunday" date is not a Sunday AND, on the same record, the "should be Monday" date is not a Monday?
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Mon Nov 12, 2012 6:10 pm
Reply with quote

Ramsri, I think the basic problem is you do not pay attention to details. From your earlier post:
Quote:
Please help me why I get wrong answer with Gaussian's algorithm !! For date 13th Feb 2016 I should get Saturday but it shows Friday.
and now you post the EXACT OPPOSITE:
Quote:
20000301 = THURSDAY (WRONG)
20040305 = TUESDAY (WRONG)
19711202 = THURSDAY (RIGHT)
20160213 = SATURDAY (RIGHT)
20121212 = WEDNESDAY (RIGHT)
Back to top
View user's profile Send private message
ramsri

Active User


Joined: 18 Oct 2008
Posts: 380
Location: India

PostPosted: Mon Nov 12, 2012 6:21 pm
Reply with quote

Robert, it was my mistake.....I realized it and posted correct results.

Thanks. icon_redface.gif
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Mon Nov 12, 2012 7:00 pm
Reply with quote

So far you've shown that you do not understand the difference between a negative 24 and a positive 24, and that you post things without reviewing them to be sure they are accurate.

Furthermore, your code makes no provision for handling the negative values that can result from the equation so I suspect your 'error" results are because you have not implemented the algorithm fully and completely. When I changed your code to allow for negative values in the MOD function, the days of the week came out correctly -- even for your "error" values.
Back to top
View user's profile Send private message
ramsri

Active User


Joined: 18 Oct 2008
Posts: 380
Location: India

PostPosted: Mon Nov 12, 2012 7:25 pm
Reply with quote

Robert, implementing algorithm.......certainly confused. may I please know where do I change my code to handle negative values and how

Thanks.
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Mon Nov 12, 2012 7:28 pm
Reply with quote

Code:
       01  WS-DOW1             PIC 9(03).
must be
Code:
       01  WS-DOW1             PIC S9(03).
Back to top
View user's profile Send private message
Robert Sample

Global Moderator


Joined: 06 Jun 2008
Posts: 8696
Location: Dubuque, Iowa, USA

PostPosted: Mon Nov 12, 2012 7:32 pm
Reply with quote

Based upon the posts you've made and the questions you've asked, ramsri, you need to move to Beginner's and Students Forum instead of this one as it is more suited to your skill level and questions.
Back to top
View user's profile Send private message
ramsri

Active User


Joined: 18 Oct 2008
Posts: 380
Location: India

PostPosted: Mon Nov 12, 2012 7:59 pm
Reply with quote

Thank you.......got the results icon_biggrin.gif
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> Mainframe Interview Questions

 


Search our Forums:

Back to Top