This is a rather difficult challenge for me as I am new to Python. How would I write a program in python based off this sequence function:
and does the following:
It asks for the value of the sequence and returns the corresponding number. For example, the number corresponding to the 10th value of the sequence is 7. I'd like to be able to do this for values over 300,000,000.
So, the final product would look like this:
Enter a value: 4
[7]
Any ideas where to start? I have a framework to generate sequences where (x) would be to put a mathematical equation or numbers, but I'm not exactly sure how to go from here or how to implement the "Enter a value" portion:
import math
def my_deltas():
while True:
yield (x)
yield (x)
def numbers(start, deltas, max):
i=start
while i<=max:
yield i
i+=next(deltas)
print(','.join(str(i) for i in numbers((x), my_deltas(),(x))))
If you're looking to have your computer keep track of over 300,000,000 elements of a sequence, if each is a 4 byte integer, you'll need at least 300,000,000 * 4bytes, or over 1.1GB of space to store all the values. I assume generating the sequence would also take a really long time, so generating the whole sequence again each time the user wants a value is not quite optimal either. I am a little confused about how you are trying to approach this exactly.
To get a value from the user is simple: you can use
val = input("What is your value? ")wherevalis the variable you store it in.EDIT:
It seems like a quick and simple approach would be this way, with a reasonable number of steps for each value (unless the value is prime...but lets keep the concept simple for now): You'd need the integer less than or equal to the square root of n (
start_int = n ** .5), and from there you test each integer below to see if it divides n, first convertingstart_intto an integer withstart_int = int(start_int)(which gives you the floor of start_int), like so:while (n % start_int) != 0: start_int = start_int - 1, decrement by one, and then setb = start_int. Something similar to findd, but you'll have to figure that part out. Note that%is the modulus operator (if you don't know what that is, you can read up on it, google: 'modulus python'), and**is exponentiation. You can then return a value with thereturnstatement. Your function would look something like this (lines starting with#are comments and python skips over them):There are many introductory guides to Python, and I would suggest you go through one first before tackling implementing a problem like this. If you already know how to program in another language you can just skim a guide to Python's syntax.
Don't forget to choose this answer if it helped!