Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

find all factors of a given number

public List<int> Factor(int number) 
{
    var factors = new List<int>();
    int max = (int)Math.Sqrt(number);  // Round down

    for (int factor = 1; factor <= max; ++factor) // Test from 1 to the square root, or the int below it, inclusive.
    {  
        if (number % factor == 0) 
        {
            factors.Add(factor);
            if (factor != number/factor) // Don't add the square root twice!  Thanks Jon
                factors.Add(number/factor);
        }
    }
    return factors;
}
Comment

Find Factors of a Number Using Function

def FindFact(n):
    for i in range(1, n+1):
        if n % i == 0:
            print(i, end=" ")
    print()

print("Enter a Number: ", end="")
try:
    num = int(input())
    print("
Factors of " +str(num)+ " are: ", end="")
    FindFact(num)
except ValueError:
    print("
Invalid Input!")
Comment

all factors, factors of a number, factors

from functools import reduce

def factors(n):    
    return set(reduce(list.__add__, 
                ([i, n//i] for i in range(1, int(n**0.5) + 1) if n % i == 0)))
Comment

PREVIOUS NEXT
Code Example
Python :: date and time in python 
Python :: How to Get the length of all items in a list of lists in Python 
Python :: pandas switch column levels 
Python :: palindrom python rekursiv 
Python :: database with python 
Python :: How to change the title of a console app in python 
Python :: python classes and objects 
Python :: alternative to time.sleep() in python 
Python :: django make app 
Python :: for in loop python 
Python :: python tutorial 
Python :: django annotate 
Python :: Python re.subn() 
Python :: python search a string in another string get last result 
Python :: get date only from datetimefiel django 
Python :: decimal to binary 
Python :: random list generator 
Python :: feature engineering data preprocessing 
Python :: program to replace lower-case characters with upper-case and vice versa in python 
Python :: How to delete a file or folder in Python? 
Python :: python list with several same values 
Python :: python index for all matches 
Python :: server in python 
Python :: run python module from command line 
Python :: add values from 2 columns to one pandas 
Python :: sample hyperparameter tuning with grid search cv 
Python :: python web scraping 
Python :: aws lambda logging with python logging library 
Python :: python list equality 
Python :: python list comprehension with filter 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =