From b1a0b53b4c10ac3f6c1641ddfbe7c31b20eb9929 Mon Sep 17 00:00:00 2001 From: Enkryp Date: Tue, 12 Oct 2021 17:04:18 +0530 Subject: [PATCH 1/9] Hacktoberfest: added sol for P104 Project Euler --- project_euler/problem_104/__init__.py | 0 project_euler/problem_104/sol.py | 138 ++++++++++++++++++++++++++ 2 files changed, 138 insertions(+) create mode 100644 project_euler/problem_104/__init__.py create mode 100644 project_euler/problem_104/sol.py diff --git a/project_euler/problem_104/__init__.py b/project_euler/problem_104/__init__.py new file mode 100644 index 000000000000..e69de29bb2d1 diff --git a/project_euler/problem_104/sol.py b/project_euler/problem_104/sol.py new file mode 100644 index 000000000000..03ee92b3c855 --- /dev/null +++ b/project_euler/problem_104/sol.py @@ -0,0 +1,138 @@ +""" +Project Euler Problem 104 : https://projecteuler.net/problem=104 + +The Fibonacci sequence is defined by the recurrence relation: + +Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1. +It turns out that F541, which contains 113 digits, is the first Fibonacci number for which the last nine digits are 1-9 pandigital (contain all the digits 1 to 9, but not necessarily in order). And F2749, which contains 575 digits, is the first Fibonacci number for which the first nine digits are 1-9 pandigital. + +Given that Fk is the first Fibonacci number for which the first nine digits AND the last nine digits are 1-9 pandigital, find k. + +""" + + +def check(a : int) ->bool : + ''' + Takes a number and checks if it is pandigital both from start and end + + + >>> check(123456789987654321) + True + + >>> check(120000987654321) + False + + >>> check(1234567895765677987654321) + True + + ''' + + checkLast=[0]*11 + checkFront=[0]*11 + + #mark last 9 numbers + for x in range(9): + checkLast[int(a%10)]=1 + a=a//10 + #flag + f=True + + #check last 9 numbers for pandigitality + + for x in range(9): + if( not checkLast[x+1]): + f=False + if(not f): + return f + + + #mark first 9 numbers + a= int(str(a)[:9]) + + for x in range(9): + checkFront[int(a%10)]=1 + a=a//10 + + #check first 9 numbers for pandigitality + + for x in range(9): + if( not checkFront[x+1]): + f=False + return f + + + + +def check1(a : int) ->bool: + ''' + Takes a number and checks if it is pandigital from END + + + >>> check1(123456789987654321) + True + + >>> check1(120000987654321) + True + + >>> check1(12345678957656779870004321) + False + + ''' + + checkLast=[0]*11 + + #mark last 9 numbers + for x in range(9): + checkLast[int(a%10)]=1 + a=a//10 + #flag + f=True + + #check last 9 numbers for pandigitality + + for x in range(9): + if( not checkLast[x+1]): + f=False + return f + + +def solution()->int: + """ + Outputs the answer ie the least fib number pandigital from both sides. + >>> solution() + 329468 + """ + + a=1 + b=1 + c=2 + # temporary fibonacci numbers + + a1=1 + b1=1 + c1=2 + # temporary fibonacci numbers mod 1e9 + + #mod m=1e9, done for fast optimisation + tocheck=[0]*1000000 + m=1000000000 + + for x in range(1000000): + c1= (a1+b1)%m + a1=b1%m + b1=c1%m + if(check1(b1)): + tocheck[x+3]=1 + + + for x in range(1000000) : + c= (a+b) + a=b + b=c + #perform check only if in tocheck + if(tocheck[x+3] and check(b)): + return x+3 #first 2 already done + + +if __name__ == "__main__": + print(f"{solution() = }") From ebf09ce1b345ce42525d6b9c0af777fbd6e3d140 Mon Sep 17 00:00:00 2001 From: Enkryp Date: Tue, 12 Oct 2021 17:11:26 +0530 Subject: [PATCH 2/9] bot requests resolved --- project_euler/problem_104/sol.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/project_euler/problem_104/sol.py b/project_euler/problem_104/sol.py index 03ee92b3c855..2b4ac8c84a68 100644 --- a/project_euler/problem_104/sol.py +++ b/project_euler/problem_104/sol.py @@ -11,7 +11,7 @@ """ -def check(a : int) ->bool : +def check(number : int) ->bool : ''' Takes a number and checks if it is pandigital both from start and end @@ -27,43 +27,43 @@ def check(a : int) ->bool : ''' - checkLast=[0]*11 - checkFront=[0]*11 + check_last=[0]*11 + check_front=[0]*11 #mark last 9 numbers for x in range(9): - checkLast[int(a%10)]=1 - a=a//10 + check_last[int(number%10)]=1 + number=number//10 #flag f=True #check last 9 numbers for pandigitality for x in range(9): - if( not checkLast[x+1]): + if( not check_last[x+1]): f=False if(not f): return f #mark first 9 numbers - a= int(str(a)[:9]) + number= int(str(number)[:9]) for x in range(9): - checkFront[int(a%10)]=1 - a=a//10 + check_front[int(number%10)]=1 + number=number//10 #check first 9 numbers for pandigitality for x in range(9): - if( not checkFront[x+1]): + if( not check_front[x+1]): f=False return f -def check1(a : int) ->bool: +def check1(number : int) ->bool: ''' Takes a number and checks if it is pandigital from END @@ -79,19 +79,19 @@ def check1(a : int) ->bool: ''' - checkLast=[0]*11 + check_last=[0]*11 #mark last 9 numbers for x in range(9): - checkLast[int(a%10)]=1 - a=a//10 + check_last[int(number%10)]=1 + number=number//10 #flag f=True #check last 9 numbers for pandigitality for x in range(9): - if( not checkLast[x+1]): + if( not check_last[x+1]): f=False return f From e0455d6eb9659073d46a8fb86703589a161910c4 Mon Sep 17 00:00:00 2001 From: Enkryp Date: Sat, 16 Oct 2021 17:30:08 +0530 Subject: [PATCH 3/9] pre-commit --- project_euler/problem_104/sol.py | 186 +++++++++++++++---------------- 1 file changed, 91 insertions(+), 95 deletions(-) diff --git a/project_euler/problem_104/sol.py b/project_euler/problem_104/sol.py index 2b4ac8c84a68..5f1173050853 100644 --- a/project_euler/problem_104/sol.py +++ b/project_euler/problem_104/sol.py @@ -11,128 +11,124 @@ """ -def check(number : int) ->bool : - ''' - Takes a number and checks if it is pandigital both from start and end - - - >>> check(123456789987654321) - True - - >>> check(120000987654321) - False - - >>> check(1234567895765677987654321) - True - - ''' - - check_last=[0]*11 - check_front=[0]*11 - - #mark last 9 numbers - for x in range(9): - check_last[int(number%10)]=1 - number=number//10 - #flag - f=True - - #check last 9 numbers for pandigitality - - for x in range(9): - if( not check_last[x+1]): - f=False - if(not f): - return f - - - #mark first 9 numbers - number= int(str(number)[:9]) - - for x in range(9): - check_front[int(number%10)]=1 - number=number//10 +def check(number: int) -> bool: + """ + Takes a number and checks if it is pandigital both from start and end - #check first 9 numbers for pandigitality - for x in range(9): - if( not check_front[x+1]): - f=False - return f + >>> check(123456789987654321) + True + >>> check(120000987654321) + False + >>> check(1234567895765677987654321) + True + """ -def check1(number : int) ->bool: - ''' - Takes a number and checks if it is pandigital from END + check_last = [0] * 11 + check_front = [0] * 11 + # mark last 9 numbers + for x in range(9): + check_last[int(number % 10)] = 1 + number = number // 10 + # flag + f = True - >>> check1(123456789987654321) - True + # check last 9 numbers for pandigitality - >>> check1(120000987654321) - True + for x in range(9): + if not check_last[x + 1]: + f = False + if not f: + return f - >>> check1(12345678957656779870004321) - False + # mark first 9 numbers + number = int(str(number)[:9]) - ''' + for x in range(9): + check_front[int(number % 10)] = 1 + number = number // 10 - check_last=[0]*11 - - #mark last 9 numbers - for x in range(9): - check_last[int(number%10)]=1 - number=number//10 - #flag - f=True + # check first 9 numbers for pandigitality - #check last 9 numbers for pandigitality - - for x in range(9): - if( not check_last[x+1]): - f=False - return f + for x in range(9): + if not check_front[x + 1]: + f = False + return f -def solution()->int: +def check1(number: int) -> bool: + """ + Takes a number and checks if it is pandigital from END + + + >>> check1(123456789987654321) + True + + >>> check1(120000987654321) + True + + >>> check1(12345678957656779870004321) + False + + """ + + check_last = [0] * 11 + + # mark last 9 numbers + for x in range(9): + check_last[int(number % 10)] = 1 + number = number // 10 + # flag + f = True + + # check last 9 numbers for pandigitality + + for x in range(9): + if not check_last[x + 1]: + f = False + return f + + +def solution() -> int: """ Outputs the answer ie the least fib number pandigital from both sides. >>> solution() 329468 """ - a=1 - b=1 - c=2 + a = 1 + b = 1 + c = 2 # temporary fibonacci numbers - a1=1 - b1=1 - c1=2 + a1 = 1 + b1 = 1 + c1 = 2 # temporary fibonacci numbers mod 1e9 - #mod m=1e9, done for fast optimisation - tocheck=[0]*1000000 - m=1000000000 + # mod m=1e9, done for fast optimisation + tocheck = [0] * 1000000 + m = 1000000000 for x in range(1000000): - c1= (a1+b1)%m - a1=b1%m - b1=c1%m - if(check1(b1)): - tocheck[x+3]=1 - - - for x in range(1000000) : - c= (a+b) - a=b - b=c - #perform check only if in tocheck - if(tocheck[x+3] and check(b)): - return x+3 #first 2 already done - + c1 = (a1 + b1) % m + a1 = b1 % m + b1 = c1 % m + if check1(b1): + tocheck[x + 3] = 1 + + for x in range(1000000): + c = a + b + a = b + b = c + # perform check only if in tocheck + if tocheck[x + 3] and check(b): + return x + 3 # first 2 already done + if __name__ == "__main__": print(f"{solution() = }") From 0c3c71223aea2ed6b8dc3a5bf8825c2633b84fa5 Mon Sep 17 00:00:00 2001 From: John Law Date: Mon, 2 May 2022 21:41:32 +0800 Subject: [PATCH 4/9] Update sol.py --- project_euler/problem_104/sol.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/project_euler/problem_104/sol.py b/project_euler/problem_104/sol.py index 5f1173050853..f133b8c85251 100644 --- a/project_euler/problem_104/sol.py +++ b/project_euler/problem_104/sol.py @@ -7,7 +7,6 @@ It turns out that F541, which contains 113 digits, is the first Fibonacci number for which the last nine digits are 1-9 pandigital (contain all the digits 1 to 9, but not necessarily in order). And F2749, which contains 575 digits, is the first Fibonacci number for which the first nine digits are 1-9 pandigital. Given that Fk is the first Fibonacci number for which the first nine digits AND the last nine digits are 1-9 pandigital, find k. - """ @@ -103,12 +102,12 @@ def solution() -> int: a = 1 b = 1 c = 2 - # temporary fibonacci numbers + # temporary Fibonacci numbers a1 = 1 b1 = 1 c1 = 2 - # temporary fibonacci numbers mod 1e9 + # temporary Fibonacci numbers mod 1e9 # mod m=1e9, done for fast optimisation tocheck = [0] * 1000000 From de41958895e546a2ca8f1e9fe3714fae2f9d819f Mon Sep 17 00:00:00 2001 From: John Law Date: Mon, 2 May 2022 21:57:48 +0800 Subject: [PATCH 5/9] Update sol.py --- project_euler/problem_104/sol.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/project_euler/problem_104/sol.py b/project_euler/problem_104/sol.py index f133b8c85251..8851d3bf3df2 100644 --- a/project_euler/problem_104/sol.py +++ b/project_euler/problem_104/sol.py @@ -4,9 +4,13 @@ The Fibonacci sequence is defined by the recurrence relation: Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1. -It turns out that F541, which contains 113 digits, is the first Fibonacci number for which the last nine digits are 1-9 pandigital (contain all the digits 1 to 9, but not necessarily in order). And F2749, which contains 575 digits, is the first Fibonacci number for which the first nine digits are 1-9 pandigital. +It turns out that F541, which contains 113 digits, is the first Fibonacci number +for which the last nine digits are 1-9 pandigital (contain all the digits 1 to 9, +but not necessarily in order). And F2749, which contains 575 digits, is the first +Fibonacci number for which the first nine digits are 1-9 pandigital. -Given that Fk is the first Fibonacci number for which the first nine digits AND the last nine digits are 1-9 pandigital, find k. +Given that Fk is the first Fibonacci number for which the first nine digits AND +the last nine digits are 1-9 pandigital, find k. """ From 1f88eff0d707a390c3331f991df1f6a97803d194 Mon Sep 17 00:00:00 2001 From: John Law Date: Mon, 2 May 2022 22:02:01 +0800 Subject: [PATCH 6/9] remove trailing zeroes --- project_euler/problem_104/sol.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/project_euler/problem_104/sol.py b/project_euler/problem_104/sol.py index 8851d3bf3df2..578d3ab59ebd 100644 --- a/project_euler/problem_104/sol.py +++ b/project_euler/problem_104/sol.py @@ -4,12 +4,12 @@ The Fibonacci sequence is defined by the recurrence relation: Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1. -It turns out that F541, which contains 113 digits, is the first Fibonacci number -for which the last nine digits are 1-9 pandigital (contain all the digits 1 to 9, -but not necessarily in order). And F2749, which contains 575 digits, is the first +It turns out that F541, which contains 113 digits, is the first Fibonacci number +for which the last nine digits are 1-9 pandigital (contain all the digits 1 to 9, +but not necessarily in order). And F2749, which contains 575 digits, is the first Fibonacci number for which the first nine digits are 1-9 pandigital. -Given that Fk is the first Fibonacci number for which the first nine digits AND +Given that Fk is the first Fibonacci number for which the first nine digits AND the last nine digits are 1-9 pandigital, find k. """ From 881a98efae918f56ed89e3b310139e57977ce50f Mon Sep 17 00:00:00 2001 From: John Law Date: Mon, 2 May 2022 22:03:53 +0800 Subject: [PATCH 7/9] Update sol.py --- project_euler/problem_104/sol.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/project_euler/problem_104/sol.py b/project_euler/problem_104/sol.py index 578d3ab59ebd..26a580034bd5 100644 --- a/project_euler/problem_104/sol.py +++ b/project_euler/problem_104/sol.py @@ -67,7 +67,6 @@ def check1(number: int) -> bool: """ Takes a number and checks if it is pandigital from END - >>> check1(123456789987654321) True @@ -98,7 +97,7 @@ def check1(number: int) -> bool: def solution() -> int: """ - Outputs the answer ie the least fib number pandigital from both sides. + Outputs the answer is the least Fibonacci numberpandigital from both sides. >>> solution() 329468 """ From b52efe944a10409b57998d73b46d926ae2456190 Mon Sep 17 00:00:00 2001 From: John Law Date: Mon, 2 May 2022 22:05:22 +0800 Subject: [PATCH 8/9] Update sol.py --- project_euler/problem_104/sol.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/project_euler/problem_104/sol.py b/project_euler/problem_104/sol.py index 26a580034bd5..0cda1820b63b 100644 --- a/project_euler/problem_104/sol.py +++ b/project_euler/problem_104/sol.py @@ -97,7 +97,7 @@ def check1(number: int) -> bool: def solution() -> int: """ - Outputs the answer is the least Fibonacci numberpandigital from both sides. + Outputs the answer is the least Fibonacci number pandigital from both sides. >>> solution() 329468 """ From 55f7a79ca4b1ee6fa7cdd606db49146321c456e4 Mon Sep 17 00:00:00 2001 From: John Law Date: Mon, 2 May 2022 22:06:28 +0800 Subject: [PATCH 9/9] Update sol.py --- project_euler/problem_104/sol.py | 1 + 1 file changed, 1 insertion(+) diff --git a/project_euler/problem_104/sol.py b/project_euler/problem_104/sol.py index 0cda1820b63b..0818ac401c3a 100644 --- a/project_euler/problem_104/sol.py +++ b/project_euler/problem_104/sol.py @@ -130,6 +130,7 @@ def solution() -> int: # perform check only if in tocheck if tocheck[x + 3] and check(b): return x + 3 # first 2 already done + return -1 if __name__ == "__main__":