Quantcast

Got Homework?

Connect with other students for help. It's a free community.

  • across
    MIT Grad Student
    Online now
  • laura*
    Helped 1,000 students
    Online now
  • Hero
    College Math Guru
    Online now

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

eamier

i don't understand this in program to prime factorization.if(n%i==0) { n=n/i;     printf("%d,",i); i--; }

  • one year ago
  • one year ago

  • This Question is Closed
  1. eamier
    Best Response
    You've already chosen the best response.
    Medals 0

    this is the full code: #include<stdio.h> main() { int n,i; printf("Enter a Number:"); scanf("%d",&n); printf("\n\nPrime Factors of %d is: ",n); for(i=2;i<=n;i++) { if(n%i==0) { n=n/i; printf("%d,",i); //n=n/i; i--; } } }

    • one year ago
  2. eamier
    Best Response
    You've already chosen the best response.
    Medals 0

    i don't know why need i--

    • one year ago
  3. tasreeb
    Best Response
    You've already chosen the best response.
    Medals 0

    decreasing the counter it is used for i--

    • one year ago
  4. eamier
    Best Response
    You've already chosen the best response.
    Medals 0

    i still don’t understand. what the counter for.

    • one year ago
  5. eamier
    Best Response
    You've already chosen the best response.
    Medals 0

    if i put like this: printf("%d,i); n=n/i; i--; it still correct, why we need n=n/i;i--;. we already get the value of i.

    • one year ago
  6. tasreeb
    Best Response
    You've already chosen the best response.
    Medals 0

    sorry i m not conformed about it but check ur code above u used the increment counter in for loop nd below the decreasing counter.

    • one year ago
  7. mihai995
    Best Response
    You've already chosen the best response.
    Medals 0

    the order of those instructions does not matter. It usually does, but not in this case. Also, I want to say that this program works poorly if it faces a large prime number. I recommend the following code: #include <stdio.h> int main(){ int n, i; scanf("%d", &n); printf("The prime factorization for %d is: ", n); for (i = 2 ; i * i <= n ; i++) while (n % i == 0){ n /= i; printf("%d ", i); } if (n != 1) printf("%d", n); return 0; }

    • one year ago
  8. eamier
    Best Response
    You've already chosen the best response.
    Medals 0

    if i skip [ n=n/i; i--; ] i will get all factor including not prime no. can you explain that part

    • one year ago
  9. eamier
    Best Response
    You've already chosen the best response.
    Medals 0

    and also this part n/ = i

    • one year ago
  10. mihai995
    Best Response
    You've already chosen the best response.
    Medals 0

    The reason you do [n /= i] is because you want to remove the factors of i in n. Let me give you an example: n = 42 When you iterate: i = 2: you divide n by 2 => n = 21 i = 3: you divide n by 3 => n = 7 This way, when you reach 6, you will not take it as a prime factor, because you already divided by 2 and 3. This way, 6 does not divide n anymore i hope you understand my explanations

    • one year ago
  11. eamier
    Best Response
    You've already chosen the best response.
    Medals 0

    so this mean that the sequence of dividing is 2,3,5,7,11...

    • one year ago
  12. eamier
    Best Response
    You've already chosen the best response.
    Medals 0

    skip number that already got its factor before

    • one year ago
  13. eamier
    Best Response
    You've already chosen the best response.
    Medals 0

    is it true?

    • one year ago
  14. basking
    Best Response
    You've already chosen the best response.
    Medals 0

    it isn't skipping "consciously"; it divides by everything it can. To understand what's happening, put in a number like 60 and go through the code in your head. i=2.... then it checks... does 2 divide 60? (that's 60%2==0). Yes it does, so take 60/2, and 2-->1 (i--). Then the loop is done so 1-->2 (i++), and it checks again 30%2==0, so 30-->15, 2-->1. Then 1-->2 and we check again. This time 15%2 !=0, so 2-->3 and we check 15%3. etc.

    • one year ago
    • Attachments:

See more questions >>>

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.