daily pastebin goal
81%
SHARE
TWEET

Untitled

Kanan_mahammadli Mar 25th, 2019 90 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Program to find the number of ways, n can be
  2.  
  3. # written as sum of two or more positive integers.
  4.  
  5.   
  6.  
  7. # Returns number of ways to write n as sum of
  8.  
  9. # two or more positive integers
  10.  
  11. def CountWays(n):
  12.  
  13.   
  14.  
  15.     # table[i] will be storing the number of
  16.  
  17.     # solutions for value i. We need n+1 rows
  18.  
  19.     # as the table is consturcted in bottom up
  20.  
  21.     # manner using the base case (n = 0)
  22.  
  23.     # Initialize all table values as 0
  24.  
  25.     table =[0] * (n + 1)
  26.  
  27.   
  28.  
  29.     # Base case (If given value is 0)
  30.  
  31.     # Only 1 way to get 0 (select no integer)
  32.  
  33.     table[0] = 1
  34.  
  35.   
  36.  
  37.     # Pick all integer one by one and update the
  38.  
  39.     # table[] values after the index greater
  40.  
  41.     # than or equal to n
  42.  
  43.     for i in range(1, n ):
  44.  
  45.   
  46.  
  47.         for j in range(i , n + 1):
  48.  
  49.   
  50.  
  51.             table[j] +=  table[j - i]            
  52.  
  53.   
  54.  
  55.     return table[n]
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top