A C Question.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mark McIntyre

    #16
    Re: A C Question.

    On Tue, 17 Jan 2006 20:58:20 -0500, in comp.lang.c , Eric Sosman
    <esosman@acm-dot-org.invalid> wrote:
    [color=blue]
    >Jack Young wrote:[color=green]
    >> Hi:
    >> There is question about C programming:
    >> Use just one C statement to check whether a variable is the power of 2. No
    >> loops allowed.[/color][/color]

    Homework question...
    [color=blue]
    > int is_power_of_two (double x) {
    > return x > 0.0; /* one statement, no loops */
    > }[/color]

    Cute. He probably meant an *integer* power? :-)

    Mark McIntyre
    --
    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

    Comment

    • ashu

      #17
      Re: A C Question.


      Jack Young wrote:[color=blue]
      > Hi:
      > There is question about C programming:
      > Use just one C statement to check whether a variable is the power of 2. No
      > loops allowed.
      > Waiting for your answer.I have not slept well a few days for this.[/color]
      its very simple
      see
      any variable it is divided by 3,5,7 or 9 then it is not the power of 2
      i.e,
      main()
      {
      if (n%3 && n%5 && n%7 && n%9 != 0 )
      printf("n is a power of 2");
      }

      Comment

      • Robert Gamble

        #18
        Re: A C Question.

        ashu wrote:[color=blue]
        > Jack Young wrote:[color=green]
        > > Hi:
        > > There is question about C programming:
        > > Use just one C statement to check whether a variable is the power of 2. No
        > > loops allowed.
        > > Waiting for your answer.I have not slept well a few days for this.[/color]
        > its very simple
        > see
        > any variable it is divided by 3,5,7 or 9 then it is not the power of 2[/color]

        By definition, a number that is a power of 2 has no prime factors other
        than 2, your observation is an incomplete obfuscation of this fact.
        Also, just so you know, any number evenly divisible by 9 is also evenly
        divisible by 3 so the last part of your assertion is superfluous.
        [color=blue]
        > i.e,
        > main()
        > {
        > if (n%3 && n%5 && n%7 && n%9 != 0 )
        > printf("n is a power of 2");
        > }[/color]

        Your example has numerious errors, both programatic and logical.
        First, let's cover the programatic errors:
        1. main returns an int so "int main (void)" would be a correct
        declaration here.
        2. You never define "n", let alone initialize it.
        3. You need to #include <stdio.h> to use printf and you should have a
        newline at the end of your string to be portable.
        4. You should return an integer value from main.

        It is true that any number evenly divisible by 3, 5, 7, or 9 is not a
        power of 2, but to say that any number that does is not evenly
        divisible by these numbers is a power of 2 is a non sequitur, by your
        criteria 1, 11, 13, and would each be a power of 2.

        Robert Gamble

        Comment

        • Richard Heathfield

          #19
          Re: A C Question.

          Robert Gamble said:
          [color=blue]
          > It is true that any number evenly divisible by 3, 5, 7, or 9 is not a
          > power of 2, but to say that any number that does is not evenly
          > divisible by these numbers is a power of 2 is a non sequitur,[/color]

          No, it isn't. It's a false conclusion, not a non sequitur.
          [color=blue]
          > by your criteria 1, 11, 13, and would each be a power of 2.[/color]

          1 *is* a power of 2! It is 2 to the power 0.

          --
          Richard Heathfield
          "Usenet is a strange place" - dmr 29/7/1999

          email: rjh at above domain (but drop the www, obviously)

          Comment

          • Robert Gamble

            #20
            Re: A C Question.

            Richard Heathfield wrote:[color=blue]
            > Robert Gamble said:
            >[color=green]
            > > It is true that any number evenly divisible by 3, 5, 7, or 9 is not a
            > > power of 2, but to say that any number that does is not evenly
            > > divisible by these numbers is a power of 2 is a non sequitur,[/color]
            >
            > No, it isn't. It's a false conclusion, not a non sequitur.[/color]

            It's a prime example of a non sequitur; Given: if A (number is
            divisible by 3,5,7,9) then B (not a power of 2), then it does not
            logically follow that NOT A (not divisible by 3,5,7,9) yields NOT B (is
            a power of two). The type of this example is more specifically
            referred to as Denying the Antecedent.
            [color=blue][color=green]
            > > by your criteria 1, 11, 13, and would each be a power of 2.[/color]
            >
            > 1 *is* a power of 2! It is 2 to the power 0.[/color]

            Okay, I screwed that up. That should have read either "11, 13, and 17
            would each" or "11 and 13 would both".

            Robert Gamble

            Comment

            • pete

              #21
              Re: A C Question.

              Robert Gamble wrote:[color=blue]
              >
              > ashu wrote:[color=green]
              > > Jack Young wrote:[color=darkred]
              > > > Hi:
              > > > There is question about C programming:
              > > > Use just one C statement to check whether a variable is the power of 2. No
              > > > loops allowed.
              > > > Waiting for your answer.I have not slept well a few days for this.[/color]
              > > its very simple
              > > see
              > > any variable it is divided by 3,5,7 or 9 then it is not the power of 2[/color]
              >
              > By definition, a number that is a power of 2 has no prime factors other
              > than 2, your observation is an incomplete obfuscation of this fact.
              > Also, just so you know, any number evenly divisible by 9 is also evenly
              > divisible by 3 so the last part of your assertion is superfluous.
              >[color=green]
              > > i.e,
              > > main()
              > > {
              > > if (n%3 && n%5 && n%7 && n%9 != 0 )
              > > printf("n is a power of 2");
              > > }[/color]
              >
              > Your example has numerious errors, both programatic and logical.
              > First, let's cover the programatic errors:
              > 1. main returns an int so "int main (void)" would be a correct
              > declaration here.
              > 2. You never define "n", let alone initialize it.
              > 3. You need to #include <stdio.h> to use printf and you should have a
              > newline at the end of your string to be portable.
              > 4. You should return an integer value from main.[/color]

              And this expression
              (n%3 && n%5 && n%7 && n%9 != 0 )
              is written strangely.

              Either
              (n%3 && n%5 && n%7 && n%9)
              or
              (n%3 != 0 && n%5 != 0 && n%7 != 0 && n%9 != 0 )
              would be better.

              --
              pete

              Comment

              • Dik T. Winter

                #22
                Re: A C Question.

                In article <1137645986.086 905.259190@o13g 2000cwo.googleg roups.com> "ashu" <riskyashish@ya hoo.com> writes:
                ....[color=blue]
                > any variable it is divided by 3,5,7 or 9 then it is not the power of 2
                > i.e,
                > main()
                > {
                > if (n%3 && n%5 && n%7 && n%9 != 0 )
                > printf("n is a power of 2");
                > }
                >[/color]

                Ah, yes, 316 is a power of 2.
                --
                dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
                home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/

                Comment

                • Joe Wright

                  #23
                  Re: A C Question.

                  Robert Gamble wrote:[color=blue]
                  > Richard Heathfield wrote:
                  >[color=green]
                  >>Robert Gamble said:
                  >>
                  >>[color=darkred]
                  >>>It is true that any number evenly divisible by 3, 5, 7, or 9 is not a
                  >>>power of 2, but to say that any number that does is not evenly
                  >>>divisible by these numbers is a power of 2 is a non sequitur,[/color]
                  >>
                  >>No, it isn't. It's a false conclusion, not a non sequitur.[/color]
                  >
                  >
                  > It's a prime example of a non sequitur; Given: if A (number is
                  > divisible by 3,5,7,9) then B (not a power of 2), then it does not
                  > logically follow that NOT A (not divisible by 3,5,7,9) yields NOT B (is
                  > a power of two). The type of this example is more specifically
                  > referred to as Denying the Antecedent.
                  >
                  >[color=green][color=darkred]
                  >>>by your criteria 1, 11, 13, and would each be a power of 2.[/color]
                  >>
                  >>1 *is* a power of 2! It is 2 to the power 0.[/color]
                  >
                  >
                  > Okay, I screwed that up. That should have read either "11, 13, and 17
                  > would each" or "11 and 13 would both".
                  >
                  > Robert Gamble
                  >[/color]
                  Powers of 2

                  #include <stdio.h>

                  int main(void) {
                  int i, j, k = 0;
                  for (i = 1; i < 20; ++i) {
                  j = i & (i - 1);
                  if (j) printf("%2d\n", i);
                  else printf("%2d is 2 to the power %d\n", i, k++);
                  }
                  return 0;
                  }

                  --
                  Joe Wright
                  "Everything should be made as simple as possible, but not simpler."
                  --- Albert Einstein ---

                  Comment

                  • Keith Thompson

                    #24
                    Re: A C Question.

                    pete <pfiland@mindsp ring.com> writes:
                    [...][color=blue]
                    > And this expression
                    > (n%3 && n%5 && n%7 && n%9 != 0 )
                    > is written strangely.
                    >
                    > Either
                    > (n%3 && n%5 && n%7 && n%9)
                    > or
                    > (n%3 != 0 && n%5 != 0 && n%7 != 0 && n%9 != 0 )
                    > would be better.[/color]

                    In the context of doing someone's homework for him (which is how this
                    thread started), the original version is perfect.

                    --
                    Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                    We must do something. This is something. Therefore, we must do this.

                    Comment

                    • pete

                      #25
                      Re: A C Question.

                      Keith Thompson wrote:[color=blue]
                      >
                      > pete <pfiland@mindsp ring.com> writes:
                      > [...][color=green]
                      > > And this expression
                      > > (n%3 && n%5 && n%7 && n%9 != 0 )
                      > > is written strangely.
                      > >
                      > > Either
                      > > (n%3 && n%5 && n%7 && n%9)
                      > > or
                      > > (n%3 != 0 && n%5 != 0 && n%7 != 0 && n%9 != 0 )
                      > > would be better.[/color]
                      >
                      > In the context of doing someone's homework for him (which is how this
                      > thread started), the original version is perfect.[/color]

                      The last time that I posted a solution to this problem,
                      I got it wrong, so I'm glad that someone corrected me.



                      --
                      pete

                      Comment

                      • CBFalconer

                        #26
                        Re: A C Question.

                        Joe Wright wrote:[color=blue]
                        >[/color]
                        .... snip ...[color=blue]
                        >
                        > #include <stdio.h>
                        >
                        > int main(void) {
                        > int i, j, k = 0;
                        > for (i = 1; i < 20; ++i) {
                        > j = i & (i - 1);
                        > if (j) printf("%2d\n", i);
                        > else printf("%2d is 2 to the power %d\n", i, k++);
                        > }
                        > return 0;
                        > }[/color]

                        Funny thing. It seems that all values than 20 are powers of two on
                        my 1's complement machine. Where, oh where, did they come from?

                        --
                        "If you want to post a followup via groups.google.c om, don't use
                        the broken "Reply" link at the bottom of the article. Click on
                        "show options" at the top of the article, then click on the
                        "Reply" at the bottom of the article headers." - Keith Thompson
                        More details at: <http://cfaj.freeshell. org/google/>


                        Comment

                        • Peter Nilsson

                          #27
                          Re: A C Question.

                          CBFalconer wrote:[color=blue]
                          > Joe Wright wrote:[color=green]
                          > > #include <stdio.h>
                          > >
                          > > int main(void) {
                          > > int i, j, k = 0;
                          > > for (i = 1; i < 20; ++i) {
                          > > j = i & (i - 1);
                          > > if (j) printf("%2d\n", i);
                          > > else printf("%2d is 2 to the power %d\n", i, k++);
                          > > }
                          > > return 0;
                          > > }[/color]
                          >
                          > Funny thing. It seems that all values than 20 are powers of two on
                          > my 1's complement machine. Where, oh where, did they come from?[/color]

                          Your imagination?! ;-)

                          --
                          Peter

                          Comment

                          • pete

                            #28
                            Re: A C Question.

                            Peter Nilsson wrote:[color=blue]
                            >
                            > CBFalconer wrote:[color=green]
                            > > Joe Wright wrote:[color=darkred]
                            > > > #include <stdio.h>
                            > > >
                            > > > int main(void) {
                            > > > int i, j, k = 0;
                            > > > for (i = 1; i < 20; ++i) {
                            > > > j = i & (i - 1);
                            > > > if (j) printf("%2d\n", i);
                            > > > else printf("%2d is 2 to the power %d\n", i, k++);
                            > > > }
                            > > > return 0;
                            > > > }[/color]
                            > >
                            > > Funny thing. It seems that all values than 20 are powers of two on
                            > > my 1's complement machine. Where, oh where, did they come from?[/color]
                            >
                            > Your imagination?! ;-)[/color]

                            "1's complement" as far as I know,
                            refers to the representation of negative integers,
                            so I don't see what CBFalconer is getting at.

                            --
                            pete

                            Comment

                            • CBFalconer

                              #29
                              Re: A C Question.

                              pete wrote:[color=blue]
                              > Peter Nilsson wrote:[color=green]
                              >> CBFalconer wrote:[color=darkred]
                              >>> Joe Wright wrote:[/color][/color]
                              >[color=green][color=darkred]
                              >>>> #include <stdio.h>
                              >>>>
                              >>>> int main(void) {
                              >>>> int i, j, k = 0;
                              >>>> for (i = 1; i < 20; ++i) {
                              >>>> j = i & (i - 1);
                              >>>> if (j) printf("%2d\n", i);
                              >>>> else printf("%2d is 2 to the power %d\n", i, k++);
                              >>>> }
                              >>>> return 0;
                              >>>> }
                              >>>
                              >>> Funny thing. It seems that all values than 20 are powers of two on
                              >>> my 1's complement machine. Where, oh where, did they come from?[/color]
                              >>
                              >> Your imagination?! ;-)[/color]
                              >
                              > "1's complement" as far as I know, refers to the representation of
                              > negative integers, so I don't see what CBFalconer is getting at.[/color]

                              Peter Nilsson (and Joe) has it right. I went off less than
                              half-cocked. I was thinking of the effect of ((v) & (-v)).

                              --
                              "If you want to post a followup via groups.google.c om, don't use
                              the broken "Reply" link at the bottom of the article. Click on
                              "show options" at the top of the article, then click on the
                              "Reply" at the bottom of the article headers." - Keith Thompson
                              More details at: <http://cfaj.freeshell. org/google/>


                              Comment

                              • Michael Wojcik

                                #30
                                Re: A C Question.


                                In article <1137676230.440 290.289980@g14g 2000cwa.googleg roups.com>, "Robert Gamble" <rgamble99@gmai l.com> writes:[color=blue]
                                > Richard Heathfield wrote:[color=green]
                                > > Robert Gamble said:[color=darkred]
                                > > > It is true that any number evenly divisible by 3, 5, 7, or 9 is not a
                                > > > power of 2, but to say that any number that does is not evenly
                                > > > divisible by these numbers is a power of 2 is a non sequitur,[/color]
                                > >
                                > > No, it isn't. It's a false conclusion, not a non sequitur.[/color]
                                >
                                > It's a prime example of a non sequitur; Given: if A (number is
                                > divisible by 3,5,7,9) then B (not a power of 2), then it does not
                                > logically follow that NOT A (not divisible by 3,5,7,9) yields NOT B (is
                                > a power of two). The type of this example is more specifically
                                > referred to as Denying the Antecedent.[/color]

                                Do you have a reference for this definition of non sequitur? I've
                                never seen it used to include errors of logic, and eg Lanham's _A
                                Handlist of Rhetorical Terms_ doesn't endorse that usage.

                                "non sequitur" of course literally means "it does not follow", so
                                it could be construed very broadly to include errors of logic (where
                                conclusions do not follow, in the logical sense, from propositions),
                                but I've only ever seen it applied to cases where a conclusion is
                                drawn from irrelevant arguments, and Lanham appears to agree.

                                --
                                Michael Wojcik michael.wojcik@ microfocus.com

                                See who I'm! -- Jackie Chan and unknown subtitler, _Dragons Forever_

                                Comment

                                Working...