Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › OEIS Sequence in Python?
New Posts  All Forums:Forum Nav:

OEIS Sequence in Python?

post #1 of 3
Thread Starter 
This is a rather difficult challenge for me. 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 only know a little python, but knowing how to translate the sequence into code is my main problem.
Edited by EmeraldPanda - 3/31/13 at 6:28am
post #2 of 3
Do you have the exact formula? The pages refers to other pages and is overall confusing (to me).

Once you have your formula to convert a given number to its corresponding value, just apply it to the input, Python (above 2.5 I think) will handle it regardless of size since it will use bignum variable type that can work with numbers bigger than 32 bit no sweat).
Code:
def convert(number):
    result = formula using number as its variable
    return result

Then you can call it like any other function.
Back in Black
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 965 BE (C3) Biostar TA790GX A3+ Sapphire HD 5770 (v2) CORSAIR XMS3 4GB DDR3 
Hard DriveOptical DriveOSMonitor
WD Caviar Black 640GB Sony Optiarc CD/DVD RW Windows 7 Ultimate x64 NEC MultiSync LCD 1960NXi 
KeyboardPowerCaseMouse
Microsoft Comfort Curve Keyboard 2000 Corsair 650TX Cooler Master Storm Scout Logitech MX 400 Laser 
  hide details  
Reply
Back in Black
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 965 BE (C3) Biostar TA790GX A3+ Sapphire HD 5770 (v2) CORSAIR XMS3 4GB DDR3 
Hard DriveOptical DriveOSMonitor
WD Caviar Black 640GB Sony Optiarc CD/DVD RW Windows 7 Ultimate x64 NEC MultiSync LCD 1960NXi 
KeyboardPowerCaseMouse
Microsoft Comfort Curve Keyboard 2000 Corsair 650TX Cooler Master Storm Scout Logitech MX 400 Laser 
  hide details  
Reply
post #3 of 3
So I found some Maple code in the page you linked to, and had some spare time, so I converted it for you:
Code:
from math import sqrt

def A063655(n):
# We loop from the floor of the square root
# all the way to 1 with a step of -1
# remember that range's second paramter
# is the lower bound and is never reach (so 0 for 1)
# Replaced floor with int cast (performance hack)
    for i in range(int(sqrt(n)), 0, -1):  
        j = int(n / i)    # Same int cast hack
        if (i * j == n):  # Once the condition passes
            return i + j    # we have found our result


# ---------------------------------
# We use our new function here

# Create a list from 1 to 100
# Number [space] Result

for n in range(1, 101):
    print n, A063655(n)

# We get the result for a realy big number
# Note how Python handles it with no effort
# Very likely the Python interpreter uses
# its 'bignum' integer type to do so.

print A063655(300000000)


As far as I understand, this is a numeric method (rather exact one however) and doesn't really use a formula like I stated above.

I used a cast to Int as a replacement for math.floor, while this isn't necessary its a very common hack that you will find pretty much everywhere (Google it, the performance difference is huge).

Also tried to comment most lines that require Python knowledge so you can understand them, the algorithm itself is not made by me and I don't exactly know all the details for it.
Edited by Chris++ - 3/31/13 at 7:31pm
Back in Black
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 965 BE (C3) Biostar TA790GX A3+ Sapphire HD 5770 (v2) CORSAIR XMS3 4GB DDR3 
Hard DriveOptical DriveOSMonitor
WD Caviar Black 640GB Sony Optiarc CD/DVD RW Windows 7 Ultimate x64 NEC MultiSync LCD 1960NXi 
KeyboardPowerCaseMouse
Microsoft Comfort Curve Keyboard 2000 Corsair 650TX Cooler Master Storm Scout Logitech MX 400 Laser 
  hide details  
Reply
Back in Black
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD Phenom II X4 965 BE (C3) Biostar TA790GX A3+ Sapphire HD 5770 (v2) CORSAIR XMS3 4GB DDR3 
Hard DriveOptical DriveOSMonitor
WD Caviar Black 640GB Sony Optiarc CD/DVD RW Windows 7 Ultimate x64 NEC MultiSync LCD 1960NXi 
KeyboardPowerCaseMouse
Microsoft Comfort Curve Keyboard 2000 Corsair 650TX Cooler Master Storm Scout Logitech MX 400 Laser 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › OEIS Sequence in Python?