일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- algeomath
- 삼각함수의그래프
- 확률실험
- 리만합
- 프랙탈
- 파이썬
- 재귀함수
- 작도
- 수학탐구
- 지오지브라
- python
- counting sunday
- Geogebra
- 시뮬레이션
- 큰수의법칙
- 피타고라스 정리
- 정오각형
- 이항분포
- 큰 수의 법칙
- 구분구적법
- 블록코딩
- 몬테카를로
- 제곱근의뜻
- project euler
- 알지오매스
- 오일러
- 하합
- 상합
- 프로젝트 오일러
- java
- Today
- Total
이경수 선생님의 수학실험실
problem19 (Counting Sundays) 본문
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 |