Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

find the factorial of a given integer in python

import math
num = 5
print("The factorial of 5 is : ", end="")
print(math.factorial(num))

# output - The factorial of 5 is : 120

# The factorial of a number is the function that multiplies the number by every natural number below it
# e.g. - 1 * 2 * 3 * 4 * 5 = 120
Comment

Program for factorial of a number in python

# Python 3 program to find
# factorial of given number
 
# Function to find factorial of given number
def factorial(n):
      
    if n == 0:
        return 1
     
    return n * factorial(n-1)
  
# Driver Code
num = 5;
print("Factorial of", num, "is",
factorial(num))
  
# This code is contributed by Smitha Dinesh Semwal
Comment

find factorial in python

factorial = lambda n: n * factorial(n-1) if n > 1 else 1

print(factorial(3)) # 6 
print(factorial(5)) # 120
Comment

Finding factorial of a number in Python

# ___PYTHON___
# Finding factorial of a number:
userInput = int(input("Enter an integer: "))

def factorial(n):

  if n == 0 or n == 1:
    return 1

  else :
    return n * factorial(n - 1)

x = factorial(userInput)
print(x)
Comment

Python function to compute factorial of a number.

def factorial(n):
    if n == 0:
        return 1
    else:
        return n * factorial(n-1)
n=int(input("Input a number to compute the factiorial : "))
print(factorial(n))
Comment

factorial of a number in python

num = int(input("Enter a number to find factorial: "))
mul = 1
for x in range(1, num+1):
    mul *= x

print("Factorial of", num, "=", mul)
Comment

factorial program in python

def Fact(num):
    z=1
    while(1):
        z=z*num
        num=num-1
        if(num==0):
            break
    
    print(z)
Fact(4)
    
Comment

Python 3 program to find factorial of given number

# Python 3 program to find
# factorial of given number
def factorial(n):
     
    # single line to find factorial
    return 1 if (n==1 or n==0) else n * factorial(n - 1)
 
# Driver Code
num = 5;
print("Factorial of",num,"is",
factorial(num))
 
# This code is contributed by Smitha Dinesh Semwal
Comment

Python 3 program to find factorial of given number

# Python 3 program to find
# factorial of given number
def factorial(n):
     
    # single line to find factorial
    return 1 if (n==1 or n==0) else n * factorial(n - 1)
 
# Driver Code
num = 5;
print("Factorial of",num,"is",
factorial(num))
 
# This code is contributed by Smitha Dinesh Semwal
Comment

Python 3 program to find factorial of given number

# Python 3 program to find
# factorial of given number
def factorial(n):
     
    # single line to find factorial
    return 1 if (n==1 or n==0) else n * factorial(n - 1)
 
# Driver Code
num = 5;
print("Factorial of",num,"is",
factorial(num))
 
# This code is contributed by Smitha Dinesh Semwal
Comment

program python factorial

#easy way to find factorial of number with while
b=1
a=int(input('the number to be entered'))
c=1
while c<=a:
    b*=c
    c+=1
print('factorial',a,'is',b)

#output:
the number to be entered x
factorial x is x!
+-+-+-+-+-+-+-+-+++-+-+-+-+-+-+++-+-+++-+++-+-++-+-A
Comment

find factorial of a number in python

# Python program to find the factorial of a number provided by the user.

# change the value for a different result
num = 7

# To take input from the user
#num = int(input("Enter a number: "))

factorial = 1

# check if the number is negative, positive or zero
if num < 0:
   print("Sorry, factorial does not exist for negative numbers")
elif num == 0:
   print("The factorial of 0 is 1")
else:
   for i in range(1,num + 1):
       factorial = factorial*i
   print("The factorial of",num,"is",factorial)
Comment

Python 3 program to find factorial of given number

# Python 3 program to find
# factorial of given number
def factorial(n):
     
    # single line to find factorial
    return 1 if (n==1 or n==0) else n * factorial(n - 1)
 
# Driver Code
num = 5;
print("Factorial of",num,"is",
factorial(num))
 
