0

How to accelerate code in python

For a given natural number K, find the remainder of dividing the number (3K + 1)/2 by 8 input data format Natural number K < 1000000000 The output data format is a non-negative integer

13th Nov 2021, 4:36 AM
Azamat share
4 ответов
0
a=int(input()) print((((3**a)+1)//2)%8)
13th Nov 2021, 4:40 AM
Azamat share
0
My code
13th Nov 2021, 4:40 AM
Azamat share
0
Isn't 3K supposed to be 3 times value of K? I see you rather raised 3 to the power of K ... Give me some sample numbers for input, also the expected output. I need those to test your code. Next time try to attach code bit link rather than raw text code. We can't refer line numbers with raw text code.
13th Nov 2021, 5:26 AM
Ipang
0
To speed up some of the integer math you can substitute binary operations that are faster. Dividing by 2 is the same as a bit shift rightward. Modulo 8 is the same as masking out all but the lowest 3 bits. a=int(input()) print(((3**a+1)>>1)&7)
13th Nov 2021, 5:50 AM
Brian
Brian - avatar