Project Euler problem 19 - How many Sundays fell on the first
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
5
down vote
favorite
Here's the problem:
How many Sundays fell on the first of the month during the twentieth
century (1 Jan 1901 to 31 Dec 2000)?
And here's my code:
#include <stdio.h>
int main(void)
int months[12] = 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ;
int year=1901;
int dayoftheweek=2; //put a 2 because the 1 jan 1901 is tuesday. 0-sunday 6-Saturday
int sunday=0;
int firstsundays=0;
while(year<=2000)
if(year%4==0) //checking if it's a leap year
months[1]=29;
for (int i = 0; i < 12; ++i)
for (int d = 1; d <= months[i]; d++)
if(dayoftheweek==7) //reset the week
dayoftheweek=0;
if(dayoftheweek==sunday && d==1)
firstsundays++;
dayoftheweek++;
months[1]=28;
year++;
printf("There are %d Sundays that fell on the first of the monthn", firstsundays);
return 0;
What could I improve?
c programming-challenge datetime
add a comment |Â
up vote
5
down vote
favorite
Here's the problem:
How many Sundays fell on the first of the month during the twentieth
century (1 Jan 1901 to 31 Dec 2000)?
And here's my code:
#include <stdio.h>
int main(void)
int months[12] = 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ;
int year=1901;
int dayoftheweek=2; //put a 2 because the 1 jan 1901 is tuesday. 0-sunday 6-Saturday
int sunday=0;
int firstsundays=0;
while(year<=2000)
if(year%4==0) //checking if it's a leap year
months[1]=29;
for (int i = 0; i < 12; ++i)
for (int d = 1; d <= months[i]; d++)
if(dayoftheweek==7) //reset the week
dayoftheweek=0;
if(dayoftheweek==sunday && d==1)
firstsundays++;
dayoftheweek++;
months[1]=28;
year++;
printf("There are %d Sundays that fell on the first of the monthn", firstsundays);
return 0;
What could I improve?
c programming-challenge datetime
add a comment |Â
up vote
5
down vote
favorite
up vote
5
down vote
favorite
Here's the problem:
How many Sundays fell on the first of the month during the twentieth
century (1 Jan 1901 to 31 Dec 2000)?
And here's my code:
#include <stdio.h>
int main(void)
int months[12] = 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ;
int year=1901;
int dayoftheweek=2; //put a 2 because the 1 jan 1901 is tuesday. 0-sunday 6-Saturday
int sunday=0;
int firstsundays=0;
while(year<=2000)
if(year%4==0) //checking if it's a leap year
months[1]=29;
for (int i = 0; i < 12; ++i)
for (int d = 1; d <= months[i]; d++)
if(dayoftheweek==7) //reset the week
dayoftheweek=0;
if(dayoftheweek==sunday && d==1)
firstsundays++;
dayoftheweek++;
months[1]=28;
year++;
printf("There are %d Sundays that fell on the first of the monthn", firstsundays);
return 0;
What could I improve?
c programming-challenge datetime
Here's the problem:
How many Sundays fell on the first of the month during the twentieth
century (1 Jan 1901 to 31 Dec 2000)?
And here's my code:
#include <stdio.h>
int main(void)
int months[12] = 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 ;
int year=1901;
int dayoftheweek=2; //put a 2 because the 1 jan 1901 is tuesday. 0-sunday 6-Saturday
int sunday=0;
int firstsundays=0;
while(year<=2000)
if(year%4==0) //checking if it's a leap year
months[1]=29;
for (int i = 0; i < 12; ++i)
for (int d = 1; d <= months[i]; d++)
if(dayoftheweek==7) //reset the week
dayoftheweek=0;
if(dayoftheweek==sunday && d==1)
firstsundays++;
dayoftheweek++;
months[1]=28;
year++;
printf("There are %d Sundays that fell on the first of the monthn", firstsundays);
return 0;
What could I improve?
c programming-challenge datetime
edited Jan 25 at 22:35
asked Jan 25 at 22:07
Alex_n00b
89111
89111
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
5
down vote
accepted
The loops could be clearer:
- The outermost loop is actually spread across three lines:
int year=1901
,while(year<=2000)
, andyear++
. It should be written as afor
loop. i
should be renamed tomonth
orm
.- The exceptional case for leap years is awkward: you sometimes clobber the February entry at the top of the loop, then reset it at the end. I'd consider such mutations to be a hack that makes the code harder to understand. It also makes the code harder to maintain, since you have hard-coded
29
and28
in scattered places.
As for the algorithm itself⦠you should be able to do better than advancing a day at a time through an entire century. Why not advance a month at a time?
#include <stdio.h>
static const int *month_lengths(int year) year % 400 == 0)) ?
LEAP_YEAR : NON_LEAP_YEAR;
int main(void)
int first_sundays = 0;
int day_of_the_week = 2; // 0 = Sun, 6 = Sat. 1 Jan 1901 was a Tuesday.
for (int year = 1901; year <= 2000; ++year)
const int *lengths = month_lengths(year);
for (int month = 0; month < 12; month++)
if (day_of_the_week == 0)
first_sundays++;
day_of_the_week = (day_of_the_week + lengths[month]) % 7;
printf("There are %d Sundays that fell on the first of the monthn",
first_sundays);
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
accepted
The loops could be clearer:
- The outermost loop is actually spread across three lines:
int year=1901
,while(year<=2000)
, andyear++
. It should be written as afor
loop. i
should be renamed tomonth
orm
.- The exceptional case for leap years is awkward: you sometimes clobber the February entry at the top of the loop, then reset it at the end. I'd consider such mutations to be a hack that makes the code harder to understand. It also makes the code harder to maintain, since you have hard-coded
29
and28
in scattered places.
As for the algorithm itself⦠you should be able to do better than advancing a day at a time through an entire century. Why not advance a month at a time?
#include <stdio.h>
static const int *month_lengths(int year) year % 400 == 0)) ?
LEAP_YEAR : NON_LEAP_YEAR;
int main(void)
int first_sundays = 0;
int day_of_the_week = 2; // 0 = Sun, 6 = Sat. 1 Jan 1901 was a Tuesday.
for (int year = 1901; year <= 2000; ++year)
const int *lengths = month_lengths(year);
for (int month = 0; month < 12; month++)
if (day_of_the_week == 0)
first_sundays++;
day_of_the_week = (day_of_the_week + lengths[month]) % 7;
printf("There are %d Sundays that fell on the first of the monthn",
first_sundays);
add a comment |Â
up vote
5
down vote
accepted
The loops could be clearer:
- The outermost loop is actually spread across three lines:
int year=1901
,while(year<=2000)
, andyear++
. It should be written as afor
loop. i
should be renamed tomonth
orm
.- The exceptional case for leap years is awkward: you sometimes clobber the February entry at the top of the loop, then reset it at the end. I'd consider such mutations to be a hack that makes the code harder to understand. It also makes the code harder to maintain, since you have hard-coded
29
and28
in scattered places.
As for the algorithm itself⦠you should be able to do better than advancing a day at a time through an entire century. Why not advance a month at a time?
#include <stdio.h>
static const int *month_lengths(int year) year % 400 == 0)) ?
LEAP_YEAR : NON_LEAP_YEAR;
int main(void)
int first_sundays = 0;
int day_of_the_week = 2; // 0 = Sun, 6 = Sat. 1 Jan 1901 was a Tuesday.
for (int year = 1901; year <= 2000; ++year)
const int *lengths = month_lengths(year);
for (int month = 0; month < 12; month++)
if (day_of_the_week == 0)
first_sundays++;
day_of_the_week = (day_of_the_week + lengths[month]) % 7;
printf("There are %d Sundays that fell on the first of the monthn",
first_sundays);
add a comment |Â
up vote
5
down vote
accepted
up vote
5
down vote
accepted
The loops could be clearer:
- The outermost loop is actually spread across three lines:
int year=1901
,while(year<=2000)
, andyear++
. It should be written as afor
loop. i
should be renamed tomonth
orm
.- The exceptional case for leap years is awkward: you sometimes clobber the February entry at the top of the loop, then reset it at the end. I'd consider such mutations to be a hack that makes the code harder to understand. It also makes the code harder to maintain, since you have hard-coded
29
and28
in scattered places.
As for the algorithm itself⦠you should be able to do better than advancing a day at a time through an entire century. Why not advance a month at a time?
#include <stdio.h>
static const int *month_lengths(int year) year % 400 == 0)) ?
LEAP_YEAR : NON_LEAP_YEAR;
int main(void)
int first_sundays = 0;
int day_of_the_week = 2; // 0 = Sun, 6 = Sat. 1 Jan 1901 was a Tuesday.
for (int year = 1901; year <= 2000; ++year)
const int *lengths = month_lengths(year);
for (int month = 0; month < 12; month++)
if (day_of_the_week == 0)
first_sundays++;
day_of_the_week = (day_of_the_week + lengths[month]) % 7;
printf("There are %d Sundays that fell on the first of the monthn",
first_sundays);
The loops could be clearer:
- The outermost loop is actually spread across three lines:
int year=1901
,while(year<=2000)
, andyear++
. It should be written as afor
loop. i
should be renamed tomonth
orm
.- The exceptional case for leap years is awkward: you sometimes clobber the February entry at the top of the loop, then reset it at the end. I'd consider such mutations to be a hack that makes the code harder to understand. It also makes the code harder to maintain, since you have hard-coded
29
and28
in scattered places.
As for the algorithm itself⦠you should be able to do better than advancing a day at a time through an entire century. Why not advance a month at a time?
#include <stdio.h>
static const int *month_lengths(int year) year % 400 == 0)) ?
LEAP_YEAR : NON_LEAP_YEAR;
int main(void)
int first_sundays = 0;
int day_of_the_week = 2; // 0 = Sun, 6 = Sat. 1 Jan 1901 was a Tuesday.
for (int year = 1901; year <= 2000; ++year)
const int *lengths = month_lengths(year);
for (int month = 0; month < 12; month++)
if (day_of_the_week == 0)
first_sundays++;
day_of_the_week = (day_of_the_week + lengths[month]) % 7;
printf("There are %d Sundays that fell on the first of the monthn",
first_sundays);
answered Jan 26 at 0:14
200_success
123k14143401
123k14143401
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186008%2fproject-euler-problem-19-how-many-sundays-fell-on-the-first%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password