Generation

generate functionSun, 05 Mar 2023

# 1. Define f(a, b, c, m, i) = Floor[(a*i^2+b*i+c)/m]. 2. define a function T to find S = Sum[f(a, b, c, m, i), {i, 1, n}]. 3. define the values of a, b, c, and m randomly as positive integers less than or equal to 10. 4. for n=10^j with j=1, if the running time of T exceeds log(n) for function T with natural number n, derive an algorithm whose running time does not exceed log(n) for n=10^j, and define a new function U that optimizes function T using this algorithm. 5. compute and output S using U.

``````#!/usr/bin/env python

import math
import time
import random

def f(a, b, c, m, i):
return int((a*i*i+b*i+c)/m)

def T(a, b, c, m, n):
s = 0
for i in range(1, n+1):
s += f(a, b, c, m, i)
return s

def U(a, b, c, m, n):
return int((a*n*(n+1)*(2*n+1)/6+b*n*(n+1)/2+c*n)/m)

for j in range(1, 11):
n = int(math.pow(10, j))
a = random.randint(1, 10)
b = random.randint(1, 10)
c = random.randint(1, 10)
m = random.randint(1, 10)
s = 0
t1 = time``````