Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

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
Csharp :: Get unique id of Device 
Csharp :: c# read xml tag value 
Csharp :: check if two date ranges overlap c# 
Csharp :: c# get random between 0 and 1 
Csharp :: C# How to make a field read-only outside of class 
Csharp :: linq map array 
Csharp :: c# jagged array initialization 
Csharp :: ASP.net ApplicationUser referance not found 
Csharp :: c# Modulo 10^9+7 (1000000007) 
Csharp :: c# minimise form 
Csharp :: upload a file selenium c# 
Csharp :: null-conditional operators c# 
Csharp :: cdn providers 
Csharp :: ontriggerenter2d 
Csharp :: binding on button c# 
Csharp :: Comparing Arrays using LINQ in C# 
Csharp :: Formcollection asp.net form 
Csharp :: if input.get touch 
Csharp :: unity create a textbox in inspector 
Csharp :: c# draggable controls 
Csharp :: get file upload file size in MB c# 
Csharp :: rgb to console color 
Csharp :: DataGridView ComboBox column selection changed event 
Csharp :: c# xml check if attribute exists 
Csharp :: Severity Code Description Project File Line Suppression State Error MSB3021 
Csharp :: instantiate date time variable C# 
Csharp :: c# return two values 
Csharp :: oauth API with the Access Token to retrieve some of users information. 
Csharp :: c# sc create service 
Csharp :: user input to array object c# 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =