MATHEMATICAL COMPUTING These problems are to be done on the

MATHEMATICAL COMPUTING:

These problems are to be done on the coding program Spyder.

Sieve of Eratosthenes:

(1) Let L be a list and let T be a target. Write code to find the index of the second occurrence of T in the list.   If there is no such occurrence,

print a message to that effect. Try doing 2 versions, one using the builtin list methods and one using your own loop(s).

(2) Write code to check if a list L is a \"palindrome\", i.e., it read the same from back to front or from front to back.   This is easy with builtins, try it

with loops.

(3)The goal is to find all the prime numbers less than or equal to some natural number maxn.

We have a list that tells us if any of the numbers 0..maxn are \"marked\". It can be an array of booleans. The zero and one slots are not used.

Start with 2 and mark all the multiples of 2.

Find the next unmarked number d, mark all of its multiples,

and so on.

You can stop when 2d>maxn, or there are no more unmarked numbers.

To print out the primes, print out all unmarked numbers.

Solution

program.py -------

# -*- coding: utf-8 -*-
\"\"\"
Created on Tue Feb 7 08:23:22 2017

@author: ASUS
\"\"\"
\"\"\"Answer - 01 \"\"\"
def findSecondOccurrence(l, T):
index = -1
count = int(0)
for i in range(0,len(l)):
if l[i] == T:
count += 1
if count == 2:
index = i
break
if index == -1:
print(\"\ There is no such occurrence\ \")
else:
print(\"\ Index of the second occurrence of \"+str(T)+\" : \"+str(index))
  
\"\"\"Answer - 02\"\"\"
def checkPalindrome(l):
k = int(len(l)) - int(1)
flag = True
for i in range(0,int(len(l)/2)):
if l[i] != l[k]:
flag = False
break
k -= 1
if flag == True:
print(\"\ Palindrome\")
else:
print(\"\ Not Palindrome\")
  
if __name__ == \'__main__\':
l = list(map(int,input().split(\' \')))
T = int(input(\"Enter target : \"))
findSecondOccurrence(l,T)
p = list(map(int,input().split(\' \')))
checkPalindrome(p)

MATHEMATICAL COMPUTING: These problems are to be done on the coding program Spyder. Sieve of Eratosthenes: (1) Let L be a list and let T be a target. Write code
MATHEMATICAL COMPUTING: These problems are to be done on the coding program Spyder. Sieve of Eratosthenes: (1) Let L be a list and let T be a target. Write code

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site