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

  • Home
  • SEARCH
  • 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 64015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:43:25+00:00 2026-05-10T18:43:25+00:00

All numbers that divide evenly into x. I put in 4 it returns: 4,

  • 0

All numbers that divide evenly into x.

I put in 4 it returns: 4, 2, 1

edit: I know it sounds homeworky. I’m writing a little app to populate some product tables with semi random test data. Two of the properties are ItemMaximum and Item Multiplier. I need to make sure that the multiplier does not create an illogical situation where buying 1 more item would put the order over the maximum allowed. Thus the factors will give a list of valid values for my test data.

edit++: This is what I went with after all the help from everyone. Thanks again!

edit#: I wrote 3 different versions to see which I liked better and tested them against factoring small numbers and very large numbers. I’ll paste the results.

static IEnumerable<int> GetFactors2(int n) {     return from a in Enumerable.Range(1, n)                   where n % a == 0                   select a;                       }  private IEnumerable<int> GetFactors3(int x) {                 for (int factor = 1; factor * factor <= x; factor++)     {         if (x % factor == 0)         {             yield return factor;             if (factor * factor != x)                 yield return x / factor;         }     } }  private IEnumerable<int> GetFactors1(int x) {     int max = (int)Math.Ceiling(Math.Sqrt(x));     for (int factor = 1; factor < max; factor++)     {         if(x % factor == 0)         {             yield return factor;             if(factor != max)                 yield return x / factor;         }     } } 

In ticks. When factoring the number 20, 5 times each:

  • GetFactors1-5,445,881
  • GetFactors2-4,308,234
  • GetFactors3-2,913,659

When factoring the number 20000, 5 times each:

  • GetFactors1-5,644,457
  • GetFactors2-12,117,938
  • GetFactors3-3,108,182
  • 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. 2026-05-10T18:43:25+00:00Added an answer on May 10, 2026 at 6:43 pm

    pseudocode:

    • Loop from 1 to the square root of the number, call the index "i".
    • if number mod i is 0, add i and number / i to the list of factors.

    realocode:

    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; } 

    As Jon Skeet mentioned, you could implement this as an IEnumerable<int> as well – use yield instead of adding to a list. The advantage with List<int> is that it could be sorted before return if required. Then again, you could get a sorted enumerator with a hybrid approach, yielding the first factor and storing the second one in each iteration of the loop, then yielding each value that was stored in reverse order.

    You will also want to do something to handle the case where a negative number passed into the function.

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

Sidebar

Ask A Question

Stats

  • Questions 79k
  • Answers 79k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think iPhone's is, by far, the most fun and… May 11, 2026 at 4:09 pm
  • Editorial Team
    Editorial Team added an answer Either myListView.Items[ myListView.SelectedIndices[ 0 ] ]; or myListView.SelectedItems[ 0 ];… May 11, 2026 at 4:09 pm
  • Editorial Team
    Editorial Team added an answer First, I strongly recommend that you not use triggers. If… May 11, 2026 at 4:09 pm

Related Questions

I've been considering fast poker hand evaluation in Python. It occurred to me that
I read an interesting DailyWTF post today, Out of All The Possible Answers... and
I have an array of numbers that potentially have up to 8 decimal places
I want to invert a 4x4 matrix. My numbers are stored in fixed-point format

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.