Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 787091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:08:51+00:00 2026-05-14T21:08:51+00:00

What could be the simplest and time efficient logic to find out the factors

  • 0

What could be the simplest and time efficient logic to find out the factors of a given Number.
Is there any algorithm that exist, based on the same.

Actually, my real problem is to find out the no. of factors that exist for a given Number..

So Any algorithm, please let me know on this..

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T21:08:52+00:00Added an answer on May 14, 2026 at 9:08 pm

    Actually, my real problem is to find out the no. of factors that exist for a given Number..

    Well, this is different. Let n be the given number.

    If n = p1^e1 * p2^e2 * ... * pk^ek, where each p is a prime number, then the number of factors of n is (e1 + 1)*(e2 + 1)* ... *(ek + 1). More on this here.

    Therefore, it is enough to find the powers at which each prime factor appears. For example:

    read given number in n
    initial_n = n
    num_factors = 1;
    for (i = 2; i * i <= initial_n; ++i) // for each number i up until the square root of the given number
    {
        power = 0; // suppose the power i appears at is 0
        while (n % i == 0) // while we can divide n by i
        {
            n = n / i // divide it, thus ensuring we'll only check prime factors
            ++power // increase the power i appears at
        }
        num_factors = num_factors * (power + 1) // apply the formula
    }
    
    if (n > 1) // will happen for example for 14 = 2 * 7
    {
        num_factors = num_factors * 2 // n is prime, and its power can only be 1, so multiply the number of factors by 2
    }
    

    For example, take 18. 18 = 2^1 * 3*2 => number of factors = (1 + 1)*(2 + 1) = 6. Indeed, the 6 factors of 18 are 1, 2, 3, 6, 9, 18.

    Here’s a little benchmark between my method and the method described and posted by @Maciej. His has the advantage of being easier to implement, while mine has the advantage of being faster if change to only iterate over the prime numbers, as I have done for this test:

     class Program
    {
        static private List<int> primes = new List<int>();
    
        private static void Sieve()
        {
            bool[] ok = new bool[2000];
    
            for (int i = 2; i < 2000; ++i) // primes up to 2000 (only need up to sqrt of 1 000 000 actually)
            {
                if (!ok[i])
                {
                    primes.Add(i);
    
                    for (int j = i; j < 2000; j += i)
                        ok[j] = true;
                }
            }
        }
    
        private static int IVlad(int n)
        {
            int initial_n = n;
            int factors = 1;
    
            for (int i = 0; primes[i] * primes[i] <= n; ++i)
            {
                int power = 0;
                while (initial_n % primes[i] == 0)
                {
                    initial_n /= primes[i];
                    ++power;
                }
                factors *= power + 1;
            }
    
            if (initial_n > 1)
            {
                factors *= 2;
            }
    
            return factors;
        }
    
        private static int Maciej(int n)
        {
            int factors = 1;
            int i = 2;
            for (; i * i < n; ++i)
            {
                if (n % i == 0)
                {
                    ++factors;
                }
            }
    
            factors *= 2;
    
            if (i * i == n)
            {
                ++factors;
            }
    
            return factors;
        }
    
        static void Main()
        {
            Sieve();
    
    
            Console.WriteLine("Testing equivalence...");
    
            for (int i = 2; i < 1000000; ++i)
            {
                if (Maciej(i) != IVlad(i))
                {
                    Console.WriteLine("Failed!");
                    Environment.Exit(1);
                }
            }
    
            Console.WriteLine("Equivalence confirmed!");
    
            Console.WriteLine("Timing IVlad...");
            Stopwatch t = new Stopwatch();
    
            t.Start();
            for (int i = 2; i < 1000000; ++i)
            {
                IVlad(i);
            }
    
            Console.WriteLine("Total milliseconds: {0}", t.ElapsedMilliseconds);
            Console.WriteLine("Timing Maciej...");
    
            t.Reset();
            t.Start();
            for (int i = 2; i < 1000000; ++i)
            {
                Maciej(i);
            }
    
    
            Console.WriteLine("Total milliseconds: {0}", t.ElapsedMilliseconds);
        }
    }
    

    Results on my machine:

    Testing equivalence…
    Equivalence confirmed!
    Timing IVlad…
    Total milliseconds: 2448
    Timing Maciej…
    Total milliseconds: 3951
    Press any key to continue . . .

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What would be a simplest algorithm one or more people could use to decide
This is the simplest example I could come up with that reproduces the problem.
In Java there are a bunch of methods that all have to do with
I thought that I could use SimpleDB to take care of the most challenging
I was looking for the simplest way to convert a date an time from
I'm getting this error... ImportError: Could not find 'input_readers' on path 'map reduce' when
could L1/L2 cache line each cache multiple copies of the main memory data word?
Could someone who is familiar with webkit please explain or point me in the
Could you let me know how can I change font size of button caption
Could you share your frequently used tricks to search the web for programming related

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.