Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Count from 1 to 1000 without using loops

Count from 1 to 1000 without using loops

Scheduled Pinned Locked Moved Managed C++/CLI
helpperformance
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Amrit Agr
    wrote on last edited by
    #1

    Hey Guys, I read this article regarding " Count from 1 to 1000 without using loops " ============================================================================= #include #include void main(int j) { printf("%d\n", j); (&main + (&exit - &main)*(j/1000))(j+1); } The only other method to count 1 to 1000 is using recursion. According to C language, j has ‘1’as its value at the beginning. When 1 <= j < 1000, &main + (&exit - &main)*(j/1000) always evaluated to &main, which is the memory address of main. (&main)(j+1) is the next iteration we want to get, which would print ‘2’ on the screen, etc. The stop condition of this recursion is that When j hits 1000, &main + (&exit - &main)*(j/1000) evaluates to &exit, which will elegantly exit this process, and has the error code 1001 returned to the operating system. ============================================================================= I tried to run this code and its perfectly working, but I am not getting how its works. How it calculates the end condition and all. Please help me out. Regards, Amrit

    L 1 Reply Last reply
    0
    • A Amrit Agr

      Hey Guys, I read this article regarding " Count from 1 to 1000 without using loops " ============================================================================= #include #include void main(int j) { printf("%d\n", j); (&main + (&exit - &main)*(j/1000))(j+1); } The only other method to count 1 to 1000 is using recursion. According to C language, j has ‘1’as its value at the beginning. When 1 <= j < 1000, &main + (&exit - &main)*(j/1000) always evaluated to &main, which is the memory address of main. (&main)(j+1) is the next iteration we want to get, which would print ‘2’ on the screen, etc. The stop condition of this recursion is that When j hits 1000, &main + (&exit - &main)*(j/1000) evaluates to &exit, which will elegantly exit this process, and has the error code 1001 returned to the operating system. ============================================================================= I tried to run this code and its perfectly working, but I am not getting how its works. How it calculates the end condition and all. Please help me out. Regards, Amrit

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      The line

      (&main + (&exit - &main)*(j/1000))(j+1);

      is evaluated each time thus: &exit - &main is the offset from the start of main to the start of exit j/1000 will evaluate to zero for all vaules of j less than 1000 Multiplying those two values together gives zero if j is less than 1000. Add that to &main (the address of main) and you get the same address, so main gets called with the parameter value j+1. This continues until the value of j reaches 1000 at which time: j/1000 results in the value 1 That is multiplied by the offset of exit which returns that offset. Add that value to main and the next call will go to exit rather than main.

      A W 2 Replies Last reply
      0
      • L Lost User

        The line

        (&main + (&exit - &main)*(j/1000))(j+1);

        is evaluated each time thus: &exit - &main is the offset from the start of main to the start of exit j/1000 will evaluate to zero for all vaules of j less than 1000 Multiplying those two values together gives zero if j is less than 1000. Add that to &main (the address of main) and you get the same address, so main gets called with the parameter value j+1. This continues until the value of j reaches 1000 at which time: j/1000 results in the value 1 That is multiplied by the offset of exit which returns that offset. Add that value to main and the next call will go to exit rather than main.

        A Offline
        A Offline
        Amrit Agr
        wrote on last edited by
        #3

        Thanks Rechard for this clear explanation.

        1 Reply Last reply
        0
        • L Lost User

          The line

          (&main + (&exit - &main)*(j/1000))(j+1);

          is evaluated each time thus: &exit - &main is the offset from the start of main to the start of exit j/1000 will evaluate to zero for all vaules of j less than 1000 Multiplying those two values together gives zero if j is less than 1000. Add that to &main (the address of main) and you get the same address, so main gets called with the parameter value j+1. This continues until the value of j reaches 1000 at which time: j/1000 results in the value 1 That is multiplied by the offset of exit which returns that offset. Add that value to main and the next call will go to exit rather than main.

          W Offline
          W Offline
          WuRunZhe
          wrote on last edited by
          #4

          Very interesting! I've never seen this code. If I wanna plus all numbers between 1 to 1000 with out loops, how can I do it like above method? Thank you for you advice!

          L 1 Reply Last reply
          0
          • W WuRunZhe

            Very interesting! I've never seen this code. If I wanna plus all numbers between 1 to 1000 with out loops, how can I do it like above method? Thank you for you advice!

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            Use a static variable and just add the value of j to it each time.

            W 1 Reply Last reply
            0
            • L Lost User

              Use a static variable and just add the value of j to it each time.

              W Offline
              W Offline
              WuRunZhe
              wrote on last edited by
              #6

              Thank you! :)

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups