Python: Pentagonal number function confusion - python

I keep getting
TypeError: 'int' object is not callable
in my program.
The purpose of this program is to list all the Pentagonal Numbers from 1 to 100, doing 10 per line. So far I have this:
def getPentagonalNumber(n):
p = n(3*n-1) // 2
print(p)
def printPentagonalNumber(numberOfPentagonal):
number_of_Pentagonal = 100
NUMBER_OF_PENTAGONAL_PER_LINE = 10
count = 0
n = 1
while count < numberOfPentagonal:
if getPentagonalNumber(n):
count += 1 # increase count
if count % NUMBER_OF_PENTAGONAL_PER_LINE == 0:
print()
n =+1
def main():
print("The first 100 pentagonal numbers are")
printPentagonalNumber(100)
main()

The error is due to this line:
p = n(3*n-1) // 2
Python does not implicitly do multiplication.
Instead, you have to use:
p = n*(3*n-1) // 2
However, there are certain other things that are not correct in your code. Let us know if you need further help.

Related

How do I create a loop that squares an integer until it reaches a certain number of digits?

The task:
Using python, create a function that uses a loop to determine how many
times a number can be squared until it reaches at least a twenty eight
digit number.
Ex: It takes three times to reach a three digit number, starting with
2: 2^2 = 4, 4^2 = 16, 16^2 = 256
Below is what I've tried:
def squaring():
maximum = int(len(28))
for i in range(3, maximum):
print(i**2)
I've also tried:
def squaring():
i = 3
while len(str(i)) < 28:
i = i ** 2
print(i)
Your first example doesn't work, as len is not defined for integers.
Your second example is actually quite right: You can add a counter to check how many times you've multiplied the original number:
def squaring():
counter = 0
i = 3
while len(str(i)) < 28:
i = i ** 2
counter += 1
print(i)
return counter
print(f'The number of squares: {squaring()}')
It's not really necessary to convert anything to a string. It can be done like this:
def count_squares(n):
count = 0
while n < 10**27+1:
n *= n
count += 1
return count
print(count_squares(3))
Output:
6
A Update to the function you created, just passing a parameter to pass number of your choice and adding a iterator 'i' in order to get x-times the number is squared
def squaring(input_number):
number = input_number
i = 0
while len(str(number)) <= 28:
number = number ** 2
i += 1
print(number)
return str(i)+'x'
You can transform the number into a string and you evaluate the len len(str(i**2)) and put a condition
You can Use While True until your Conditions occur!
‍‍
_iter = 0
i = 2
while True:
i = i**2
_iter += 1
if len(str(i)) >=28:
break
print(_iter)

Looping a function that changes it's output every iteration

