Why does it say the printed output is longer? [closed] - python

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm trying to make a small program that prints the sum of numbers from 1 to 101 that are divisible by 5. I tried this and the output I get is just one line, but the site says the length of my printed output is 47, and it's longer than the instructor's printed output which is 4.

Don't print "the total sum of numbers divisible by 5 is". Just print the number. The checker doesn't like the English message.

As already pointed out, the sum of all integers divisible by 5 between 1 and 101 is 1050:
>>> def divisibles(start, end, divisor=5):
... for i in range(start, end+1): # Last value should be included
... if not i % divisor:
... yield i
...
>>> sum(divisibles(1, 101))
1050
>>>

Related

Reading ints in input in a Python program [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 months ago.
Improve this question
I have to take input from the user then the operator has to print what number is before the input and after the input like:
input= 3
has to print 2 and 4
I used range, did I do it wrong? I am just a beginner in Python.
number=int(input)
for num in range(number ,+ 1, -1):
print(num)
You first need to use input() to let the user register a number.
Then, simply print the number with number - 1 and number + 1.
number = int(input("What is your number? "))
print(f"{number - 1} {number + 1}")
Outputs to:
What is your number? 3
2 4
you don't need range do this task
num = int(input())
print(num-1, num+1)
Other answers which say that you don't require a loop are perfectly fine, however I want to add a little suggestion in case you need to print more than just the number before and after. Maybe (as your first implementation suggests) you want to print the 5 numbers before and after. In this case you can do:
R = 5 #range
N = int(input("Enter your number. "))
numbers = [ i for i in range(N-R,N+R+1) if i != N]
print(numbers)

Sum of digit of a number using recursion in python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 months ago.
Improve this question
Here in this program, I tried to understand but couldn't get completely.
How is this recursive function doing the sum and returning total sum of this? Please explain me in detail?
# Recursive Python3 program to
# find sum of digits of a number
# Function to check sum of
# digit using recursion
def sum_of_digit( n ):
if n < 10:
return n
return (n % 10 + sum_of_digit(n // 10)) # how this is working ?
num = 12345
result = sum_of_digit(num)
print("Sum of digits in",num,"is", result)
The best way to understand a recursive function is to dry run it.
First you need to understand what n % 10 mean is. this means the remainder of a n when divided by 10.
In this case when we divide 12345 by 10 , we get 5 as remainder.
so n % 10 part of code becomes 5.
Now, the second part is n//10 which gives you 1234 that are remaining digits.
Applying the same function again will give you 4 + sum_of_digit(123) and so on.
Even if this do not clear your confusion try, running this code on paper with some small number.

How to find the GCD/HCF of three numbers in Python3 [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I need a code to find the common prime factors of three numbers in Python 3
Whenever I run it shows me the wrong HCF / GCD.
Very simple.
Write a function, that calculates gcd/lcm of two numbers.
Then do something like this.
gcd(a,b,c) = gcd(a, gcd(b,c))
>>> def gcd(a,b):
... if b == 0:
... return a
... else:
... return gcd(b, a%b)
...
>>> gcd(3,5)
1
>>> gcd(10,5)
5
>>> gcd(10,15)
5
>>> gcd(5,gcd(10,15))
5
You can try by yourself, for lcm.

What is the most effiecient way to get Factorial Sum? [closed]

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.

How do I generate the Fibonacci sequence without using a list? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have been asked the following:
Using a while-loop, write a program that generates a Fibonacci
sequence of integers. Your program should ask the user how many
Fibonacci sequence entries to generate and print this quantity of them
to the screen.
I don't know where to begin. Can someone point me in the right direction?
use a variable to hold last value and current value, print the current value, and then update the last value... don't want to write it for you :)
Let's think about this problem a little bit before just giving out the answer:
The fibonacci sequence is of the form
0 1 1 2 3 5 8 13 21 ...
So as you can see your next number is the sum of the previous two numbers so, based on its definition you can tell you need two variables to store the previous two numbers in and a variable to store the sum in. You also need to know when you need to terminate your loop (the number you get from the user).
Looks like someone has already posted it for you...never mind.
Every fibonacci number is generated as the sum of the previous two fibonacci numbers. The first two fibonacci numbers are 0 and 1.
Using the above as the definition, let's start designing your code:
function fibonnacci:
n := ask user how many numbers to output # hint: use raw_input() and int()
if n is 1:
output 0
else if n is 2:
output 0, 1
else:
output 0, 1
lastNumber := 1
twoNumbersAgo := 0
count up from 3 to n:
nextNumber := twoNumbersAgo + lastNumber
output nextNumber
twoNumbersAgo := lastNumber
lastNumber = nextNumber
end function

Categories