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. if the running time of T exceeds log(n) for a function T with natural number n when n=10^j with j=1, derive an algorithm whose running time does not exceed log(n) for n=10^j, and define a new efficient recursive function U that optimizes the function T using this algorithm. 5. compute and output S using U.

import random f = lambda x: random.randint(0, 10) def f(a, b, c, m, i): return (a*i*i + b*i + c)//m def T(a, b, c, m, n): return sum([f(a, b, c, m, i) for i in range(1, n+1)]) def U(a, b, c, m, n): return (n * (2*a*n + b) // 2) // m def main(): for i in range(1, 5): print("T(10^" + str(i) + ") = " + str(T(f(), f(), f(), f(), 10**i))) print("U(10^" + str(i) + ") = " + str(U(f(), f(), f(), f(), 10**i))) main()

Questions about programming?Chat with your personal AI assistant