?
Solved

SEMANTIC ERROR  with while loop in C program

Posted on 2006-07-06
4
Medium Priority
?
1,388 Views
Last Modified: 2012-08-14
I'm using a while loop to calculate prime factors. I'm getting the prime factors, but I'm getting TWO sets of each one. So, for 100 (primes should be just 2 and 5, I'm getting 2 2 5 5. Could someone please take a look at my code (especially my while loop) and suggest where my error might be? Any other general suggestions on how to improve what I've written would also be appreciated.

------------------------------------MY CODE-------------------------------------------------------

// prime factorizarion.

#include <stdio.h>

// isPrime function
// return 1 if number prime
int isPrime(int n)
{
int i = 2;
while ( i <= n/2 )
{      if ( (n % i) == 0 )
             //this is not a prime
             return 0;
       i++;
}
// this is a prime
return 1;
}



// prime factor a number
int main()
{
    int prime; // prime divider
    int num; // number to find primes factors of

    // get number
    printf("Enter a number greater than 1: ");
    scanf("%d",&num);

   while(num > 1)
   {
    prime = 2; // start with lowest prime

    // To prime factor a number, begin dividing by the smallest possible prime
    // and continue until the quotient is a prime number.
    while(!isPrime(num))
    {
          // test if num is prime
          if((num % prime)==0)
          {
              printf("%d ",prime);  // print out factor
              num = num / prime;
          }

          // find next prime
          else
          {
              prime++;

              while(!isPrime(prime))
              {
                  prime++;
              }
          }

    }

    // print out last factor
   printf("%d\n",num);

       // get next  number
    printf("Enter a number greater than 1: ");
    scanf("%d",&num);
   }

     
   printf("\n");

return 1;
}
0
Comment
Question by:computerese
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 5

Expert Comment

by:bastibartel
ID: 17052534
Hi there,

Turn this:
// test if num is prime
          if((num % prime)==0)
          {
              printf("%d ",prime);  // print out factor
              num = num / prime;
          }


Into this  (use a while loop to divide be the prime as OFTEN as possible)

// test if num is prime
          while((num % prime)==0)
          {
              printf("%d ",prime);  // print out factor
              num = num / prime;
          }
0
 
LVL 5

Assisted Solution

by:bastibartel
bastibartel earned 1000 total points
ID: 17052550
Sorry, this still printed out the prime several times ;-)

if((num % prime)==0)
{
      printf("%d ",prime);  // print out factor only once
      while((num % prime)==0)  
            num = num / prime;     // but divide as often as possible
}
         

0
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 1000 total points
ID: 17052567
Hi computerese,

Your code, when presented with 100 will go through the following steps:

Is 2 a prime factor of 100? Yes! Print '2' and set number to 50.
Is 2 a prime factor of 50? Yes! Print '2' and set number to 25.
Is 2 a prime factor of 25? No!
Is 3 a prime factor of 25? No!
Is 5 a prime factor of 25? Yes! Print '5' and set number to 5.
Is 2 a prime factor of 5? No!
Is 3 a prime factor of 5? No!
Is 5 a prime factor of 5? Yes! Print '5' and set number to 1.
Finished.

So the correct prime fators of 100 ARE 2,2,5,5. What did you expect?

You may like to change this code:

          if((num % prime)==0)
          {
              printf("%d ",prime);  // print out factor
              while ( (num % prime) == 0 ) num = num / prime;
          }


Paul
0
 
LVL 5

Expert Comment

by:bastibartel
ID: 17056438
:-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question