python count no of digits in a non negative integer - python

I am writing a program for counting number of digits in a non negative integer whose upper bound is 10^{1000000} and has a time limit of 12 seconds
I have declared an array with size 1000000 and the indexes 0,1,2...,1000000 correspond to the number of digits of the number represented by 10^{array index}.
However I am getting Time Limit Exceeded for input ranges close to 10^{10000}.
Please find inline the code for the same:
def Number(x):
a=[0]*(1000000)
for j in xrange(len(a)):
#print j
a[j]=j+1
i=0
flag=0
flagc=0
counter=0
while(i<len(a)-1):
#print x
if (10**i)==x:
print a[i]
flag=1
break
elif x>(10**i) and x<(10**(i+1)):
print a[i]
flag=1
break
i=i+1
if (i==len(a)-1 and flag==0 and x==(10**i)):
print a[i]
number=int(input())
Number(number+1)
Please help as to how to handle large input values for the above as time limit got exceeded is coming for inputs close to 10^10000

OP has stated in a comment that his problem is he needs to take a numerical input in the form of a string, add 1 to it, and return the number of digits.
Adding 1 to a number will change the number of digits IF AND ONLY IF the string is composed completely of 9s. A simple test can achieve this. If it is all 9s, output the string length plus 1, otherwise output the unchanged string length.
num = input()
length = len(num)
if num == '9' * length: # checks if string is all 9's
print(length + 1)
else:
print(length)
By eliminating the need to convert to int and back, we save a lot of time. This code executes in fractions of a second.

The simplest way to do this in pure python is to use len(number) where "number" has the required base.

To the commenters saying that this is an integer, the data being discussed in this question would overflow an integer, and these numbers would have to be represented as a strings in memory due to their length.

As shown above in the comments, math.log10 is not accurate enough for large numbers. Try a while loop:
n = 10 ** 10000 - 1
count = 0
while n > 0:
n //= 10
count += 1
print(count)
Output:
10000
Changing n to 10 ** 10000 outputs 10001 as expected.
EDIT: I found something very surprising (to me, at least):
len(str(n))
is actually EXTREMELY fast! I tested for numbers up to 10^1000000, and it finishes executing in a matter of seconds!

Related

Error with my random number generation code

My code it generates a random number it checks if it is in the list if it is it generates another random number and then it checks if it is equal or not and shows the result and repeats the process. But when I start the code it generated the normal number but it repeats the numbers and it shouldn't repeat the number. What do I do?
from random import randint
import os
n = 0
numsort = 14564487
attempt = 0
numbers = []
while n < 100:
num = randint(10000000, 99999999)
if num in numbers:
num = randint(10000000, 99999999)
numbers.append(num)
attempt += 1
if num == numsort:
print(f'{num}' + '\033[32m' + ' Right number' + '\033[0m')
print(f'After {attempt} attempts it was')
break
if num != numsort:
print(f'{num}' + '\033[31m' + ' Wrong number' + '\033[0m')
print(f'Attempt # {attempt}')
os.system('clear')
The issue here is a simple one. The primary problem is with this code snippet:
num = randint(10000000, 99999999)
if num in numbers:
num = randint(10000000, 99999999)
numbers.append(num)
Translating this to English, we have:
Generate a random seven-digit integer
If the integer is in our list of numbers then regenerate it and add it to the list
The problem you have here is that you have no guarantee that the regenerated number won't also be in the list. The chance of this happening will grow as the list grows. And, since your loop variable is never updated, this list will grow infinitely.
To fix this problem, you have two possible solutions. For small lists, you can continue on with your strategy of regenerating numbers and replace the if with a while:
num = randint(1000000, 9999999)
while num in numbers:
num = randint(1000000, 999999999)
numbers.append(num)
Note that this will only work while the desired size of your list is much smaller than the range of possible values being generated. As you fill up the list, the chance of collisions between the range of possible values and actual values will increase, causing the while loop to run longer.
Alternatively, you can use sample to choose your values, which would replace the entire loop:
numbers = random.sample(range(1000000, 10000000), n)
Note here that the use of range means that this is actually just as space efficient as the previous solution and will guarantee unique values.

Is there a way of checking if a value is evenly divisible by any value in a given data set?

I am currently attempting to write a simple bit of python 3 code that allows the user to tell the program which prime number it wants to find and then return the number to the user. I have hit a roadblock because I need to check if the "newprime" value is in fact a prime, which requires me to divide the value by all the previous prime numbers ("primes") and then checking if the answer is a whole number or not. Here is the current program
import numpy
primes = [2]
print("the how many-th prime would you like to calculate?")
numberOfPrime = int( input() )
x = 0
while x <= numberOfPrime:
notprime = 0 #placeholder, should be all numbers which when divided by any of the set "primes" gives a whole number
while newprime == notprime:
newprime = primes[x] + 1
primes.append(newprime)
print(primes)
x += 1
print(primes[numberOfPrime], " is the ", numberOfPrime, "-th prime number", sep="")
As you can see, I added a comment where I would have to insert the missing part.
How do I best approach this?

digit_sum error, iterating through a string and then changing to an integer, python

On Codecademy.com I am on section 8, "Practice Makes Perfect", exercise 4 they ask you to:
Write a function called digit_sum that takes a positive integer n
as input and returns the sum of all that number's digits. For example:
digit_sum(1234) should return 10 which is 1 + 2 + 3 + 4. (Assume that
the number you are given will always be positive.)
def digit_sum(n):
total = 0
n = str(n)
for i in n:
total += int(n[i])
return total
Since you have to only add one of the digits, I wrote a program that changed the number into a string and then iterate over each digit. When I iterate, I add each number to a local varible, total, by accessing the digit and then changing the number back into an integer. After writing my program, I was thrown the error "string indices must be integers, not str". This is the error you get when you try to add a number to a string. I was confused by this so I researched if other people were having the same problem. I also asked my programming teacher and we still could not figure it out. Can someone explain what I am doing wrong?
It does not tell me what line is throwing the error either. I forgot to mention but on the website they give a solution, but it is not at all in the terms of what I am doing, I am just severely confused of why this error is occurring.
Just change it to:
def digit_sum(n):
total = 0
n = str(n)
for i in n:
total += int(i)
return total
In python, unike c++ and other languages:
the for loop iterates the element itself and not its index.
Your code iterates over the characters of your string, not the indices. Change to:
def digit_sum2(n):
total = 0
n = str(n)
for i in range(0, len(n)):
total += int(n[i])
return total

What is wrong with my code? I cannot grasp the concept of

decimal = input("Please insert a number: ")
if decimal > 256:
print "Value too big!"
elif decimal < 1:
print "Value too small!"
else:
decimal % 2
binary1 = []
binary0 = []
if decimal % 2 == 0:
binary1.append[decimal]
else:
binary0.append[decimal]
print binary1
print binary0
So Far, I want to test this code, it says on line 13:
TypeError: builtin_function_or_method' object has no attribute
__getitem__.
I don't understand why it is wrong.
I would like to convert the decimal number into binary. I only wanted to try and get the first value of the input then store it in a list to use then add it to another list as either a 0, or a 1. And if the input doesn't divide by 2 equally, add a zero. How would I do this?
binary1.append[decimal]
You tried to get an element from the append method, hence triggering the error. Since it's a function or method, you need to use the appropriate syntax to invoke it.
binary1.append(decimal)
Ditto for the other append call.
In response to your binary question. It is possible to brute-force your way to a solution quite easily. The thinking behind this is we will take any number N and then subtract N by 2 to the biggest power that will be less than N. For instance.
N = 80
2^6 = 64
In binary this is represented as 1000000.
Now take N - 2^6 to get 16.
Find the biggest power 2 can be applied to while being less than or equal to N.
2^4 = 16
In binary this now represented as 1010000.
For the actual code.
bList = []
n = int(raw_input("Enter a number"))
for i in range(n, 0, -1): # we will start at the number, could be better but I am lazy, obviously 2^n will be greater than N
if 2**i <= n: # we see if 2^i will be less than or equal to N
bList.append(int( "1" + "0" * i) ) # this transfers the number into decimal
n -= 2**i # subtract the input from what we got to find the next binary number
print 2**i
print sum(bList) # sum the binary together to get final binary answer

Determining whether a number is prime or not

I know it's been discussed many times; I've read it, but somehow I can't get it.
I want to write a program that determines if the entered number is prime or not.
One of the implementations I found somewhere on the Internet:
from math import *
def main():
n = abs(input("Enter a number: "))
i = 2
msg = 'is a prime number.'
while i <= sqrt(n):
if n % i == 0:
msg = 'is not a prime number.'
i = i + 1
print n, msg
main()
A couple of questions here:
In the above, what is i, and why does it have a starting value of 2?
What does i = i + 1 do in this program?
How does the interpreter know when to print 'is a prime number.' even though it is out of the body loop?
A prime number is a number that's only divisible by 1 and itself. The method it's using is to try dividing your candidate number n by every other number from 2 up to itself; however if any number i is a divisor of your number n then so is n / i and at least one of them is less than or equal to sqrt(n) therefore we need only test up to sqrt(n) inclusive. In practice we need only test the divisors that are actually prime themselves but since we don't have a list of primes to hand we'll test every one.
what in the above i is? and why it got a 2 starting value?
i is the potential factor of n we're testing. It starts with 2 because we don't care if 1 divides n (and trivially it will) because the prime definition allows / expects that.
what is the i = i + 1 statement, in this concrete example for? Can't see its use in the program.
It's incrementing the i value at the end of the loop defined by the while i <= sqrt(n); it means we advance i to test the next candidate divisor of n.
and finally, how python knows when to print 'is a prime number.' although it is out of the body loop?
We initialise msg to "is a prime number" and if we find any divisor then we change it to "is not a prime number" inside the loop. If the loop doesn't find a divisor, or if the loop never runs, we'll use the initial value we set which is "is a prime number". Incidentally you could break out of the loop when you find a divisor; there's no point carrying on the test after that.
As another aside you probably want to compute sqrt(n) outside the while and store than in a variable to use in the while - you may be recalculating the square root for every iteration, which is relatively expensive.
I've added comments on the sides to explain what each line does:
from math import * # imports everything from the math module
def main():
n = abs(input("Enter a number: ")) # gets input from the user
i = 2 # starts off at 2 because all input is divisble by 1
msg = 'is a prime number.' # the message is initially set
while i <= sqrt(n):
if n % i == 0: # if 'i' divides evenly into n
msg = 'is not a prime number.' # only set if it isn't a prime
i = i + 1 # increases 'i' by 1 so it can check every value up to the square-root of 'n' (to see if it divides evenly)
print n, msg
main()
The program has to go through every value of i (up to the square-root of n) so that every possible factor is checked.
This is sort of a rough-prime checker, and inefficient for large numbers that aren't prime:
If the input was a number like 1234567890, it will iterate through every number up to the square root of that number, which is 35147 (rounded up).
Using return statements break the loop, so the first number you check, 2, it is declared not prime since it is evenly divisible by 2.
By using return, it will stop the function, and save you 35,146 calculations. That isn't a massive number (for computers, at least) but it's still more memory-efficient and takes less time.
def isPrime(n):
'''Checks if 'n' is prime.'''
from math import sqrt
if n == 0 or n == 1:
return False
else:
for check in range(2, int(sqrt(n))+1):
if n % check == 0: return False
return True

Categories