count how many times a prime is dividing a number











up vote
0
down vote

favorite












I'm a beginner, my code finds all the primes that divide a given number but I want it to also print how many times the prime divides.
this is my code:



public static void main(){
Scanner myScanner = new Scanner (System.in) ;
int n = myScanner.nextInt();
int prime = 2 ;
int count = 0 ;
while ( prime <= n ){
if ( n%prime == 0 ) {
n = n/prime ;
System.out.println(prime + " " +count);
}
if ( n%prime !=0 ){
prime = prime + 1;
}
}
}


Hope you'll understand me, thanks!!










share|improve this question




















  • 2




    you are not increasing your counter, add "count++;" before the print.
    – dorony
    Nov 10 at 21:54















up vote
0
down vote

favorite












I'm a beginner, my code finds all the primes that divide a given number but I want it to also print how many times the prime divides.
this is my code:



public static void main(){
Scanner myScanner = new Scanner (System.in) ;
int n = myScanner.nextInt();
int prime = 2 ;
int count = 0 ;
while ( prime <= n ){
if ( n%prime == 0 ) {
n = n/prime ;
System.out.println(prime + " " +count);
}
if ( n%prime !=0 ){
prime = prime + 1;
}
}
}


Hope you'll understand me, thanks!!










share|improve this question




















  • 2




    you are not increasing your counter, add "count++;" before the print.
    – dorony
    Nov 10 at 21:54













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I'm a beginner, my code finds all the primes that divide a given number but I want it to also print how many times the prime divides.
this is my code:



public static void main(){
Scanner myScanner = new Scanner (System.in) ;
int n = myScanner.nextInt();
int prime = 2 ;
int count = 0 ;
while ( prime <= n ){
if ( n%prime == 0 ) {
n = n/prime ;
System.out.println(prime + " " +count);
}
if ( n%prime !=0 ){
prime = prime + 1;
}
}
}


Hope you'll understand me, thanks!!










share|improve this question















I'm a beginner, my code finds all the primes that divide a given number but I want it to also print how many times the prime divides.
this is my code:



public static void main(){
Scanner myScanner = new Scanner (System.in) ;
int n = myScanner.nextInt();
int prime = 2 ;
int count = 0 ;
while ( prime <= n ){
if ( n%prime == 0 ) {
n = n/prime ;
System.out.println(prime + " " +count);
}
if ( n%prime !=0 ){
prime = prime + 1;
}
}
}


Hope you'll understand me, thanks!!







java primes divide






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 21:48









Stav Alfi

3,86474078




3,86474078










asked Nov 10 at 21:44









Sapirush Kalifa

6




6








  • 2




    you are not increasing your counter, add "count++;" before the print.
    – dorony
    Nov 10 at 21:54














  • 2




    you are not increasing your counter, add "count++;" before the print.
    – dorony
    Nov 10 at 21:54








2




2




you are not increasing your counter, add "count++;" before the print.
– dorony
Nov 10 at 21:54




you are not increasing your counter, add "count++;" before the print.
– dorony
Nov 10 at 21:54












2 Answers
2






active

oldest

votes

















up vote
0
down vote













You forgot to increase the counter:



System.out.println(prime + " " +(++count));





