Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
What is the logic behind
int("string", integer)
for example:
int("220", 3)
yields
24
The optional integer is the numeric base to use in converting the string (defaults to base 10).
220 base 3 = 2 * (3**2) + 2 * (3**1) + 0 * (3**0)
= 2*9 + 2*3 + 0*1
= 18 + 6 + 0
= 24
the int() method in python takes two inputs. First is the string you want to evaluate the second is the base.
In your example the base is three therefore:
1*0 = 0
3*2 = 6
9*2 = 18
0 + 6 + 18 = 24
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
counttonum = 1
countnum = input("[•] Provide A Number :")
while counttonum < countnum:
print("[", counttonum, "] : Number = ", counttonum)
counttonum +=1
I was trying to make a counting tool that counts up to the provided number from the “input()” function.
For example:
providedNumberFromInput = 5
output = 1
2
3
4
5
And it’ll stop if the provided number is reached. Please help me.
You are very close to solution. Problem is that input() returns value as string so you will need to convert it. And also if you want to include entered number use <= instead of <
while counttonum <= int(countnum):
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 months ago.
This post was edited and submitted for review 8 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I am trying to compute the square root of 2 (up to nearly 10 million digits) with the following code
import os
os.chdir('/home/username/Desktop')
def sqroot(a, digits):
a = a * (10**(2*digits))
x_prev = 0
x_next = 1 * (10**digits)
while x_prev != x_next:
x_prev = x_next
x_next = (x_prev + (a // x_prev)) >> 1
return x_next
sqrt2 = sqroot(2,10000000)
file = open("Root_2",'w')
file.write(sqrt2)
file.close()
The problem is, I cannot write it to a file as an integer (TypeError: write() argument must be str, not int). I tried to convert it to a string but got an OverflowError as it is too big to convert to a string. Is there any way to work around this?
If you write to the file like the following:
n = 10**10000000
with open("Root_2",'w') as f:
print(n, file=f)
It will run without errors. Change n to the 10000000 digits number you get from your function.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
The code is to find the factorial of each individual value in an array and then find the sum of them together .An example would be [1,2,3,4], which will then be 1!+2!++3!+4!=33.A single integer equal to the desired sum, reduced modulo . The issue is that when it comes on to large numbers(just my assumption) it results in "Terminated due to timeout" status
At first I used a for loop to go through each value in the array. Thinking that it may be a search issue I used for in range to ensure it has a set range. Sadly that still hasn't solved the problem. I now assume that it has to be a problem with factorial since multiplication is
def factModSum(arr):
sum=0
for i in range (0,len(arr)):
sum=sum+factorial(arr[i])
return sum%(10**9)
Example 1:
Input: 1 2 3 4
output: 33
Expected output: 33
Example 2:
Input:2 3 5 7
Output:5168
Expected output: 33
Example 3:
Input:12 13 14
Output:884313600
Expected output: 33
At the core of it at the function works. But Im getting timeout error for some of my Test case , therefore assuming that the code is not able to process large numbers in a given time
If you are modding by 10 ** 9 you can try this:
def factModSum(arr):
return sum(factorial(i) for i in arr if i < 40) % 10**9
This is because n! with n >= 40 is congruent to 0 mod 10**9.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Why in gods name is it 57.5
compiler output
input return strings, thus 2 + 3 is 23 (the default behaviour for + with strings is to concatenate them), then the casting to int turns this into an actual 23, so finally you get 5 * 23 / 2 == 115 / 2 == 57.5.
To solve this, cast each parameter to int before doing the math operations:
int(h) * (int(a)+int(b)) / 2
You concatenate two string, then turn the result into an int. If you do int(a) + int(b) it will work properly.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I would like to do 2 char into a short.
I want to do that (c syntax):
short var = (msg[4:5]<<8) | (msg[5:6])
algo:
ord(msg[4:5) = 105 -> 0b1101001
ord(msg[5:6) = 135 -> 0b10000111
var = 0b1101001 <<8 eq 0b1101001 00000000 eq 0x6900
var |= 0b10000111 eq 0b1101001 10000111 eq 0x6987
var = 27015
So i want as result the a numeric value
If you have any solution...
Thanks
(ord(msg[4])<<8) + ord(msg[5])