Does .NET provide a "factors of" method ?
I need to determine the factors of an integer. 15 3415
Try something like this
public List<intFactors Of(int value)
{
List<intresult = new List<int>();
List<intprimeNu mbers = new List<int{ 2, 3, 5, 7, 11, etc };
while (value 1)
{
foreach (int prime in primeNumbers)
if (value % prime == 0)
{
result.Add(prim e);
value = value / prime;
}
}
return result;
}
Might not work or even compile :-)
Pete
Peter Morris wrote:
Try something like this
public List<intFactors Of(int value)
{
List<intresult = new List<int>();
List<intprimeNu mbers = new List<int{ 2, 3, 5, 7, 11, etc };
while (value 1)
{
foreach (int prime in primeNumbers)
if (value % prime == 0)
{
result.Add(prim e);
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.
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<intFindFac tors( 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******@xs4al l.nlwrote in message
news:48******** *************@n ews.xs4all.nl.. .
Peter Morris wrote:
>Try something like this
public List<intFactors Of(int value) { List<intresult = new List<int>(); List<intprimeNu mbers = new List<int{ 2, 3, 5, 7, 11, etc }; while (value 1) { foreach (int prime in primeNumbers) if (value % prime == 0) { result.Add(prim e); 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.
Actually 2 upwards to n / 2 would be sufficient.
"Jeroen Mostert" <jm******@xs4al l.nlwrote in message
news:48******** *************@n ews.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.
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
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.
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******** ******@TK2MSFTN GP02.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<intFindFac tors( 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******@xs4al l.nlwrote in message
news:48******** *************@n ews.xs4all.nl.. .
>Peter Morris wrote:
>>Try something like this
public List<intFactors Of(int value) { List<intresult = new List<int>(); List<intprimeNu mbers = new List<int{ 2, 3, 5, 7, 11, etc }; while (value 1) { foreach (int prime in primeNumbers) if (value % prime == 0) { result.Add(prim e); 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.
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 :-) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 Vainio http://www.students.tut.fi/~vainio24
|
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> nodes; // For clarity, let this be "orig_nodes"
BuildTree includes a member variable:
|
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 "end" is obvious. But not so for "start".
|
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 Statement (C# Reference)
statement to serialize access.
"
But when is it better to use "volatile" instead of "lock" ?
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
| |
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |