Getting Factors of a Number - c#

I'm trying to refactor this algorithm to make it faster. What would be the first refactoring here for speed?
public int GetHowManyFactors(int numberToCheck)
{
// we know 1 is a factor and the numberToCheck
int factorCount = 2;
// start from 2 as we know 1 is a factor, and less than as numberToCheck is a factor
for (int i = 2; i < numberToCheck; i++)
{
if (numberToCheck % i == 0)
factorCount++;
}
return factorCount;
}

The first optimization you could make is that you only need to check up to the square root of the number. This is because factors come in pairs where one is less than the square root and the other is greater.
One exception to this is if n is an exact square then its square root is a factor of n but not part of a pair.
For example if your number is 30 the factors are in these pairs:
1 x 30
2 x 15
3 x 10
5 x 6
So you don't need to check any numbers higher than 5 because all the other factors can already be deduced to exist once you find the corresponding small factor in the pair.
Here is one way to do it in C#:
public int GetFactorCount(int numberToCheck)
{
int factorCount = 0;
int sqrt = (int)Math.Ceiling(Math.Sqrt(numberToCheck));
// Start from 1 as we want our method to also work when numberToCheck is 0 or 1.
for (int i = 1; i < sqrt; i++)
{
if (numberToCheck % i == 0)
{
factorCount += 2; // We found a pair of factors.
}
}
// Check if our number is an exact square.
if (sqrt * sqrt == numberToCheck)
{
factorCount++;
}
return factorCount;
}
There are other approaches you could use that are faster but you might find that this is already fast enough for your needs, especially if you only need it to work with 32-bit integers.

Reducing the bound of how high you have to go as you could knowingly stop at the square root of the number, though this does carry the caution of picking out squares that would have the odd number of factors, but it does help reduce how often the loop has to be executed.

Looks like there is a lengthy discussion about this exact topic here: Algorithm to calculate the number of divisors of a given number
Hope this helps

The first thing to notice is that it suffices to find all of the prime factors. Once you have these it's easy to find the number of total divisors: for each prime, add 1 to the number of times it appears and multiply these together. So for 12 = 2 * 2 * 3 you have (2 + 1) * (1 + 1) = 3 * 2 = 6 factors.
The next thing follows from the first: when you find a factor, divide it out so that the resulting number is smaller. When you combine this with the fact that you need only check to the square root of the current number this is a huge improvement. For example, consider N = 10714293844487412. Naively it would take N steps. Checking up to its square root takes sqrt(N) or about 100 million steps. But since the factors 2, 2, 3, and 953 are discovered early on you actually only need to check to one million -- a 100x improvement!
Another improvement: you don't need to check every number to see if it divides your number, just the primes. If it's more convenient you can use 2 and the odd numbers, or 2, 3, and the numbers 6n-1 and 6n+1 (a basic wheel sieve).
Here's another nice improvement. If you can quickly determine whether a number is prime, you can reduce the need for division even further. Suppose, after removing small factors, you have 120528291333090808192969. Even checking up to its square root will take a long time -- 300 billion steps. But a Miller-Rabin test (very fast -- maybe 10 to 20 nanoseconds) will show that this number is composite. How does this help? It means that if you check up to its cube root and find no factors, then there are exactly two primes left. If the number is a square, its factors are prime; if the number is not a square, the numbers are distinct primes. This means you can multiply your 'running total' by 3 or 4, respectively, to get the final answer -- even without knowing the factors! This can make more of a difference than you'd guess: the number of steps needed drops from 300 billion to just 50 million, a 6000-fold improvement!
The only trouble with the above is that Miller-Rabin can only prove that numbers are composite; if it's given a prime it can't prove that the number is prime. In that case you may wish to write a primality-proving function to spare yourself the effort of factoring to the square root of the number. (Alternately, you could just do a few more Miller-Rabin tests, if you would be satisfied with high confidence that your answer is correct rather than a proof that it is. If a number passes 15 tests then it's composite with probability less than 1 in a billion.)

You can limit the upper limit of your FOR loop to numberToCheck / 2
Start your loop counter at 2 (if your number is even) or 3 (for odd values). This should allow you to check every other number dropping your loop count by another 50%.
public int GetHowManyFactors(int numberToCheck)
{
// we know 1 is a factor and the numberToCheck
int factorCount = 2;
int i = 2 + ( numberToCheck % 2 ); //start at 2 (or 3 if numberToCheck is odd)
for( ; i < numberToCheck / 2; i+=2)
{
if (numberToCheck % i == 0)
factorCount++;
}
return factorCount;
}

Well if you are going to use this function a lot you can use modified algorithm of Eratosthenes http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes and store answars for a interval 1 to Max in array. It will run IntializeArray() once and after it will return answers in 0(1).
const int Max =1000000;
int arr [] = new int [Max+1];
public void InitializeArray()
{
for(int i=1;i<=Max;++i)
arr[i]=1;//1 is factor for everyone
for(int i=2;i<=Max;++i)
for(int j=i;i<=Max;i+=j)
++arr[j];
}
public int GetHowManyFactors(int numberToCheck)
{
return arr[numberToCheck];
}
But if you are not going to use this function a lot I think best solution is to check unitll square root.
Note: I have corrected my code!

An easy to implement algorithm that will bring you much farther than trial division is Pollard Rho
Here is a Java implementation, that should be easy to adapt to C#: http://www.cs.princeton.edu/introcs/78crypto/PollardRho.java.html

https://codility.com/demo/results/demoAAW2WH-MGF/
public int solution(int n) {
var counter = 0;
if (n == 1) return 1;
counter = 2; //1 and itself
int sqrtPoint = (Int32)(Math.Truncate(Math.Sqrt(n)));
for (int i = 2; i <= sqrtPoint; i++)
{
if (n % i == 0)
{
counter += 2; // We found a pair of factors.
}
}
// Check if our number is an exact square.
if (sqrtPoint * sqrtPoint == n)
{
counter -=1;
}
return counter;
}

Codility Python 100 %
Here is solution in python with little explanation-
def solution(N):
"""
Problem Statement can be found here-
https://app.codility.com/demo/results/trainingJNNRF6-VG4/
Codility 100%
Idea is count decedent factor in single travers. ie. if 24 is divisible by 4 then it is also divisible by 8
Traverse only up to square root of number ie. in case of 24, 4*4 < 24 but 5*5!<24 so loop through only i*i<N
"""
print(N)
count = 0
i = 1
while i * i <= N:
if N % i == 0:
print()
print("Divisible by " + str(i))
if i * i == N:
count += 1
print("Count increase by one " + str(count))
else:
count += 2
print("Also divisible by " + str(int(N / i)))
print("Count increase by two count " + str(count))
i += 1
return count
Example by run-
if __name__ == '__main__':
# result = solution(24)
# result = solution(35)
result = solution(1)
print("")
print("Solution " + str(result))
"""
Example1-
24
Divisible by 1
Also divisible by 24
Count increase by two count 2
Divisible by 2
Also divisible by 12
Count increase by two count 4
Divisible by 3
Also divisible by 8
Count increase by two count 6
Divisible by 4
Also divisible by 6
Count increase by two count 8
Solution 8
Example2-
35
Divisible by 1
Also divisible by 35
Count increase by two count 2
Divisible by 5
Also divisible by 7
Count increase by two count 4
Solution 4
Example3-
1
Divisible by 1
Count increase by one 1
Solution 1
"""
Github link

I got pretty good results with complexity of O(sqrt(N)).
if (N == 1) return 1;
int divisors = 0;
int max = N;
for (int div = 1; div < max; div++) {
if (N % div == 0) {
divisors++;
if (div != N/div) {
divisors++;
}
}
if (N/div < max) {
max = N/div;
}
}
return divisors;

Python Implementation
Score 100% https://app.codility.com/demo/results/trainingJ78AK2-DZ5/
import math;
def solution(N):
# write your code in Python 3.6
NumberFactor=2; #one and the number itself
if(N==1):
return 1;
if(N==2):
return 2;
squareN=int(math.sqrt(N)) +1;
#print(squareN)
for elem in range (2,squareN):
if(N%elem==0):
NumberFactor+=2;
if( (squareN-1) * (squareN-1) ==N):
NumberFactor-=1;
return NumberFactor

Related

How do I find prime numbers in a dynamic array

There are other questions on this topic but none of them really answer my question. I designed a piece of code to find the prime numbers in a dynamically sized array. Here is the code:
int userInput = int.Parse(Console.ReadLine());
int[] grades = new int[userInput];
for( int i = 2; i < grades.Length; i++ )
{
grades[i] = i;
int[] prevNums = new int[i];
List<int> primes = new List<int>();
for (int k = 1; k < grades[i]; k++)
{
prevNums[k] = k;
int result = grades[i] / k;
bool failed = false;
foreach (int n in prevNums) // go over every number in the list
{
if (n == result) // check if it matches
{
failed = true;
}
}
if (failed == false && prevNums[k] == grades[i] - 1)
{
Console.WriteLine(grades[i]);
primes.Add(grades[i]);
}
}
}
Instead of printing every prime in the array it instead always returns 2. Any suggestions would be super helpful. I do understand there is a very simple method to find primes but this is more of a test/educational thing than a piece of code to do a job.
Instead of printing every prime in the array it instead always returns 2
The reason for this is here
if (failed == false && prevNums[k] == grades[i] - 1)
We can see that the only time you consider a number to be prime is if that number contains no number before it which is equal to i/k and if k == (i - 1). One of the very few times this is true is for the number 2.
In order to talk about how to fix this we need to define what a prime number is and how to find it, and then compare that with your code. Doing that we can identify where we went wrong and how to fix it.
A number is considered prime if it's only divisible by 1 and itself. Knowing this we can create some simple instructions and create an algorithm from those instructions.
How can we check to see if a number is divisible by another number? In C# and most modern languages we can use the % modulo operator. This operator provides us with the remainder when two numbers are divided. So for 3/2 the remainder would be 1. But when we do say 2/1 we get 0 since 2 can be divided by 1 with no numbers left over.
In your code you identified if a number is divisible by using
grades[i] / k;
This doesn't give us a remainder but we can easily change it to do so by replacing the division operator with the modulo operator(%).
Now that we can determine if a number is divisible by another number we need to find out if a number is prime.
To do that we look at all the numbers before the number we think is prime. When we look at the numbers we're checking to see if any number before the current number can be equally divided into the current number. If the remainder of dividing the two numbers is not zero, then the two numbers are not divisible. If the remainder is zero then they are equally divisible and the current number isnt prime.
Let's compare that with what you have, your code says "for each number between 2 and the input number(currentNumber), check if any number between 1 and the current number(previousNumber) are equal to the current number divided by the previous number. And if that is true then the current number is not prime".
I went ahead and created a working example of finding primes for what I think was the way you were intending. There are better ways such as the Sieve of Eratosthenes, but I kept the code as similar to yours as possible.
// get the number from the user
int userInput = int.Parse(Console.ReadLine());
// create a place to put the primes we find
List<int> primes = new List<int>();
// count from 2(the smallest positive prime) and count torwards the user number
for (int i = 2; i < userInput; i++)
{
// create a way to tell if the current number was divisible by some other number preceding it
bool failed = false;
// walk backwards from the current number and check each one
for (int k = i; k-- > 2;)
{
// check to see if the current number / previous number has a remainder
if (i % k == 0)
{
// since there was no remainder give up as the current number cant be prime
failed = true;
break;
}
}
// if we got here and haven't failed
// it means the number is prime and we should add it to the list
if (failed == false)
{
Console.WriteLine(i);
primes.Add(i);
}
}

How does prime factorization guarantee that the factors will be prime?

In this particular code how does it not output 6 1, because 6 gives no remainder dividing 18 by it.
int n = 18;
int[] fact = new int[100];
int[] pow = new int[100];
int d = 0;
for (int i = 2; i <= n; i++)
{
int s = 0;
while(n % i == 0)
{
s++;
n /= i;
}
if (s > 0)
{
fact[d] = i;
pow[d] = s;
d++;
}
}
Every numbers is either a prime, or a multiple of a prime. Look at: https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
If it is not divisible by a prime, it can not be divisible by any multiple of that prime either. As 18 can be divided by 2 - 4, 6, 8, 10 and everything else never needs to be tested. As 19 can not be divided by 2, 4, 6, 8 and 10 never need to be tested. That 20 is divisible by 10, never needs to be tested. It already divides by 2 so we excluded half the possible numbers back then.
We do that exclusion of divisors instinctively, but usually only for 2 - testing only odd numbers - but you can drive that way further. As the result of a division must also be a prime or multiple of a prime, we even can stop once we passed the Squareroot.
All you really need is a unbroken list of primes, going to up to whatever Prime is > Squarroot(PrimeCandidate). Short of reading it from memory, that is the quickest way I know off.
I actually wrote something on the topic a while back, about the 5-ish ways of checking for a prime: https://social.msdn.microsoft.com/Forums/en-US/85fc2406-d2e9-495a-bea7-e516661f8b40/primal-issues-multithreading-lists-in-memory-and-checking-for-prime-number?forum=csharpgeneral

Finding sum of consecutive primes

I am working on improving my C# skills, and in the process I am trying to solve some of the problems on Project Euler, in this case problem 50. The problem states:
The prime 41, can be written as the sum of six consecutive primes:
41 = 2 + 3 + 5 + 7 + 11 + 13
This is the longest sum of consecutive primes that adds to a prime below one-
hundred.
The longest sum of consecutive primes below one-thousand that adds to
a prime, contains 21 terms, and is equal to 953.
Which prime, below one-million, can be written as the sum of the most
consecutive primes?
Seems simple enough. I wrote a method to tell if something is prime, made a list of the primes below 1 million (which is easily more than I need, but I don't know how many I actually need), and iterated through that list to find the sums of the primes. Here is my code:
public static void Main()
{
IEnumerable<int> primes = Enumerable.Range(0, 1000000)
.Where(i => isPrime(i));
int sum = 0;
List<int> history = new List<int>();
foreach (int bar in primes)
{
if (sum + bar < 1000000)
{
sum += bar;
Console.WriteLine(sum);
history.Add(bar);
}
}
while (!isPrime(sum))
{
sum -= history[history.Count - 1];
history.Remove(history[history.Count - 1]);
}
Console.WriteLine(sum);
Console.ReadLine();
}
public static bool isPrime(int num)
{
if (num <= 1)
{
return false;
}
else if (num == 2)
{
return true;
}
else if (num % 2 == 0)
{
return false;
}
else
{
var boundary = (int)Math.Floor(Math.Sqrt(num));
for (int i = boundary; i > 1; i--)
{
if (num % i == 0)
{
return false;
}
}
return true;
}
}
If I am correct, this should find the sum of my primes up to a million, then subtract primes until the sum is a prime number itself. When I run this, the code sums up to 997661, but that is not prime. I subtract the recently added primes until I get a result of 958577, which is prime, but this is not the correct answer. I am fairly certain my method to find primes is correct, but I cannot figure out what is causing my answer to be wrong. What's worse, I don't know the correct answer, so I can't work backwards to see what is causing the issue.
I suspect something may be broken inside of my while loop, like maybe I am removing the wrong values from the list. If anyone can offer some insight into why my program is not working, I would very much appreciate it.
Find the longest list of primes with a sum less than 1000000. That's the list that starts at 2 and goes as high as possible. Let the length of this list be L.
Now, iterate through all lists with sums less than 1000000, starting with the list of length L, then all lists of length L-1, then L-2, etc.
Stop when you get a prime sum.
About 1 in every 15 integers near 1000000 is prime, so you wont have to check very many lists, and of course you should make subsequent lists by adding and removing primes from the ends instead of recalculating the whole sum.
well i am improving my python skills and solved the question using python.I think the question might be wrong and i did the calculation manually.well here is my answer
The prime 41, can be written as the sum of six consecutive primes: 41 = 2 + 3 + 5 + 7 + 11 + 13
This is the longest sum of consecutive primes that adds to a prime below one-hundred. The longest sum of
The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal to 953.
Which prime, below one-million, can be written as the sum of the most consecutive primes?
solution:-
i tried to solve it in step by step process and here is my justification towards my assumption.
import sympy
sum=0
lst1=[]
for num in range(1,100):
#isprime(n):return True when the num is prime and false when the num is composite
if sympy.isprime(num) is True:
sum+=num
lst1.append(sum)
print("The sum list 1 is: ",lst1)
lst2=[]
for sum in lst1:
if sum<100:
if sympy.isprime(sum)==True:
lst2.append(sum)
print("The list 2 is: ",lst2)
print("The required answer is :",max(lst2))
the longest sum of consecutive primes that adds to a prime below one-hundred is 41
enter image description here
so similarly i changed the limits from 100 to 1000 as below..
import sympy
sum=0
lst1=[]
for num in range(1,1000):
#isprime(n):return True when the num is prime and false when the num is composite
if sympy.isprime(num) is True:
sum+=num
lst1.append(sum)
print("The sum list 1 is: ",lst1)
lst2=[]
for sum in lst1:
if sum<1000:
if sympy.isprime(sum)==True:
lst2.append(sum)
print("The list 2 is: ",lst2)
print("The required answer is :",max(lst2))
enter image description here
here the answer is different which gave me 281 is the highest whereas in the question it was mentioned 953 to be actual answer.
just by changing your upper limits from 1000 to 1000000 we get answer as 958577
enter image description here
EXPLANATION:when you add up manually u will get 963 instead of 953 which makes 963 a composite number.Therefore,i think there is a mistake in this question and maybe the developer gave a wrong answer or so.
class Example {
public static void main(String[] args) {
int count = 0;
int sum = 0;
for (int j = 2; j < 1000; j++) {
count = 0;
for (int i = 1; i <= j; i++) {
if (j % i == 0) {
count++;
}
}
if (count == 2) {
sum += j;
if (sum >= 1000) {
sum -= j;
break;
}
}
}
System.out.println("longest sum of consecutive primes that adds to a prime below 1000: " + sum);
}
}

Fast algorithm for pandigital check

I'm working on a project for which I need a very fast algorithm for checking whether a supplied number is pandigital. Though the logic seems sound, I'm not particularly happy with performance of the methods described below.
I can check up to one million 9-digit numbers in about 520ms, 600ms and 1600ms respectively. I'm working on a low-latency application and in production I'll have a dataset of about 9 or 9.5 billion 7- to 9-digit numbers that I'll need to check.
I have three candidiates right now (well, really two) that use the following logic:
Method 1: I take an input N, split into into a byte array of its constituent digits, sort it using an Array.Sort function and iterate over the array using a for loop checking for element vs counter consistency:
byte[] Digits = SplitDigits(N);
int len = NumberLength(N);
Array.Sort(Digits);
for (int i = 0; i <= len - 1; i++)
{
if (i + 1 != Digits[i])
return false;
}
Method 2: This method is based on a bit of dubious logic, but I split the input N into a byte array of constituent digits and then make the following test:
if (N * (N + 1) * 0.5 == DigitSum(N) && Factorial(len) == DigitProduct(N))
return true;
Method 3: I dislike this method, so not a real candidate but I cast the int to a string and then use String.Contains to determine if the required string is pandigital.
The second and third method have fairly stable runtimes, though the first method bounces around a lot - it can go as high as 620ms at times.
So ideally I really like to reduce the runtime for the million 9-digit mark to under 10ms. Any thoughts?
I'm running this on a Pentium 6100 laptop at 2GHz.
PS - is the mathematical logic of the second method sound?
Method 1
Pre-compute a sorted list of the 362880 9-digit pandigital numbers. This will take only a few milliseconds. Then for each request, first check if the number is divisible by 9: It must be to be pandigital. If it is, then use a binary search to check if it is in your pre-computed list.
Method 2
Again, check if the number is divisible by 9. Then use a bit vector to track the presence of digits. Also use modular multiplication to replace the division by a multiplication.
static bool IsPandigital(int n)
{
if (n != 9 * (int)((0x1c71c71dL * n) >> 32))
return false;
int flags = 0;
while (n > 0) {
int q = (int)((0x1999999aL * n) >> 32);
flags |= 1 << (n - q * 10);
n = q;
}
return flags == 0x3fe;
}
Method 1 comes in at 15ms/1M. Method 2 comes in at 5.5ms/1M on my machine. This is C# compiled to x64 on an i7 950.
just a thought: (after the definitition of pandigital from wikipedia)
int n = 1234567890;
int Flags = 0;
int Base = 10;
while(n != 0)
{
Flags |= 1<<(n % Base); n /= Base;
}
bool bPanDigital = Flags == ((1 << Base) - 1);

Append a digit to an integer and make sure sum of each digits ends with 1

What is the algorithm in c# to do this?
Example 1:
Given n = 972, function will then append 3 to make 9723, because 9 + 7 + 2 + 3 = 21 (ends with 1). Function should return 3.
Example 2:
Given n = 33, function will then append 5 to make 335, because 3 + 3 + 5 = 11 (ends with 1). Function should return 5.
Algorithms are language independent. Asking for "an algorithm in C#" doesn't make much sense.
Asking for the algorithm (as though there is only one) is similarly misguided.
So, let's do this step by step.
First, we note that only the last digit of the result is meaningful. So, we'll sum up our existing digits, and then ignore all but the last one. A good way to do this is to take the sum modulo 10.
So, we have the sum of the existing digits, and we want to add another digit to that, so that the sum of the two ends in 1.
For the vast majority of cases, that will mean sum + newDigit = 11. Rearranging gives newDigit = 11 - sum
We can then take this modulo 10 (again) in order to reduce it to a single digit.
Finally, we multiply the original number by 10, and add our new digit to it.
The algorithm in general:
(10 - (sum of digits mod 10) + 1) mod 10
The answer of the above expression is your needed digit.
sum of digits mod 10 gives you the current remainder, when you subtract this from 10 you get the needed value for a remainder of 0. When you add 1 you get the needed value to get a remainder of 1. The last mod 10 gives you the answer as a 1 digit number.
So in C# something like this:
static int getNewValue(string s)
{
int sum = 0;
foreach (char c in s)
{
sum += Convert.ToInt32(c.ToString());
}
int newDigit = (10 - (sum % 10) + 1) % 10;
return newDigit;
}
Another alternative using mod once only
int sum = 0;
foreach (char c in s)
sum += Convert.ToInt32(c.ToString());
int diff = 0;
while (sum % 10 != 1)
{
sum++;
diff++;
}
if (diff > 0)
s += diff.ToString();
Well, it's easier in C++.
std::string s = boost::lexical_cast<string>( i );
i = i * 10 + 9 - std::accumulate( s.begin(), s.end(), 8 - '0' * s.size() ) % 10;
Addicted to code golf…

Categories