Factor of a number

  • Follow


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


 


0
Reply J 6/23/2007 3:13:55 AM

"J" <silky_mez@yahoo.com> wrote in message 
news:4qudnfw4jKd5EuHbnZ2dnUVZ_sWdnZ2d@comcast.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;
} 


0
Reply Hal 6/23/2007 4:26:43 AM


Hal Rosser wrote:
> "J" <silky_mez@yahoo.com> wrote in message 
> news:4qudnfw4jKd5EuHbnZ2dnUVZ_sWdnZ2d@comcast.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;
> } 


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
0
Reply Lew 6/23/2007 12:20:27 PM

"Lew" <lew@lewscanon.nospam> wrote in message 
news:QN-dnTh_kbmSjeDbnZ2dnUVZ_t3inZ2d@comcast.com...
>> 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.

Right you are.
I should have used a StringBuilder.
But the OP seemed to need a nudge away from his original course.


0
Reply Hal 6/23/2007 11:03:13 PM

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...
>>> 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.
>
> Right you are.
> I should have used a StringBuilder.
> But the OP seemed to need a nudge away from his original course.
>
> 


0
Reply J 6/24/2007 1:15:56 AM

4 Replies
124 Views

(page loaded in 0.079 seconds)

Similiar Articles:













7/20/2012 5:42:02 AM


Reply: