VBA: Write a sub that can figure out, given a user-suppliedinteger n, how many prime numbers are there within 1 and n and willlist them on a sheet in an EXCEL workbook.
Solution
import xlwt
from xlwt import Workbook
def isPrime(num):
for i in range(2,num):
if(num%i == 0):
return False
return True
def excel_write(arr):
wb = Workbook()
sheet = wb.add_sheet(‘Sheet’)
sheet.write(0, 0, ‘Prime Numbers’)
for x in range(1,len(arr)):
sheet.write(x, 0, arr[x])
wb.save(‘prime.xls’)
print(‘Successfully saved in prime.xls’)
no = int(input(‘Input the Number : ‘))
prime_arr = []
for x in range(2,no):
if(isPrime(x)):
prime_arr.append(x)
excel_write(prime_arr)
CODEEXECUTION:
EXCEL SAVEDFILE:
RESTART:
OR
OR