이경수 선생님의 수학실험실

problem19 (Counting Sundays) 본문

Project Euler

problem19 (Counting Sundays)

(이경수) 2019. 2. 16. 18:29

problem19 (Counting Sundays)


You are given the following information, but you may prefer to do some research for yourself.

  • 1 Jan 1900 was a Monday.
  • Thirty days has September,
    April, June and November.
    All the rest have thirty-one,
    Saving February alone,
    Which has twenty-eight, rain or shine.
    And on leap years, twenty-nine.
  • A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.

How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)






In Python

# Counting Sunday
import time

startTime = time.time()
days = 1
count = 0
for year in range(1900, 2001):
for month in range(1, 13):
if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12:
days += 31
elif month == 4 or month == 6 or month == 9 or month == 11:
days += 30
elif month == 2 and year % 4 == 0 and year != 1900:
days += 29
else:
days += 28
if days % 7 == 0 and year > 1900:
count += 1
print(count)
print(time.time() - startTime, "seconds")

Run time: 0.0008108615875244141 seconds



In Java:


//Euler19 Counting Sunday

package project_euler11_20;

public class Euler19 {

public static void main(String[] args) {

int days = 1;

int count = 0;

long startTime = System.currentTimeMillis();

for (int i = 1900; i < 2001; i++) {

for (int j = 1; j < 13; j++) {

switch(j) {

case 1: case 3: case 5: case 7: case 8: case 10: case 12:

days += 31; break;

case 4: case 6: case 9: case 11:

days += 30; break;

case 2:

if(i % 4 == 0 && i != 1900) {

days += 29;

} else {

days += 28;

} break;

}

if (days % 7 == 0 && i > 1900) {

count++;

}

}

}

System.out.println(count);

long endTime = System.currentTimeMillis();

System.out.println((double)(endTime - startTime)/(double)1000 + "seconds");

}


}

Run time : 0.0seconds


solution: 171


'Project Euler' 카테고리의 다른 글

Problem 21(Amicable numbers)  (0) 2019.04.14
Problem 20 (Factorial digit sum)  (0) 2019.04.07
problem18 (Maximum path sum I)  (0) 2019.02.16
problem17 (Number letter counts)  (0) 2019.02.14
problem16 (Power digit sum)  (0) 2019.02.14
Comments