473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

"factors of" method

Does .NET provide a "factors of" method ?

I need to determine the factors of an integer.
Aug 19 '08 #1
15 3390
<"John A Grandy" <johnagrandy-at-gmail-dot-com>wrote:
Does .NET provide a "factors of" method ?
No.
I need to determine the factors of an integer.
You'll need to use the standard algorithms to find them, I'm afraid.
How big might these integers get?

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Aug 19 '08 #2
Try something like this

public List<intFactorsOf(int value)
{
List<intresult = new List<int>();
List<intprimeNumbers = new List<int{ 2, 3, 5, 7, 11, etc };
while (value 1)
{
foreach (int prime in primeNumbers)
if (value % prime == 0)
{
result.Add(prime);
value = value / prime;
}
}

return result;
}
Might not work or even compile :-)

Pete


Aug 19 '08 #3
Peter Morris wrote:
Try something like this

public List<intFactorsOf(int value)
{
List<intresult = new List<int>();
List<intprimeNumbers = new List<int{ 2, 3, 5, 7, 11, etc };
while (value 1)
{
foreach (int prime in primeNumbers)
if (value % prime == 0)
{
result.Add(prime);
value = value / prime;
}
}

return result;
}
Might not work or even compile :-)
It'll work (in the sense that it produces unique prime factors, if you've
typed out the list correctly), but it's preposterously inefficient for most
numbers. Try value == 4 to see what I mean. It's easy enough to fix, though;
you need to replace one keyword with another. The resulting list will be
slightly different.

If you're going for the brute force approach, though, there's no particular
reason to use a list of primes. Just divide by every number from 2 upwards;
only the primes will actually come out as factors.

--
J.
Aug 19 '08 #4
Hmmm ... perhaps I mis-used the word "factor" ....

I'm not just interested in prime factors , but all factors.

Brute force is easy, but perhaps a better algorithm exists.
public static List<intFindFactors( int number, int maxFactors )
{
List<intfactors = new List<int>( maxFactors );

factors.Add( 1 );

for ( int i = 2; i < number / 2 + 1; i++ )
{
if ( ( number % i ) == 0 )
{
factors.Add( i ) ;

if ( factors.Count >= maxFactors )
{
break;
}
}
}

return factors;
}
"Jeroen Mostert" <jm******@xs4all.nlwrote in message
news:48*********************@news.xs4all.nl...
Peter Morris wrote:
>Try something like this

public List<intFactorsOf(int value)
{
List<intresult = new List<int>();
List<intprimeNumbers = new List<int{ 2, 3, 5, 7, 11, etc };
while (value 1)
{
foreach (int prime in primeNumbers)
if (value % prime == 0)
{
result.Add(prime);
value = value / prime;
}
}

return result;
}
Might not work or even compile :-)
It'll work (in the sense that it produces unique prime factors, if you've
typed out the list correctly), but it's preposterously inefficient for
most numbers. Try value == 4 to see what I mean. It's easy enough to fix,
though; you need to replace one keyword with another. The resulting list
will be slightly different.

If you're going for the brute force approach, though, there's no
particular reason to use a list of primes. Just divide by every number
from 2 upwards; only the primes will actually come out as factors.

--
J.

Aug 19 '08 #5
Actually 2 upwards to n / 2 would be sufficient.

"Jeroen Mostert" <jm******@xs4all.nlwrote in message
news:48*********************@news.xs4all.nl...
If you're going for the brute force approach, though, there's no
particular reason to use a list of primes. Just divide by every number
from 2 upwards; only the primes will actually come out as factors.

--
J.
Aug 19 '08 #6
On Aug 19, 3:03*pm, "John A Grandy" <johnagrandy-at-gmail-dot-com>
wrote:
Does .NET provide a "factors of" method ?

I need to determine the factors of an integer.
Ah...the holy grail of computing!

How big are the numbers you're wanting to factor?

Browse through this wikipedia article. It gives a brief overview of
several algorithms.

http://en.wikipedia.org/wiki/Integer_factorization
Aug 20 '08 #7
Actually 2 upwards to n/2 would be sufficient.

I think up to the square root is enough as
well, while being faster for most cases.

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
Aug 20 '08 #8
If you know how to get PRIME factors, you
only need to loop through them and multiply
them pairwise in all permutations.

I suspect that what you'd like to see is,
in fact, a list of vectors such that all
elements of one multiplied equal to the
original number you wanted to factorize.
Have i nailed it?

E.g., for 180 you need to have (i guess)
2 * 2 * 3 * 3 * 5 (prime factors)
but also
4 * 3 * 3 * 5
2 * 6 * 3 * 5
2 * 3 * 3 * 10
2 * 2 * 9 * 5
2 * 2 * 3 * 15
12 * 3 * 5
3 * 3 * 20
2 * 18 * 5
2 * 2 * 45
2 * 3 * 30
3 * 60
2 * 90
36 * 5
I might have missed a few possibilities,
of course, but this is the general idea
of what i SUSPECT you'd like to get.

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
"John A Grandy" <johnagrandy-at-gmail-dot-comskrev i meddelandet
news:OR**************@TK2MSFTNGP02.phx.gbl...
Hmmm ... perhaps I mis-used the word "factor" ....

I'm not just interested in prime factors , but all factors.

Brute force is easy, but perhaps a better algorithm exists.
public static List<intFindFactors( int number, int maxFactors )
{
List<intfactors = new List<int>( maxFactors );

factors.Add( 1 );

for ( int i = 2; i < number / 2 + 1; i++ )
{
if ( ( number % i ) == 0 )
{
factors.Add( i ) ;

if ( factors.Count >= maxFactors )
{
break;
}
}
}

return factors;
}
"Jeroen Mostert" <jm******@xs4all.nlwrote in message
news:48*********************@news.xs4all.nl...
>Peter Morris wrote:
>>Try something like this

public List<intFactorsOf(int value)
{
List<intresult = new List<int>();
List<intprimeNumbers = new List<int{ 2, 3, 5, 7, 11, etc };
while (value 1)
{
foreach (int prime in primeNumbers)
if (value % prime == 0)
{
result.Add(prime);
value = value / prime;
}
}

return result;
}
Might not work or even compile :-)
It'll work (in the sense that it produces unique prime factors, if you've
typed out the list correctly), but it's preposterously inefficient for
most numbers. Try value == 4 to see what I mean. It's easy enough to fix,
though; you need to replace one keyword with another. The resulting list
will be slightly different.

If you're going for the brute force approach, though, there's no
particular reason to use a list of primes. Just divide by every number
from 2 upwards; only the primes will actually come out as factors.

--
J.


Aug 20 '08 #9
but it's preposterously inefficient

Try value == 4 to see what I mean. It's easy enough to fix, though; you
need to replace one keyword with another.
Maybe you should reveal the keyword?
If you're going for the brute force approach, though, there's no
particular reason to use a list of primes. Just divide by every number
from 2 upwards;
I could, but that would be preposterously inefficient :-)

Aug 20 '08 #10
Probably a better way to do it than this, but it works
Dictionary<int, intresult = new Dictionary<int,int>();

int sourceValue = 4096;
int lowValue = 1;
int highValue = sourceValue;

while (lowValue <= highValue)
{
int newHighValue = highValue;
while (newHighValue >= lowValue)
{
if (newHighValue * lowValue == sourceValue)
{
result[lowValue] = newHighValue;
highValue = newHighValue - 1;
break;
}
newHighValue--;
}

lowValue++;
}

foreach (KeyValuePair<int, intkvp in result)
Console.WriteLine("{0} x {1}", kvp.Key, kvp.Value);
Console.ReadLine();
Pete
Aug 20 '08 #11
Probably a better way to do it than this, but it works.
Dictionary<int, intresult;
We don't know (yet) what OP ment but
intuitively i'd lke to find more than
*pairs* the product of which equals to
the original number.

{2, 3, 5} -30

--
Regards
Konrad Viltersten
----------------------------------------
May all spammers die an agonizing death;
have no burial places; their souls be
chased by demons in Gehenna from one room
to another for all eternity and beyond.
Aug 20 '08 #12
Try this. I am not sure if the output is correct or not but at first glance
it looks okay. Should at least give you a good start, I hope.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Dictionary<int, intresult = new Dictionary<int, int>();

int sourceValue = int.MaxValue;
int lowValue = 1;
int highValue = sourceValue;

while (lowValue <= highValue)
{
highValue = FindHighestFactor(lowValue, highValue, sourceValue);

if (highValue * lowValue == sourceValue)
{
result[lowValue] = highValue;
highValue--;
}

lowValue++;
if (lowValue % 50 == 0)
Console.WriteLine("Processing {0}", lowValue);
}

foreach (KeyValuePair<int, intkvp in result)
Console.WriteLine("{0} x {1}", kvp.Key, kvp.Value);
Console.ReadLine();

}

private static int FindHighestFactor(int lowValue, int highValue, int
sourceValue)
{
int currentProduct = lowValue * highValue;
int difference = sourceValue - currentProduct;
if (difference == 0)
return highValue;
return highValue + (int)Math.Ceiling((double)difference / lowValue);

}
}
}

Aug 20 '08 #13
Those are all prime factors of the source. He seems to have wanted all
factors (e.g. for solving quadratic equasions).

Aug 20 '08 #14
Check my other post, it's a faster approach that doesn't seem to work at a
constant speed.

Aug 20 '08 #15
Check my other post, it's a faster approach that doesn't seem to work at a
constant speed.
"seems to work at a constant speed" - took 0.1 seconds to work out all
factors of int.MaxValue

Aug 20 '08 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

49
by: Ville Vainio | last post by:
I don't know if you have seen this before, but here goes: http://text.userlinux.com/white_paper.html There is a jab at Python, though, mentioning that Ruby is more "refined". -- Ville...
11
by: Joseph Turian | last post by:
Fellow hackers, I have a class BuildNode that inherits from class Node. Similarly, I have a class BuildTree that inherits from class Tree. Tree includes a member variable: vector<Node>...
4
by: =?utf-8?B?Qm9yaXMgRHXFoWVr?= | last post by:
Hello, what is the use-case of parameter "start" in string's "endswith" method? Consider the following minimal example: a = "testing" suffix="ing" a.endswith(suffix, 2) Significance of...
94
by: Samuel R. Neff | last post by:
When is it appropriate to use "volatile" keyword? The docs simply state: " The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.