0

So I have this code in python, but it wont run as numbers are too large for python to handle, I've found out that there is such thing as Cobra which works faster, almost as C++ and can handle a task like this. I've tried rewriting the code myself, but I keep getting compiling errors, I'm compiling at online compiler tio.run

Here is the original code:

def fib(n):
    a = 0
    b = 1
    for _ in range(n):
        a, b = b, a + b
    return a


def power(x, e, n):
    return pow(x, e) % n


def get_hash_sum():
    n = power(power(13371337, 0xdeadbeeffeeddeef, 0xffffffffffffffff), 0xcafecafeabcd, 0xfffffff)
    res = 0
    while n > 0:
        res += fib(n)
        n = n // 2 if n % 2 == 0 else (n - 1) // 2

    return res & 0xffffffffffffffff


def main():
    print(f'Flag is Course{{{get_hash_sum()}}}')


if __name__ == '__main__':
    main()

And here is what I've achieved trying to rewrite it based on my research on difference between python and cobra on the internet, which is pretty poor.

class Fibb
    def fib(n)
        a = 0
        b = 1
        for _ in range(n)
            a, b = b, a + b
        return a

class Pow
    def power(x, e, n)
    return pow(x, e) % n

class Hash
    def get_hash_sum()
        n = Pow.power(Pow.power(13371337, 0xdeadbeeffeeddeef, 0xffffffffffffffff), 0xcafecafeabcd, 0xfffffff)
        res = 0
        while n > 0
            res += Fibb.fib(n)
        n = n // 2 if n % 2 == 0 else (n - 1) // 2
        return res & 0xffffffffffffffff

class Program
    def main
    print(f'Flag is Course{{{Hash.get_hash_sum()}}}')
Mark
  • 27
  • 3
  • 1
    always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot). There are other useful information. – furas Jan 03 '21 at 17:51
  • Cobra just happens to use whitespace indentation instead of curly braces. But it's a separate language and doesn't understand Python code. Porting this Python to Cobra will require about the same effort as porting it to C++ or any other language. – Matthew Strawbridge Jan 04 '21 at 13:42
  • Python has a built-in modular exponentiation function. Get rid of your `power()` function and just call `pow()` with three arguments (i.e., `pow(x, e, n)`). You can also use modular arithmetic to calculate Fibonacci numbers quickly. – r3mainer Jan 05 '21 at 16:59

0 Answers0