I have successfully discovered the method to write algorithm for Collatz Conjecture and to find area using Simpson’s numerical method. But my problem is I cannot combine these program. I have trouble in storing the generated numbers under “while” loop.

How to store the list of numbers (like 8,4,2,1) to np.array([]) such that is produces area output

from __future__ import print_function
import numpy as np
from scipy.integrate import simps
from numpy import trapz
n=int(input("Enter the Number "))
count=0
a=0
a=n
while(n!=1):
if(n%2==0):
n=n//2
print(n,'\n')
count=count+1
else:
n=3*n+1
print(n,'\n')
count=count+1
y1 = np.array([n])
#This abve line should be corrected I believe
print("Collatz for ",a," count is ",count)
# Compute the area using the composite Simpson's rule.
area = simps(y1, dx=1)
print("area for Collatz",a,"=", area)

Indeed . The line creates a Numpy array. So on each iteration of the for loop you loose what is in there and get an array with on item: the last number.

Create a list before the while loop (all_ns = []) and append every number calculated in the loop to it (all_ns.append(n)). Create a Numpy array from that list just before calculating the area
(y1 = np.array(all_ns)) .

Thank you so much I am getting output now . But since area is calculated using Simpson’s 1/3 rule, is there any command for Simpson’s 3/8 rule (which is more accurate than 1/3)

area = simps(y1, dx=1)
print("area for collatz",a,"=", area)

For example if I enter 8 as collatz I get 4,2,1. According to Simpson’s 1/3 rule it considers only 4,2,1 for area calculation but how can I input the entered number in the list at initial position (list should be 8,4,2,1). How can I reframe this command.

from __future__ import print_function
import numpy as np
from scipy.integrate import simps
from numpy import trapz
n=int(input("Enter the Number "))
count=0
a=0
a=n
all_ns=[n]
while(n!=1):
if(n%2==0):
n=n//2
print(n,'\n')
count=count+1
all_ns.append(n)
y1 = np.array((all_ns))
else:
n=3*n+1
print(n,'\n')
count=count+1
all_ns.append(n)
y1 = np.array((all_ns))
print(y1)
print("Collatz for ",a," count is ",count)
# Compute the area using the composite Simpson's rule.
area = simps(y1, dx=1)
print("area for collatz",a,"=", area)