| Author |
Factor of a number
|
|
|
| Good evening i have quick question: I want to add a feature which will
allow me to to show the factor of a number inputted
What would be the best way to do this by implementing this into my Prime
Calculator Method or create a new method?
class PrimeCalculator
{
PrimeCalculator(long number)
{
long nextprime;
if (isPrime(number))
{
showResult (number + " is a prime.");
}
else{
nextprime = number + 1;
if (nextprime % 2 == 0)
nextprime++;
while(!isPrime(nextprime))
nextprime += 2;
showResult (number + " is not a prime", "Next Number is "+ nextprime);
}
}
| |
| Hal Rosser 2007-06-23, 4:19 am |
|
"J" <silky_mez@yahoo.com> wrote in message
news:4qudnfw4jKd5EuHbnZ2dnUVZ_sWdnZ2d@co
mcast.com...
> Good evening i have quick question: I want to add a feature which will
> allow me to to show the factor of a number inputted
> What would be the best way to do this by implementing this into my Prime
> Calculator Method or create a new method?
there may be more than one factor - why not just have a static method so you
don't have to create an object for each calculation.
this method returns a list of factors separated by spaces for the number in
a string.
public static String listFactors(int n){
String out = "1";
for ( int i = 2; i < n; i++){
if (n % i == 0 ){
out += " " + i;
}
}
return out;
}
| |
|
| Hal Rosser wrote:
> "J" <silky_mez@yahoo.com> wrote in message
> news:4qudnfw4jKd5EuHbnZ2dnUVZ_sWdnZ2d@co
mcast.com...
>
> there may be more than one factor - why not just have a static method so you
> don't have to create an object for each calculation.
> this method returns a list of factors separated by spaces for the number in
> a string.
>
> public static String listFactors(int n){
> String out = "1";
> for ( int i = 2; i < n; i++){
> if (n % i == 0 ){
> out += " " + i;
> }
> }
> return out;
> }
Bear in mind that this technique uses String concatenation which litters the
heap with created objects, negating your claim of " you don't have to create
an object for each calculation." In point of fact, you are creating three
with each iteration.
--
Lew
| |
|
| Hal Rosser wrote:
> "J" <silky_mez@yahoo.com> wrote in message
> news:4qudnfw4jKd5EuHbnZ2dnUVZ_sWdnZ2d@co
mcast.com...
>
> there may be more than one factor - why not just have a static method so you
> don't have to create an object for each calculation.
> this method returns a list of factors separated by spaces for the number in
> a string.
>
> public static String listFactors(int n){
> String out = "1";
> for ( int i = 2; i < n; i++){
> if (n % i == 0 ){
> out += " " + i;
> }
> }
> return out;
> }
Bear in mind that this technique uses String concatenation which litters the
heap with created objects, negating your claim of " you don't have to create
an object for each calculation." In point of fact, you are creating two with
each iteration.
--
Lew
| |
| Hal Rosser 2007-06-23, 7:10 pm |
|
"Lew" <lew@lewscanon.nospam> wrote in message
news:QN-dnTh_kbmSjeDbnZ2dnUVZ_t3inZ2d@comcast.com...
>
>
> Bear in mind that this technique uses String concatenation which litters
> the heap with created objects, negating your claim of " you don't have to
> create an object for each calculation." In point of fact, you are
> creating two with each iteration.
Right you are.
I should have used a StringBuilder.
But the OP seemed to need a nudge away from his original course.
| |
|
| Hmmm.... That gives me an idea what to do, but i"m not quite sure. I'll
fidget around with it and report back to you.
"Hal Rosser" <hmrosser@bellsouth.net> wrote in message
news:sEhfi.2879$s8.897@bignews1.bellsouth.net...
>
> "Lew" <lew@lewscanon.nospam> wrote in message
> news:QN-dnTh_kbmSjeDbnZ2dnUVZ_t3inZ2d@comcast.com...
>
> Right you are.
> I should have used a StringBuilder.
> But the OP seemed to need a nudge away from his original course.
>
>
|
|
|
|