I'm doing codeforces and found in huge number, my simple code started to make some bugs.
n = int(input())
for i in range(n):
candies = sum(map(int,input().split()))
if candies % 2 == 0:
print(int(candies/2))
else:
print(int((candies-1)/2))
when I put
1
9547116602436426 8316250877914575 2981260158260522
I expected to see '10422313819305761' but what I get is '10422313819305760'.
But in other small numbers, of all combination in odd and even numbers, it worked very well. I used math.floor instead of if as well, but it gave me same answer.
Is there any limit on huge numbers in python?
Related
My text book asks me to "write pseudo code algorithm which inputs 10 numbers. Each time a number less than zero is input, program displays which number it is, and its value. When all numbers have been input, display the average of all the negative numbers. Your algorithm should allow for the fact that there may be no negative numbers".
The issue I have is that I'm unsure on how to write pseudo code correctly, so I've written it in python code-however, that is not necessarily the task. Furthermore, I've been able to more or less to everything but the issue where I need display the average of all the negative numbers has gotten me stuck... I'm not sure how to add that into my code and I am desperate! Need this for school tomorrow!
count = 0
nums = []
while count != 10:
num = int(input ("enter a number "))
nums.append(num)
if num < 0:
print (num)
neg_nums.append(num)
count = count+1
print (neg_nums)
I actually need to print the average of all the negative numbers, however I have no clue on how I can actually code that into this...
You almost had it. Just store only the negative numbers to the list and then in the end sum all of them up and divide by the amount of negative numbers. I also changed the loop from while loop to for loop, because it makes the code clearer and more compact:
neg_nums = []
for _ in range(10):
num = int(input("enter a number "))
if num < 0:
print(num)
neg_nums.append(num)
if neg_nums:
print(sum(neg_nums) / len(neg_nums))
else:
print("no negative numbers")
We also check in the end if there were some negative numbers inputted, so we don't end up calculating 0 / 0.
I'm new to Python and testing myself with some questions. One that came up was to test whether a user inputted number was prime or not. Easy enough, but one person came up with a single line of code as a solution.
import math
num = int(input("Enter a number greater than 2 "))
if sum([True if num%factor == 0 else False for factor in ([2] + list(range(3,int(math.sqrt(num)),2)))]):
print("Number is composite")
else:
print("Number is prime")
I understand part of it: the if statement is True if (number/iterations) gives a remainder of 0, else False, where the only iterations that need to be checked are 2 -> sqrt(number) skipping all even numbers.
However, I can't work out how to sum operator at the start of the if statement works. I assume it operates on the True(1)/False(0) statements, but how does this interaction play out and how does it affect the if/else statements?
My code is a PYTHON program that identifies if a number is prime or not. When I entered 45 however, it said that 45 was a prime, even though 45 isn't a prime number. Also, every time I run the program, it prints 'Sorry, the number you have entered is not prime.' or 'The number is indeed prime!' multiple times, instead of once. How do I make it print the output statements once and how can fix the program so that it says 45 IS NOT a prime number.
n = eval(input("Enter a number to find if that number is prime: "))
a = 2
while n > a:
if ((n % a) == 0) & (a != n):
print('Sorry, the number you have entered is not prime.')
break
else:
print('The number you have entered is indeed prime!')
Because you are printing it every time. If you want to break after finding/not finding the prime number, indent one more level for the break.
Also, this does not calculate prime numbers. It calculates if its even number.
Follow the solution here
Your code has some issues. To start with, you're never updating a, so you only ever check if the number n is even.
Once you fix that, you have two indentation problems. The first is that the break line needs to be inside the body of the if statement. Indent it more so that it's inside the if block.
The second indentation issue is more subtle. If you leave the else where it is, it will print out that the number is prime every time you test a potential factor that doesn't divide the number. That's both unhelpful (since it prints a lot) and wrong (since it says the number is prime even if it will later find a factor and say it's not prime). You can fix this by unindenting the else line so that it is lined up with the while statement. Using an else after a loop is an obscure bit of Python syntax. The body of the else only runs if the condition of the loop fails. It gets skipped if the loop exits due to a break statement.
Here's all of those necessary fixes together:
while n > a:
if ((n % a) == 0) & (a != n):
print('Sorry, the number you have entered is not prime.')
break # indent this line more!
a += 1 # increment a, so you don't keep checking 2 over and over
else: # unindent this line (and the next line too)
print('The number you have entered is indeed prime!')
There are some other things that could be improved in your code, though they aren't causing it to run incorrectly. I'd recommend using int instead of eval to parse your number, and I'd use the logical-and operator and instead of the bitwise-and operator & in the if statement (though actually you don't need either, since the a != n check is redundant, as the loop would have already ended if it was true).
number = int(raw_input("Enter a number :"))
div = range(0, number)
list = []
while div <= number:
if number % div == 0:
list.append(div)
div =+ 1
print list
this is my code which I made for this exercise :
http://www.practicepython.org/exercise/2014/02/26/04-divisors.html
I am new to programming so i dont know what is wrong with my code but its not giving any output.
I guess this answer is simple enough, and looking from the down-votes people think this too. However... one only learns by asking!
There are three essential mistakes in the code that you propose, and a number of ways to make it more Pythonic.
First, and foremost, dividing by zero is not possible! So you'd want to check numbers in the range (1-number).
Based on the indentation list is printed many times, instead of only at the end of completing the while loop.
You want to avoid using list as a variable because it is a Python keyword.
Then, to make it more Pythonic my proposition is the following:
number = int(input("Enter a number :"))
output = []
for i in range(1,number+1):
if not number%i:
output.append(i)
print(output)
Note that raw_input no longer exists in Python 3.x. Also note that this way we avoid the while loop, that from experience, can easily lead to mistakes. In stead it has been replaced by automatically looping through the entries in the list generated by range(1,number).
Finally a note on range but probably also on semantics. I consider number also to be a divisor of number. To do so I have used range(1,number+1). Because, for example range(5) returns a list up to 5: [0,1,2,3,4]. I.e. it does not include 5.
Here is the code you need:
number = int(input("Enter a number :"))
list = []
# theoretically this is the limit you should test for divisors
for div in range(2, int(number/2 + 1)):
if number % div == 0:
# adds the divisor to the list
list.append(div)
div += 1
# prints all divisors in new line
for item in list:
print(item)
If you are using python 2.7, instead of input use raw_input
TASK : Write a program that prints out all the primes below 10,000.
Hint: you will need to use % which gives the remainder of a division operation.
So x=11%5 would give 1 as 11/5=2 remainder 1
I FOUND THIS CODE , can someone add annotations next to all the lines explaining what is happening.I totally don't understand this code.
numbers=[]
for i in range(0,10001):
numbers.append(" ") #We just put spaces in each box for now
numbers[0]="X"
numbers[1]="X"
firstFree=2
while firstFree<10001:
multiple=firstFree*2
while multiple<10001: #Mark all multiples of firstFree as X
numbers[multiple]='X'
multiple=multiple+firstFree
firstFree=firstFree+1
while firstFree<10000 and numbers[firstFree]=='X':
firstFree=firstFree+1
for i in range(0,10001):
if(numbers[i]!='X'):
print(i)
As you have been told, you should not learn that way. Plus, in my humble opinion, this code you have found is extremely ugly and hard to read.
This is beter example.
#Main loop, this is self explanatory.
for dividend in range(2, 10000):
#This loops through all divisors available.
for divisor in range(2, dividend):
#Check against reminder of division.
if dividend % divisor == 0:
#Prints all composites of prime number that failed against check.
print('{} equals {} * {}'.format(dividend, divisor, dividend//divisor))
#Breakes second loop.
break
#Find answer for this thing yourself. It is interesting.
else:
#Enters here when have not found division factor.
print('{} is one of the prime numbers.'.format(dividend))
Read tons of tutorials, train and learn.