share|improve this answer




























    up vote
    0
    down vote













    You need to increase the counter for every division that has a remainder 0

    and you must exit the loop once the remainder is not 0:



    public static void main(String args) {
    Scanner myScanner = new Scanner (System.in) ;
    int n = myScanner.nextInt();
    int prime = 2;
    int count = 0;

    while ( prime <= n ){
    if ( n % prime == 0 ) {
    n /= prime;
    count++;
    } else
    break;
    }

    System.out.println(prime + " " +count);
    }





    share|improve this answer























    • it's not exectly what I want, lets say n=100, it prints: 2 1 2 2 5 3 5 5 I want it to print: 2 2 5 2
      – Sapirush Kalifa
      Nov 10 at 22:27












    • No it print 2 2. Copy the code and run it again
      – forpas
      Nov 10 at 22:28












    • @SapirushKalifa did you copy my code or something else?
      – forpas
      Nov 10 at 22:49










    • tried, didn't work
      – Sapirush Kalifa
      Nov 11 at 9:16










    • What didn't work? Don't you want to print 2 2 when you set number to 100?
      – forpas
      Nov 11 at 9:17











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243710%2fcount-how-many-times-a-prime-is-dividing-a-number%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    You forgot to increase the counter:



    System.out.println(prime + " " +(++count));





    share|improve this answer

























      up vote
      0
      down vote













      You forgot to increase the counter:



      System.out.println(prime + " " +(++count));





      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        You forgot to increase the counter:



        System.out.println(prime + " " +(++count));





        share|improve this answer












        You forgot to increase the counter:



        System.out.println(prime + " " +(++count));






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 21:58









        HaroldSer

        1,2762615




        1,2762615
























            up vote
            0
            down vote













            You need to increase the counter for every division that has a remainder 0

            and you must exit the loop once the remainder is not 0:



            public static void main(String args) {
            Scanner myScanner = new Scanner (System.in) ;
            int n = myScanner.nextInt();
            int prime = 2;
            int count = 0;

            while ( prime <= n ){
            if ( n % prime == 0 ) {
            n /= prime;
            count++;
            } else
            break;
            }

            System.out.println(prime + " " +count);
            }





            share|improve this answer























            • it's not exectly what I want, lets say n=100, it prints: 2 1 2 2 5 3 5 5 I want it to print: 2 2 5 2
              – Sapirush Kalifa
              Nov 10 at 22:27












            • No it print 2 2. Copy the code and run it again
              – forpas
              Nov 10 at 22:28












            • @SapirushKalifa did you copy my code or something else?
              – forpas
              Nov 10 at 22:49










            • tried, didn't work
              – Sapirush Kalifa
              Nov 11 at 9:16










            • What didn't work? Don't you want to print 2 2 when you set number to 100?
              – forpas
              Nov 11 at 9:17















            up vote
            0
            down vote













            You need to increase the counter for every division that has a remainder 0

            and you must exit the loop once the remainder is not 0:



            public static void main(String args) {
            Scanner myScanner = new Scanner (System.in) ;
            int n = myScanner.nextInt();
            int prime = 2;
            int count = 0;

            while ( prime <= n ){
            if ( n % prime == 0 ) {
            n /= prime;
            count++;
            } else
            break;
            }

            System.out.println(prime + " " +count);
            }





            share|improve this answer























            • it's not exectly what I want, lets say n=100, it prints: 2 1 2 2 5 3 5 5 I want it to print: 2 2 5 2
              – Sapirush Kalifa
              Nov 10 at 22:27












            • No it print 2 2. Copy the code and run it again
              – forpas
              Nov 10 at 22:28












            • @SapirushKalifa did you copy my code or something else?
              – forpas
              Nov 10 at 22:49










            • tried, didn't work
              – Sapirush Kalifa
              Nov 11 at 9:16










            • What didn't work? Don't you want to print 2 2 when you set number to 100?
              – forpas
              Nov 11 at 9:17













            up vote
            0
            down vote










            up vote
            0
            down vote









            You need to increase the counter for every division that has a remainder 0

            and you must exit the loop once the remainder is not 0:



            public static void main(String args) {
            Scanner myScanner = new Scanner (System.in) ;
            int n = myScanner.nextInt();
            int prime = 2;
            int count = 0;

            while ( prime <= n ){
            if ( n % prime == 0 ) {
            n /= prime;
            count++;
            } else
            break;
            }

            System.out.println(prime + " " +count);
            }





            share|improve this answer














            You need to increase the counter for every division that has a remainder 0

            and you must exit the loop once the remainder is not 0:



            public static void main(String args) {
            Scanner myScanner = new Scanner (System.in) ;
            int n = myScanner.nextInt();
            int prime = 2;
            int count = 0;

            while ( prime <= n ){
            if ( n % prime == 0 ) {
            n /= prime;
            count++;
            } else
            break;
            }

            System.out.println(prime + " " +count);
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 10 at 22:15

























            answered Nov 10 at 22:09









            forpas

            3,4021214




            3,4021214












            • it's not exectly what I want, lets say n=100, it prints: 2 1 2 2 5 3 5 5 I want it to print: 2 2 5 2
              – Sapirush Kalifa
              Nov 10 at 22:27












            • No it print 2 2. Copy the code and run it again
              – forpas
              Nov 10 at 22:28












            • @SapirushKalifa did you copy my code or something else?
              – forpas
              Nov 10 at 22:49










            • tried, didn't work
              – Sapirush Kalifa
              Nov 11 at 9:16










            • What didn't work? Don't you want to print 2 2 when you set number to 100?
              – forpas
              Nov 11 at 9:17


















            • it's not exectly what I want, lets say n=100, it prints: 2 1 2 2 5 3 5 5 I want it to print: 2 2 5 2
              – Sapirush Kalifa
              Nov 10 at 22:27












            • No it print 2 2. Copy the code and run it again
              – forpas
              Nov 10 at 22:28












            • @SapirushKalifa did you copy my code or something else?
              – forpas
              Nov 10 at 22:49










            • tried, didn't work
              – Sapirush Kalifa
              Nov 11 at 9:16










            • What didn't work? Don't you want to print 2 2 when you set number to 100?
              – forpas
              Nov 11 at 9:17
















            it's not exectly what I want, lets say n=100, it prints: 2 1 2 2 5 3 5 5 I want it to print: 2 2 5 2
            – Sapirush Kalifa
            Nov 10 at 22:27






            it's not exectly what I want, lets say n=100, it prints: 2 1 2 2 5 3 5 5 I want it to print: 2 2 5 2
            – Sapirush Kalifa
            Nov 10 at 22:27














            No it print 2 2. Copy the code and run it again
            – forpas
            Nov 10 at 22:28






            No it print 2 2. Copy the code and run it again
            – forpas
            Nov 10 at 22:28














            @SapirushKalifa did you copy my code or something else?
            – forpas
            Nov 10 at 22:49




            @SapirushKalifa did you copy my code or something else?
            – forpas
            Nov 10 at 22:49












            tried, didn't work
            – Sapirush Kalifa
            Nov 11 at 9:16




            tried, didn't work
            – Sapirush Kalifa
            Nov 11 at 9:16












            What didn't work? Don't you want to print 2 2 when you set number to 100?
            – forpas
            Nov 11 at 9:17




            What didn't work? Don't you want to print 2 2 when you set number to 100?
            – forpas
            Nov 11 at 9:17


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243710%2fcount-how-many-times-a-prime-is-dividing-a-number%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Xamarin.iOS Cant Deploy on Iphone

            Glorious Revolution

            Dulmage-Mendelsohn matrix decomposition in Python