more efficient method of dealing with large numbers in Python? [closed]

2024/11/15 15:23:45

I have a question in Python which I have created an answer for it, but I am trying to achieve better efficiency for the answer.

I cant use functions, recursions, only basic stuff..

The question is:

For the number 3 power 2209 there are 1000 digits. find 12 sequential numbers that it's sum is the maximal.

For example: 5 power 36 equals 14551915228366851806640625. The 12 sequential numbers that product the maximal sum are 836685180664.

sumOfBig=0
Big=""
x=5**36
strp=str(x)
s=len(strp)
print(x)
print()
for i in range(s-11):new=strp[i:i+12]l=0for j in new:l=l+int(j)print(i)print(new)print(l)print()if l>sumOfBig:sumOfBig=lBig=new
print(Big)
print(sumOfBig)

Do you guys have any ideas for better code?

Answer

well, you can have more efficient way of summing the 12 sequential numbers. you can keep track of 12 sequential numbers, pop/subtract the oldest(leftmost) one from the subset's sum, push/add the newest(rightmost) one.

also, sum(iterable) is a built-in function.

my new code with only basic list and for-loop:

x = 5 ** 36
num_list = [int(i) for i in str(x)]
sumOfBig = last_sum = sum(num_list[:12])
maximal_index = 0for i, n in enumerate(num_list[12:]):last_sum = last_sum + n - num_list[i]if last_sum > sumOfBig:maximal_index = i+1sumOfBig = last_sumprint num_list[maximal_index:maximal_index+12] #[8, 3, 6, 6, 8, 5, 1, 8, 0, 6, 6, 4]
https://en.xdnf.cn/q/119297.html

Related Q&A

MLM downline distribution count

I make my first MLM software and I think I managed to code how to get the points from the downline even though it is a recursive problem I didnt use recursion and I might refactor to a recursive versio…

Can someone please explain to me the purpose of the asterisk in Python? [duplicate]

This question already has answers here:What does asterisk * mean in Python? [duplicate](5 answers)How are pythons unpacking operators * and ** used?(1 answer)Closed 5 years ago.For instance, can some…

Linear Programming with cvxpy

I would like to ask you regarding on the Linear Program for optimization.I have an objective function, and constraint functions as below,variables(x1, x2, x3, x4, x5, x6) are quantities of the products…

Program runs forever without giving an error when plotting data only on continent [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

KeyError while perfoming solve of two equation

1st i need to get two equation of two longest line length i put lenghths with eq in list like these [( length 1 , eq 1 ) ,.....] sort list with reverse get two equation of two longest line when run the…

Most Pythonic way to merge two dictionnaries having common key/value pair

I have two lists of python dictionnaries : l1 = [{"id":1, "name":"A"}, {"id":2, "name":"B"}] l2 = [{"id":1, "full_name":&…

How to close a while True loop instantly Python

I have a problem ... How can i press P on my keyboard and close the entire program faster ( i would like instantly ) ? The script that i made runs in a loop ( Loop B ) and checks for an image on deskt…

How to replace a value in a list

the program asks user to enter 5 unique number, if the number is already in the list, ask for a new number. after 5 unique numbers have been entered, display the listnumbers = [1,2,3,4,5] count = 0 ind…

How To Reverse A Nested Python List

Given the following nested list,myList=([1,[2,3],[[4,5,[6],7],8,9]])I want to reverse it to be converted into:myList= [[[4, 5, [6], 7], 8, 9], [2, 3], 1]How do I do that? Thanks.

Extract street address from a string

Is there any way to extract a street address from a string (say, email) using python? The address does not come in a set format. It can come without state, zip code, city, but I can guess and supply t…