Generate and Enter Value for OEIS Sequence in Python?

649 Views Asked by At

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:

http://oeis.org/A063655

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))))
2

There are 2 best solutions below

3
Paul Vasiu On

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? ") where val is 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 converting start_int to an integer with start_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 set b = start_int. Something similar to find d, 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 the return statement. Your function would look something like this (lines starting with # are comments and python skips over them):

def find_number(value):
    #using value instead of n
    start_int = value ** .5
    start_int = int(start_int)
    while (n % start_int) != 0:
      #same thing as start_int = start_int - 1
      start_int -= 1
    b = start_int

    #...more code here

    semiperimeter = b + d
    return semiperimeter

#Let's use this function now!

#store
my_val = input("Enter a value: ")

my_number = find_number(my_val)

print my_number

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!

0
cdlane On
from math import sqrt, floor

def A063655(n):
    for i in range(floor(sqrt(n)), 0, -1):
        j = floor(n / i)
        if i * j == n:
            return i + j

if __name__ == '__main__':

    my_value = int(input("Enter a value: "))

    my_number = A063655(my_value)

    print(my_number)

USAGE

> python3 test.py
Enter a value: 10
7
> python3 test.py
Enter a value: 350000
1185
>