Reputation: 113
Ok, I have this script that i wrote:
i=1024;
a=[0]*i;
for k in range(0,i):
a[k]=(k*k*k*k-74*k*k+173) % 1000033
print a
I don't understand how to find the biggest number in the list AND its position.
Upvotes: 0
Views: 2762
Reputation: 298
lst = [1,3,4,56,2,66,20,312]
print "%d found at index %d is the max of the list" % (max(lst), lst.index(max(lst)))
Upvotes: 1
Reputation: 107786
# create the list with a list comprehension
m = [(k*k*k*k-74*k*k+173) % 1000033 for k in range(i)]
# enumerate the values and pick the largest by value
pos, val = max(enumerate(m), key=lambda (pos, val): val)
Upvotes: 3
Reputation: 42835
Just keep a running track, then you don't need the list:
largest = None
i = 1024
for k in range(i):
a = (k ** 4 - 74 * k ** 2 + 173) % 1000033
if not largest or largest[1] < a:
largest = (k, a)
print(largest)
Output:
(2, 999926)
P.S. i = 1048576
took a couple seconds and spat out:
(156865, 1000032L)
Note that it switched to long integers in there somewhere. This is with Python 2.6.1.
P.P.S. Also note that this method only finds the lowest index with the maximum. To get the highest index, replace the <
with <=
:
(843168, 1000032L)
Upvotes: 1
Reputation: 76862
Here's one way:
value = max(a)
index = a.index(value)
In your example, value is 999926 and index is 2.
Upvotes: 6