Click here to Skip to main content
11,582,911 members (71,674 online)

Algorithms

 
QuestionSpecify numbers as product of Primes? Pin
Ian Uy25-Jun-08 19:37
memberIan Uy25-Jun-08 19:37 
AnswerRe: Specify numbers as product of Primes? Pin
cp987625-Jun-08 21:32
membercp987625-Jun-08 21:32 
GeneralRe: Specify numbers as product of Primes? Pin
Ian Uy26-Jun-08 0:42
memberIan Uy26-Jun-08 0:42 
AnswerRe: Specify numbers as product of Primes? Pin
Robert.C.Cartaino26-Jun-08 4:48
memberRobert.C.Cartaino26-Jun-08 4:48 
GeneralRe: Specify numbers as product of Primes? Pin
Ian Uy26-Jun-08 4:50
memberIan Uy26-Jun-08 4:50 
GeneralRe: Specify numbers as product of Primes? Pin
The Developer8-Aug-08 7:03
memberThe Developer8-Aug-08 7:03 
GeneralRe: Specify numbers as product of Primes? Pin
Ian Uy26-Jun-08 6:28
memberIan Uy26-Jun-08 6:28 
GeneralRe: Specify numbers as product of Primes? Pin
Paul Conrad28-Jun-08 5:44
memberPaul Conrad28-Jun-08 5:44 
GeneralRe: Specify numbers as product of Primes? Pin
Ian Uy28-Jun-08 5:46
memberIan Uy28-Jun-08 5:46 
GeneralRe: Specify numbers as product of Primes? Pin
Paul Conrad28-Jun-08 6:11
memberPaul Conrad28-Jun-08 6:11 
GeneralRe: Specify numbers as product of Primes? Pin
Ravi Bhavnani8-Aug-08 7:16
memberRavi Bhavnani8-Aug-08 7:16 
GeneralRe: Specify numbers as product of Primes? Pin
cp987628-Jun-08 20:27
membercp987628-Jun-08 20:27 
GeneralRe: Specify numbers as product of Primes? Pin
Luc Pattyn29-Jun-08 2:42
mvpLuc Pattyn29-Jun-08 2:42 
GeneralRe: Specify numbers as product of Primes? Pin
MarkBrock27-Jun-08 18:46
memberMarkBrock27-Jun-08 18:46 
GeneralRe: Specify numbers as product of Primes? Pin
Paul Conrad28-Jun-08 5:42
memberPaul Conrad28-Jun-08 5:42 
GeneralRe: Specify numbers as product of Primes? Pin
ChandraRam2-Jul-08 22:49
memberChandraRam2-Jul-08 22:49 
GeneralRe: Specify numbers as product of Primes? Pin
MarkBrock2-Jul-08 23:06
memberMarkBrock2-Jul-08 23:06 
AnswerRe: Specify numbers as product of Primes? Pin
Member 419459326-Jun-08 8:19
memberMember 419459326-Jun-08 8:19 
GeneralRe: Specify numbers as product of Primes? Pin
Robert.C.Cartaino26-Jun-08 8:47
memberRobert.C.Cartaino26-Jun-08 8:47 
GeneralRe: Specify numbers as product of Primes? Pin
Ian Uy26-Jun-08 17:51
memberIan Uy26-Jun-08 17:51 
GeneralRe: Specify numbers as product of Primes? Pin
cp987629-Jun-08 14:57
membercp987629-Jun-08 14:57 
AnswerRe: Specify numbers as product of Primes? Pin
Arash Partow29-Jun-08 0:48
memberArash Partow29-Jun-08 0:48 
GeneralRe: Specify numbers as product of Primes? [modified] Pin
cp987629-Jun-08 3:53
membercp987629-Jun-08 3:53 
[edit] since Arash has completely changed the code in his previous post, this appears somewhat out of context [/edit]

I think this is an example of misguided optimization. Whilst Ian Uy's example looks inefficient

int main()
{	
int InputCase;	
cin >> InputCase;
for(int i=2;i<=InputCase;i++)
   {
   if(InputCase%i==0)
      {
      cout << i;
      InputCase/=i;
      i--;
      }
   }
return 0;
}

all he is doing each iteration is testing

   if(InputCase%i==0)

In order to avoid these "unnecessary" tests, you are testing each n as

   if (n % 2 == 0) n++;   
   while(!is_prime(n)) { ++n; } 

The function is_prime(n) will involve much more overhead than the original code, and it doesn't matter that 'i' may not be prime.

Peter
"Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."
modified on Saturday, October 18, 2008 11:35 PM

GeneralRe: Specify numbers as product of Primes? Pin
Luc Pattyn29-Jun-08 4:40
mvpLuc Pattyn29-Jun-08 4:40 
GeneralRe: Specify numbers as product of Primes? Pin
cp987629-Jun-08 14:47
membercp987629-Jun-08 14:47 
GeneralRe: Specify numbers as product of Primes? Pin
Arash Partow30-Jun-08 2:54
memberArash Partow30-Jun-08 2:54 
GeneralRe: Specify numbers as product of Primes? Pin
cp987630-Jun-08 3:18
membercp987630-Jun-08 3:18 
GeneralRe: Specify numbers as product of Primes? Pin
Arash Partow30-Jun-08 4:27
memberArash Partow30-Jun-08 4:27 
GeneralRe: Specify numbers as product of Primes? Pin
Arash Partow30-Jun-08 2:17
memberArash Partow30-Jun-08 2:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web03 | 2.8.150603.1 | Last Updated 3 Jul 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid