From ae631d3c0a44e76741a5cde1a1b80ee258923b70 Mon Sep 17 00:00:00 2001 From: Seongbeom Park Date: Mon, 7 Mar 2022 22:00:25 +0900 Subject: [PATCH] finish level5/dodge-the-lasers --- README.md | 9 +++++---- .../dodge-the-lasers/constraints.txt | 0 {others => level5}/dodge-the-lasers/readme.txt | 0 .../dodge-the-lasers/solution.py | 18 ++++++++---------- 4 files changed, 13 insertions(+), 14 deletions(-) rename {others => level5}/dodge-the-lasers/constraints.txt (100%) rename {others => level5}/dodge-the-lasers/readme.txt (100%) rename {others => level5}/dodge-the-lasers/solution.py (57%) diff --git a/README.md b/README.md index 3b6085e..487e9f3 100644 --- a/README.md +++ b/README.md @@ -28,11 +28,12 @@ * Completed in: 6 hrs, 49 mins, 26 secs. * DP & Optimization -## Encrypted message -CFcSBwgCCBoHRhkKU1cGAA4AGU5YQR5THBwNFwoGGAxTQQMQVBUSBg4EAAwQRhUQVBUHFAQTGRpT QQMQVBkPERkECQAWDVwXX1BGEwgJBAwCBFRVHQRGUlFBShwaDVZTGBUFVUdBShsVA1tZBwNGUlFB ShoVB1wXX1BGFAQOSklOQR5HGh5AVRY= - -## Others ### dodge-the-laser +* Completed in: 2 days, 20 hrs, 22 mins, 39 secs. * Reference * [Beatty sequence](https://en.wikipedia.org/wiki/Beatty_sequence) * How to find A001951 A Beatty sequence: a(n)=floor(n\*sqrt(2)), stackexchange, https://math.stackexchange.com/questions/2052179/how-to-find-sum-i-1n-left-lfloor-i-sqrt2-right-rfloor-a001951-a-beatty-s + +## Encrypted message +CFcSBwgCCBoHRhkKU1cGAA4AGU5YQR5THBwNFwoGGAxTQQMQVBUSBg4EAAwQRhUQVBUHFAQTGRpT QQMQVBkPERkECQAWDVwXX1BGEwgJBAwCBFRVHQRGUlFBShwaDVZTGBUFVUdBShsVA1tZBwNGUlFB ShoVB1wXX1BGFAQOSklOQR5HGh5AVRY= + diff --git a/others/dodge-the-lasers/constraints.txt b/level5/dodge-the-lasers/constraints.txt similarity index 100% rename from others/dodge-the-lasers/constraints.txt rename to level5/dodge-the-lasers/constraints.txt diff --git a/others/dodge-the-lasers/readme.txt b/level5/dodge-the-lasers/readme.txt similarity index 100% rename from others/dodge-the-lasers/readme.txt rename to level5/dodge-the-lasers/readme.txt diff --git a/others/dodge-the-lasers/solution.py b/level5/dodge-the-lasers/solution.py similarity index 57% rename from others/dodge-the-lasers/solution.py rename to level5/dodge-the-lasers/solution.py index f3dc8df..3891747 100644 --- a/others/dodge-the-lasers/solution.py +++ b/level5/dodge-the-lasers/solution.py @@ -2,25 +2,23 @@ import math from decimal import * def solution(s): - getcontext().prec = 10000 - sqrt_2 = Decimal(2).sqrt() + getcontext().prec = len(s)+1 + return str(beatty_sum_sqrt2(int(s))) - return str(beatty_sum(sqrt_2, int(s))) - -def beatty_sum(r, n): +def beatty_sum_sqrt2(n): if n == 0: return 0 if n == 1: return 1 - floor_r_n = int(math.floor(r*n)) - - return natural_number_sum(floor_r_n) - 2 * natural_number_sum(floor_r_n - n) - beatty_sum(r, floor_r_n - n) + floor_r_n = int(Decimal(2*(n**2)).sqrt()) + + return natural_number_sum(floor_r_n) - 2 * natural_number_sum(floor_r_n - n) - beatty_sum_sqrt2(floor_r_n - n) def natural_number_sum(n): return n*(n+1)/2 def ten(n): - return 10**n + return str(10**n) tests = [ ['77', '4208'], @@ -34,7 +32,7 @@ tests = [ ['8', '47'], ['9', '59'], ['10', '73'], - [ten(100), '0'] + [ten(100), '70710678118654752440084436210484903928483593768847403658833986899536623923105351942519376716382078638821760123411090095254685423841027253480565451739737157454059823250037671948325191776995310741236436'] ]