Checking whether a number contains numbers 1 to n as factors
Your problem is you forgot to use the output of isMultiple in your recursive part
if (number % currentFactor == 0)
{
currentFactor--;
returnBool = isMultiple(number, currentFactor); //you need a to save the value here.
}
Without assigning returnBool
there is no way of knowing if the inner isMultiple returned true or not.
Python - Check if numbers in list are factors of a number
To check if there are any factors of the number guess
remaining you can use any()
:
hasfactors = any(guess % n == 0 for n in numbers)
To check if all the remaining numbers are prime, all()
can be used. (Since you say you already prevented the user from inputting prime numbers I assume you have some kind of isprime()
function):
onlyprimes = all(isprime(n) for n in numbers)
How many factors in an integer
The %
(modulus) operator gives you the remainder of a division. If that remainder is 0, then the second multiple is a factor of the second. So just loop through all the numbers from 1
to n
and check if they're factors; if so, add them to the list with append
:
def factors(n):
result = []
for i in range(1, n + 1):
if n % i == 0:
result.append(i)
return result
Here's a demo.
Or, more concisely using lambdas:
def factors(n):
return filter(lambda i: n % i == 0, range(1, n + 1))
Here's a demo.
I have to figure out if the numbers in an array are factors of a given number
If the array contains any 0s, you should return false.
Other than that, you should return false
when you encounter a number that is not a factor of n
.
Return true
only after the loop (i.e. after validating that all the array elements are factors of n
).
public static boolean areFactors (int[] arrNum, int n) {
if (arrNum == null || arrNum.length == 0) {
return false;
}
for (int i = 0; i < arrNum.length; i++) {
if (arrNum[i] == 0 || n % arrNum[i] != 0) {
return false;
}
}
return true;
}
How to factor a number and determine whether its a prime number
This determines if the number is prime or not the quickest way. Another method would be to use a for loop
to determine the number of factors for the number and then say it's prime if it has more than two factors.
int num; // is the number being tested for if it's prime.
boolean isPrime = true;
for (int i = 2; i <= Math.sqrt(num); i++) // only have to test until the square root of the number
{
if (num%i == 0) // if the number is divisible by anything from 2 - the square root of the number
{
isPrime = false; // it is not prime
break; // break out of the loop because it's not prime and no more testing needed
}
}
if (isPrime)
{
System.out.println(num + " is a prime number.");
}
else
{
System.out.println(num + " is a composite number.");
}
Complete n-Digit Factor
You're attacking the problem from the functional definition, rather than analyzing the numerical properties of that definition.
Concatenating an n
-digit number to itself is the same as multiplying by 10^n + 1. For instance, doing this with 3-digit numbers is equivalent to multiplying each by 10^3 + 1, or 1001.
A number divides all such integers iff that number divides the multiplier. Therefore, you can drop this massive iteration and check; simply factor 10^n + 1.
For instance, 1001 factors into 7 * 11 * 13; from there, you can generate the seven needed integers in the answer: 7, 11, 13, 77, 91, 143, 1001.
There are many available factoring programs; a simple search will find you that code.
Input a number and find its factors - wrong answer
ok so i am stupid and writing an answer to my own question....
import math
n= 35#int(input("Enter your number : "))
i=int(math.sqrt(n)+1)
while i !=0:
if n%i==0:
print(i)
print(n/i)
i=i-1
this is actually the most efficient way
Related Topics
How to Get the Current Project Directory from C# Code When Creating a Custom Msbuild Task
Validate Indian Phone Number With Optional +91 or 0 Preceeding 10 Digits
How to Show a Console Output/Window in a Forms Application
Reload Page After Jquery.Get to MVC Controller Action
What Is the Purpose of a Question Mark After a Type (For Example: Int Myvariable)
How Does Httpcontext.Current.User.Identity.Name Know Which Usernames Exist
How to Have a a Razor Action Link Open in a New Tab
How to Create a Dynamic Email Template That Can Be Modified Without Changing Code in C# .Net MVC
How to Check Byte Array Empty or Not
How to Generate a System (Pc/Laptop) Hardware Unique Id in C#
Restsharp Serializing Json Objects to Post Parameters
Convert a List of Objects from One Type to Another Using Lambda Expression
Localhost Port Is Changing in Visual Studio 2017
Add Spaces Before Capital Letters
Easiest Way to Check If an Arbitrary String Is a Valid Filename
Cannot Access a Closed Stream of a Memorystream, How to Reopen