|
if value n return reason
14 2 1 14 has two factors, 2 and 7
18 5 0 18 does not have 5 factors, it only has four (2, 3, 6 and 9)
25 2 0 25 = 5*5 but we are only interested in distinct factors so the 5 is only counted once.
36 7 1 36 has seven factors (2, 3, 4, 6, 9, 12, 18). Note that 6 is counted only once.
7 0 1 all prime numbers are 0-factored
-5 anything 0 the value must be greated than 0
and I do code know but I doesn't know what is meant by return type?
public static int GetFactorCount(int numberToCheck, int n)
{
int factorCount = 0;
int sqrt = n;
for (int i = 1; i < sqrt; i++)
{
if (numberToCheck % i == 0)
{
factorCount += 2;
}
}
if (sqrt * sqrt == numberToCheck)
{
factorCount++;
}
return factorCount;
}
|
|
|
|
|
It is really easy to count the number of prime factors of a number. I will not give you the implementation, but can help you with the algorithm.
1. Define a factorCount variable and initialize it to 0.
2. Define a factor variable and initialize it to 2.
3. If factor is equal or bigger than the number to check, goto step 6, else, continue by step 4.
4. If factor divides the number to check:
4.1 Increase factorCount by 1.
4.2 Divide the number to check by factor.
4.3 Go to step 4.
5. Increase factor by 1.
5. Go to step 3.
6. Return factorCount.
That is all.
modified on Tuesday, February 8, 2011 9:44 AM
|
|
|
|
|
You probably need to start by researching any algorithms that solve the mathematics of this question. Once you know that solution it should be a simple matter to convert it into the programming language of your choice.
Is this really a .NET question?
I must get a clever new signature for 2011.
|
|
|
|
|
Richard MacCutchan wrote: Is this really a .NET question?
Technically, no...
".45 ACP - because shooting twice is just silly" - JSOP, 2010 ----- You can never have too much ammo - unless you're swimming, or on fire. - JSOP, 2010 ----- "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass." - Dale Earnhardt, 1997
|
|
|
|
|
Try something and post the code here (if you face any issues).
Someone will try and help you.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
|
|
|
|
|
Since the function signature is quite misleading from a C# point of view, let me give you some hints:
the return value is actually a boolean: "returns 1 if the integer is n-factored", and implicitly 0 otherwise - that's a stone-age coding praxis typical of C or COM. Now you can start to interpret the parameters: "value" should be the integer number for which to find the number of factors, and "n" the number of factors. I.e. if "value" has "n" factors return 1, otherwise 0.
Now take the code you've posted, and make little changes: calculate "sqrt", then compare "factorcount" and "n" for getting the return value.
|
|
|
|
|
I have a CustomerInfo form which is an Mdi form to the main form. The CustomerInfo form a has TabControl with 4 TabPages on it.
From the Main form
CustomerInfo CI = new CustomerInfo();
In the form constructor event i have the following pseudeocode
LoadGeneralTab()
LoadOrderTab()
LoadInvoiceTab()
LoadPayHistoryTab()
Each of these functions loads several Datagrids with information.
Then from the main form
CI.MdiParent = this;
CI.Show();
The CustomerInfo form shows up as expected and all is good. But when I click on the orderTab or invoiceTab or the payHistoryTab it takes a long time to switch over to it. And the speed of the switch seems to be the amount of information contained in the datagrid views. What I don't understand is why this is happening as it seems to me that all the grids have been populated with information in the constructor so what is taking so long to switch over. I have done this before and not had the same problem. And help would be appreciated.
|
|
|
|
|
Member 2256533 wrote: And the speed of the switch seems to be the amount of information contained in the datagrid views
Doesn't that answer your question. That implies you need to look closer to how the data is retrieved. May be you need to optimize your query. It is kinda hard to tell without looking at the code.
|
|
|
|
|
No not really, It does take some time for the CustomerInfo form to show up, and I assume that is because it does take time to load all the information on each of the tabs. But once the CustomerInfo form is displayed all the information should be loaded. So switching between tab pages should be quick. But it seems that the tabs are being reloaded everytime I switch over to them.
|
|
|
|
|
I guess the Controls (DGVs) that haven't been visible yet (those not on the first tab page) still haven't been rendered at all; so clicking on a tab will first cause the DGV to be actually created (including its Handle property), then the data being loaded into it.
You could force this to happen sooner, IIRC reading Control.Handle suffices to make sure it really exists. Of course adding this to your Form.Load (or Shown) event will postpone its rendering of everything, including the first tab.
A better way to handle the situation could be this, and I would recommend it anyway whenever the database accesses might take more than a second:
- launch a thread to access the database, do not touch the Controls from that thread;
- in the mean time, make sure all Controls really exist (Handle!);
- when the database thread is done, make it signal the GUI thread and start showing the data.
A BackgroundWorker would be good, having the DB stuff in its DoWork and the show-in-GUI stuff in its RunWorkercompleted handler.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
It's the grids doing it, not the database access as such.
If you can "page" the results for the grids, only loading as many rows as will be displayed, it should speed up a bit.
..and water fell from the sky like rain.
|
|
|
|
|
I'm writing a program that uses net framework 4. another person will use this program in his own computer. Is it necessary for he/she to have net framework 4 installed in his/her computer? which version he/she will require?
Thanks,
Sia swar
Baluch
|
|
|
|
|
If you build it to run on .Net 4, the target machine will need to have .Net 4 installed.
Will Rogers never met me.
|
|
|
|
|
They will need .NET installed on there machine. Framework 4.0 will be required. It must also be a Windows operating system and in fact if it is Windows 7, it should have it already installed.
Best to create a installation for your app that will handle the dependency for .NET. Test the installation before releasing.
It shouldn't be a big deal.
"You get that on the big jobs."
|
|
|
|
|
FYI: some 8 months ago my then new Win7 laptop had all .NET versions installed up to 3.5, I had to install 4.0 myself.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
I forgot it was just a recommended update for Windows 7 and Vista. So in most cases .NET 4 will be installed on Windows 7 but not all.
"You get that on the big jobs."
|
|
|
|
|
Yes they will require Framework 4.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
My latest tip/trick
|
|
|
|
|
I went for a job interview a while ago and failed on this question. I have researched it but can not find a definitive answer. Can someone point me to definitive documentation which clearly explains disposing of objects which have subscribed or registered events.
If the GC attempts to collect ClassA and finds it subscribes to an event of ClassB. Will ClassA be removed from memory?
If the GC attempts to collect ClassB and finds ClassA subscribes to an event of ClassB. Will ClassB be removed from memory?
I was not aware of this issue but the person interviewing me seemed concerned. At the end of the day, I don't care, I'd just like to find out what the truth is regarding eventing and disposing of objects.
"You get that on the big jobs."
|
|
|
|
|
The question to ask yourself is: who is referencing whom?
In both cases it is classB's event member that holds a reference to classA.
Robert Croll wrote: If the GC attempts to collect ClassB and finds ClassA subscribes to an event of ClassB. Will ClassB be removed from memory?
the subscription does not influence the life of classB, so it can be collected if it isn't alive anymore.
Robert Croll wrote: If the GC attempts to collect ClassA and finds it subscribes to an event of ClassB. Will ClassA be removed from memory?
the subscription keeps classA alive for as long as classB is alive.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Robert Croll wrote: If the GC attempts to collect ClassA and finds it subscribes to an event of ClassB. Will ClassA be removed from memory?
If the GC attempts to collect ClassB and finds ClassA subscribes to an event of ClassB. Will ClassB be removed from memory?
That depends. Can they be reached by following references from "any" variable? If not, both will be removed from memory - if they're not static classes, that is
Robert Croll wrote: I was not aware of this issue but the person interviewing me seemed concerned
..next time, ask for the answer and why he choose that particular question.
It wouldn't make much sense to keep the answer a secret; you can find a huge amount of documentation with Google - it would merely prove that you interviewer knows how to differentiate between a correct answer (like Luc's) and a technical correct but completely useless answer (like mine).
..and you wanna know why he choose that question. Did they have a history of leaking things? It puts the interviewer in a defending position, giving you a few seconds to breathe and to collect your mind
I are Troll
|
|
|
|
|
Thanks Eddy,
Both you and Luc have helped. The reason I asked was you also find a lot of misinformation on Google as well. But you have pointed me in the right direction. I think I'll be implementing weak events in future.
BTW I like your approach to interview questions. I'll have to remember that
"You get that on the big jobs."
|
|
|
|
|
This is the reason if u register explicit events handlers at start , you should release them at end.
|
|
|
|
|
Im trying to work with the Listview groups, and I added of course my groups, but im curious why "Default" keeps showing at the top, anyone know why?
|
|
|
|
|
Herboren wrote: im curious why "Default" keeps showing at the top
Probably because it's not part of the Groups collection;
All items should be assigned to groups before they are displayed. Any items that are not assigned to a group will appear in the default group, which has the header label "DefaultGroup{0}". The default group is not contained in the Groups collection, and cannot be altered. It is primarily useful in debugging to ensure that all items have been properly added to groups.
I are Troll
|
|
|
|
|
Hi Experts,
in an application I have business objects with properties, some of which are enumerations. To edit those properties, I inserted a PropertyGrid.
Now I have to localize the UI and therefore the enumeration name and its values, too.
The enumeration name already works well implementing ICustomTypeDescriptor.
For the enumeration values, the tool of choice seems to be deriving a class from TypeConverter. I did so and overrode
- CanConvertFrom
- ConvertFrom (just calling base class, at the moment)
- CanConvertTo
- ConvertTo
The initial value now is translated as requested. But the DropDown list doesn't drop down any longer. There is no small arrow pointing down when clicking on that initial value, as I was used to.
Do I have to override yet another method (something like GetMeThosePossibleValues(), maybe)?
Or does anyone have another idea?
Ciao,
luker
-- Modified Friday, February 4, 2011 6:11 AM
|
|
|
|