I'm new to python and I'm trying to create something that if the number is odd for example 7 it does this (7 * 3+1) and if it is even for example 6 it does this (6/2) but the problem is that I want to loop this but it updates the number every output from the example earlier 7 I want this to turn to (22/2) and so on and if the number 1 is reached it stops.
output = []
number = 7
def mat(self):
if (self % 2) == 0:
even = self / 2
return even
else:
odd = self * 3 + 1
return odd
while mat(number) != 1:
output.append(mat(number))
output.append(mat(mat(number))
print(output)
this part doesn't work because it will never reach 1 and it only has 1 output (22) starting from the number 7 :
while mat(number) != 1:
output.append(mat(number))
output.append(mat(mat(number))
To update number, you need to assign it:
number = mat(number)
The best place to do this is in the while loop:
while number != 1:
number = mat(number)
For an exercise like this, it makes sense to just print the value on each iteration rather than trying to create an array of results:
while number != 1:
print(number)
number = mat(number)
Just update the value
For while loop:
a = 0
while a<10:
print("Hello World")
a = a + 1
For for loop:
a = 0
for i in range(10):
print("Hello World")
a = a + 1
if a >= 10:
break

I want to find the sum of the number which i have

I have some code where I must find the multiples of number 3 and then summarize them
I have done the first job, I mean I found all the multiples of number 3 with for loop but I can't summarize all the numbers which I found.
I have tried so many times and tried to find the solution on google, but could not find
x = 3
for number in range(1000):
if number%x == 0:
print(number)
I need now the sum of all the numbers which indicates on this code, when you run this code you can see that there is publishing only the numbers that can divide by 3 now I need the sum of them
It's easier than you think:
sum(range(0, 1000, 3))
Explanation:
range is defined as such: range([start], end[, step]) so range(0, 1000, 3) means go from 0 to 1000 in steps of 3
The sum function will sum over any iterable (which includes range)
You need a variable to hold the sum (If you are in learning stage):
x = 3
total = 0
for number in range(1000):
if number % x == 0:
print(number)
total += number # equivalent to: total = total + number
print(total)
Edit:
To answer your comment use condition or condition:
x = 3
y = 5
total = 0
for number in range(10):
if number % x == 0 or number % y == 0:
print(number)
total += number # equivalent to: total = total + number
print(total)
You could create a result variable to which you can keep adding:
result = 0
x = 3
for number in range(1000):
if number%x == 0:
result += number
print(result)
The best way is using filter and sum:
# any iterable variable
iterable_var = range(100)
res = sum(filter(lambda x: x % 3 == 0, iterable_var), 0)

Recursion function for counting the number of digits in a number?

So I know that this is something simple that can be done without a recursion function but I need to know the backside to this as I can't seem to figure out how to write this using recursion. im using this so far
n = int(raw_input("What is n? "))
def digit(n):
if n< 10:
return 1
else:
new = n/10
print 1 + digit(new/10)
return 1 + digit(new/10)
digit(n)
Now if I type in a number such as 33 then it outputs 2 but if I do a longer number then it doesn't print it properly and I was unsure as to what exactly it wrong with it.
The problem is,
new = n/10
return 1 + digit(new/10)
You are already dividing the number by 10, in new = n / 10, which reduces the last digit and you are again dividing it by 10 before calling digit. So, you are ignoring 1 digit in every recursive call.
Instead, you can simply do
return 1 + digit(n / 10)
or
new = n / 10
return 1 + digit(new)
#!/usr/bin/python
n = int(raw_input("What is n? "))
def digit(n):
if n < 10:
return 1
else:
return 1 + digit(n/10)
print digit(n)
You can do like this too.
def counter(number):
if(number == 0):
return 0
return counter(int(number/10)) + 1
number = 1998
print(counter(number)) // it will print 4

Integer returned as sum of digits?

Here is what I'm supposed to write:
a function, countDigits, which will take an integer as a parameter and return the sum of its digits (you must use a for loop). For instance, the number 123 would return 6 (1 + 2 + 3)
a main function which will count numbers starting at one, and ending when the total digits (for all the numbers) exceeds 1,000,000. So, if you want the total digits for the number 5, you would add:
1 = 1
2 = 2 + 1
3 = 3 + 3
4 = 6 + 4
5 = 10 + 5, so 15 would be the total digit count.
your program will print both the number and the digit count before it exceeds 1,000,000. This is easiest with a while loop.
I've written the code for the function countDigits, which is:
def countDigits():
value=int(input('Enter Integer: '))
str_num= str(value)
total = 0
for ch in str_num:
total += int(ch)
print(total)
However, I'm stuck as to how to write the main function. Can anyone point me in the right direction?
EDIT
Here is my revised countDigits function:
def countDigits(value):
str_num= str(value)
total = 0
for ch in str_num:
total += int(ch)
print(total)
a one-liner:
factorial_digit_sum = lambda x: sum( sum(map(int, str(a))) for a in range(1,x+1) )
If you EVER write real code like this, Guido will hunt you down. Was kind of a fun brain teaser to golf, though.
For a real answer:
def countDigits(number):
return sum(map(int, str(number)))
def main():
total = 0
count = 1
while total <= 1000000:
total += countDigits(count)
total -= countDigits(count) # you want the number BEFORE it hits 1000000
print("Summing the digits of {}! yields {}".format(count, total))
main()
The problem in your code is that your countDigits function requests user input. You should change that to accepting the integer as a parameter. It also prints the result instead of returning it.
As #Blorgbeard mentioned in the comments, change countDigits to accept an integer as input. Also, return the total from it.
In the main function, read input, call countDigits and add them in a while loop until the total is greater than 1,000,000
def countDigits(value):
str_num= str(value)
total = 0
for ch in str_num:
total += int(ch)
return total
grandTotal = 0
while ( grandTotal < 1000000 ):
value=int(input('Enter Integer: '))
grandTotal += countDigits(value)

Categories

Resources