How Do I Implement These Algorithms Below
Alogrithm 1: Get a list of numbers L1, L2, L3....LN as argument Assume L1 is the largest, Largest = L1 Take next number Li from the list and do the following If Largest is l
Solution 1:
Is what you mean with the first one find the largest number? Then you should use max() like
list = [1,2,4,5,3]
printmax(list)
>>> 5
This should help with the second one:
defprime_number(n):
if n > 1:
for x inrange(2,n):
if (n % x) == 0:
returnFalsebreakelse:
returnTrue
If a number is prime, then the factors are only 1 and itself. If there is any other factor from 2 to the number, it is not prime. n % x finds the remainder when n is divided by x. If x is a factor of n, then n % x has a remainder of 0.
Solution 2:
def get_algorithm_result(numbers):
largest = numbers[0]
for i in numbers:
if largest < i:
largest = i
return largest
and
defprime_number(number):
if number > 1:
for i inrange(2, number):
if (number % i) == 0:
returnFalseelse:
returnTrueelse:
returnFalse
Solution 3:
defget_algorithm_result(numb):
largest = numb[0]
for Li in numb:
if largest < Li:
largest = Li
if Li == numb[-1]:
return largest
else:
continuedefprime_number(primes):
if primes > 1:
for i inrange(3, primes):
if (primes % i) == 0:
returnFalseelse:
returnTrueelse:
returnFalse
Post a Comment for "How Do I Implement These Algorithms Below"