Lyrk
Lyrk

Reputation: 2000

How to reduce this if statement?

if(year%400==0 || (year%100!=0 && year%4==0)) 

statement is in the form of a or (b' and c)

Does this reduce to a or (b or c') ?

if(year%400==0 || (year%100==0 || year%4!=0))

Is there a mistake in this reduction? (I thought it was correct but these two does not give the same results.)

Upvotes: 0

Views: 79

Answers (2)

Stitt
Stitt

Reputation: 494

Consider the example of year = 100. Your first statement will return false, while the second will return true.

The second or statement enclosed in parentheses does not need to be, and so can be considered as a simple check of the three conditions individually.

Upvotes: 0

ikegami
ikegami

Reputation: 385496

Does this reduce to a or ( b or c' )?

No. You can see this by building a truth table.


We can change the and to an or.

  b' and c
= ( b' and c )''
= ( b or c' )'

so

a or ( b' and c )

becomes

a or ( b or c' )'

Not useful, except to show it's not equivalent to a or ( b or c' ).


We can change the and to an or using the same approach.

a or ( b' and c )

becomes

( a' and ( b or c' ) )'

Again, not useful.


It can be simplified to year % 4 == 0, as long as you only deal with the years 1901 to 2099.

Upvotes: 4

Related Questions