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.
Related
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.
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 1 year ago.
Improve this question
i need to convert this over to a recursive function. the program basically prints out all variations of a string.
def comb(L):
for i in range(3):
for j in range(3):
for k in range(3):
# check if the indexes are not
# same
if (i!=j and j!=k and i!=k):
print(L[i], L[j], L[k])
# Driver Code
comb([1, 2, 3])
output:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
This might get you started:
Every permutation of a list can be made by picking a value from the list & putting it at the front of every permutation of what is left in the list after taking that value out. (Note that the "what is left" part is a smaller list than what you started with.)
When your list is small enough, it is the only permutation.
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 1 year ago.
Improve this question
I am having trouble with my code the main issue is I want to return 1 by using the getEquivalentNumber function but the problem is that when I run the code it gives me the result of -63 is there any way for me to improve the code to get a better result.
print('*' * 50)
user_input = input('Please enter a word. \n'
'-->')
print('*' * 50)
def getEquivalentNumber(_Mychar):
_Mychar = _Mychar.lower()
equivalentNumber = ord(_Mychar) - 96
return equivalentNumber
def computeSumOfCharacters(myWord):
sum = 0
for i in myWord: # apple
sum += getEquivalentNumber(i)
return sum
print(computeSumOfCharacters(user_input))
You define functions with def.
You can get the value of the units digit of a number with number % 10.
You can remove the units digit with number = number // 10.
You can accumulate those digits by starting an accumulator at zero and adding each digit.
You can loop until the number becomes zero.
You can return a value from the function with return.
Apologies if some of that seems too basic but I'm not sure what skill level you're at. That's pretty much the process I'd follow, without giving you the actual code.
The only thing that concerns me is computeSumOfDigits(911) must return a SINGLE digit yet the text after that says 9 + 1 + 1 = 11.
If you are required to further process results that are not a single digit, you can check that before returning and call the same function on the result, something like (pseudo-code):
if accumulator > 9:
return computeSumOfDigits(accumulator)
That would calculate: 9 + 1 + 1 -> 11, 1 + 1 -> 2.
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
>>>
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 8 years ago.
Improve this question
I am trying to randomly generate an array of integers of range between -10^7 to 10^7 such that there exists a sub-array with a sum of zero.
For example : 7 6 4 8 -4 -8
1<=Size of array<=10^5
I am able to do it using brute-force but it is taking a lot of time as I have to generate very big such arrays. Is there any efficient way of achieving this using python or c++?
Edit: Actually I want to generate a lot of such arrays with different scenarios. I am generating these arrays as testcases for a problem to determine whether any given array of positive and negative integers contain a sub-array with zero sum.
Tried brute force code:
import random
N = random.randint(10,100)
mylist = []
for i in xrange(1,N):
mylist.append(random.randint(-100,100))
list2 = [1,-1]
mylist = mylist[1:N-2] + list2 + mylist[N-2:N]
print mylist
So I have to manually tweak it a lot.
Thanks!
The answer depends on how random you want your array to be. Like some of the commenters mentioned, you can always include a zero and are thus guaranteed to have a subarray with sum of zero. I thought it would be helpful to your eventual solution, so I want to mention that you can check if an array has a subarray with sum of zero in O(N^2) time.
def array_has_zerosubarray( A ):
for _begin in xrange(0,len(A)-1):
for _end in xrange(_begin+1,len(A)):
sum = 0
for ai in range(_begin,_end):
sum = sum + A[ai]
if sum==0:
return True
return False