# This code is contributed by Smitha Dinesh Semwal
Comment

python math.factorial algorithm

# A pure Python version.

# Returns the number of bits necessary to represent an integer in binary, 
# excluding the sign and leading zeros.
# Needed only for Python version < 3.0; otherwise use n.bit_length().
def bit_length(self):
    s = bin(self)       # binary representation:  bin(-37) --> '-0b100101'
    s = s.lstrip('-0b') # remove leading zeros and minus sign
    return len(s)       # len('100101') --> 6

def num_of_set_bits(i) :
    # assert 0 <= i < 0x100000000
    i = i - ((i >> 1) & 0x55555555)
    i = (i & 0x33333333) + ((i >> 2) & 0x33333333)
    return (((i + (i >> 4) & 0xF0F0F0F) * 0x1010101) & 0xffffffff) >> 24

def rec_product(start, stop):
   """Product of integers in range(start, stop, 2), computed recursively.
      start and stop should both be odd, with start <= stop.
   """
   numfactors = (stop - start) >> 1
   if numfactors == 2 : return start * (start + 2)
   if numfactors > 1 :
       mid = (start + numfactors) | 1
       return rec_product(start, mid) * rec_product(mid, stop)
   if numfactors == 1 : return start
   return 1
 
def binsplit_factorial(n):
   """Factorial of nonnegative integer n, via binary split.
   """
   inner = outer = 1
   for i in range(n.bit_length(), -1, -1):
       inner *= rec_product((n >> i + 1) + 1 | 1, (n >> i) + 1 | 1)
       outer *= inner
   return outer << (n - num_of_set_bits(n))
 
# Test (from math import factorial).
[[n, binsplit_factorial(n) - factorial(n)] for n in range(99)]
Comment

python math.factorial algorithm

bigint Factorial(int n)
  {
      bigint p = 1, r = 1;
      loop(n, p, r);
      return r << nminussumofbits(n);
  }

  loop(int n, reference bigint p, reference bigint r)
  {
      if (n <= 2) return;
      loop(n / 2, p, r);
      p = p * partProduct(n / 2 + 1 + ((n / 2) & 1), n - 1 + (n & 1));
      r = r * p;
  }

  bigint partProduct(int n, int m)
  {
      if (m <= (n + 1)) return (bigint) n;
      if (m == (n + 2)) return (bigint) n * m; 
      int k =  (n + m) / 2;
      if ((k & 1) != 1) k = k - 1;
      return partProduct(n, k) * partProduct(k + 2, m);
  }

  int nminussumofbits(int v)
  {
      long w = (long)v;
      w -= (0xaaaaaaaa & w) >> 1;
      w = (w & 0x33333333) + ((w >> 2) & 0x33333333);
      w = w + (w >> 4) & 0x0f0f0f0f;
      w += w >> 8;
      w += w >> 16;
      return v - (int)(w & 0xff);
  }
Comment

PREVIOUS NEXT
Code Example
Python :: sort dictionary 
Python :: adjust size of plot 
Python :: python extract value from a list of dictionaries 
Python :: django staff_member_required decorator 
Python :: icon tkiner 
Python :: how to print hello world 
Python :: unlimited keyword arguments python 
Python :: python list of all characters 
Python :: missingno python 
Python :: load saved model tensorflow 
Python :: beautifulsoup find_all by id 
Python :: django jalali date 
Python :: dropna for specific column pandas 
Python :: pandas series to numpy array 
Python :: shutil move overwrite 
Python :: how to compare two text files in python 
Python :: discord.py send messages 
Python :: How to install XGBoost package in python on Windows 
Python :: length of a matrix in python 
Python :: python remove articles from string regex 
Python :: drop row pandas 
Python :: all the positions of a letter occurrences in a string python 
Python :: if list item is found in string get that item python 
Python :: comparing two dataframe columns 
Python :: detect keypress in python 
Python :: pandas find location of values greater than 
Python :: how to commenbt code in python 
Python :: How many columns have null values present in them? in pandas 
Python :: python string replace index 
Python :: channel lock command in discord.py 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =