Checking Whether a Number Contains Numbers 1 to N as Factors

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



Leave a reply



Submit