943,929 Members | Top Members by Rank

Ad:
You are currently viewing page 4 of this multi-page discussion thread; Jump to the first page
May 15th, 2006
0

Re: 500 ways to print [1..10]!

I thought it was funny, but I don't know why it got edited.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
May 16th, 2006
0

Re: 500 ways to print [1..10]!

Old Fashon Vic 20 BASIC

10 fort = 1 to 10
20 print t
30 next t
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nightowl512 is offline Offline
3 posts
since May 2004
Jun 18th, 2006
0

Re: 500 ways to print [1..10]!

puts (1..10).to_a
Reputation Points: 10
Solved Threads: 0
Newbie Poster
julesjacobs is offline Offline
10 posts
since Jun 2006
Jun 21st, 2006
0

Re: 500 ways to print [1..10]!

in Delphi :

program simplecount;

var
   i: Integer;
 
begin
   for i := 1 to 10 do
      WriteLn(IntToStr(i));
end.

and in perl (because everyone loves perl) :

#!/usr/bin/perl

for($i  = 0; $i < 10; ++$i)
    {
        print $i . "\n"
    }


I use Ruby mostly these days. From previous posts you can tell why; so so elegant
Last edited by pty; Jun 21st, 2006 at 6:24 pm.
pty
Reputation Points: 64
Solved Threads: 39
Posting Pro
pty is offline Offline
530 posts
since Oct 2005
Jun 21st, 2006
3

Re: 500 ways to print [1..10]!

Here's another Haskell version.
main = mapM_ (putStrLn . show) [1..10]
Last edited by Rashakil Fol; Jun 21st, 2006 at 8:08 pm.
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Jul 29th, 2006
1

Re: 500 ways to print [1..10]!

HTML

<html>
<head><title>It's an instructional language</title></head>
<body>
1,
2,
3,
4,
5,
6,
7,
8,
9,
10
</body>
</html>

If we apply some simple definitions to terms, HTML is a valid language to use for this. (i.e. Program: Series of instructions to be executed. HTML thusly instructs the interpreter to write the code.)

Ada
with Ada.Integer_Text_IO;

procedure print10 is
  begin
     for i in integer range 1..10 loop
       Ada.Integer_Text_IO.put(i);
     end loop;
  end print10;

Yet another C++ version:
#include <iostream>

using namespace std;

int main()
  {
  int i=1;
  cout << i;    // 1
  cout << ++i;  // 2
  cout << ++i;  // 3
  cout << ++i;  // 4
  cout << ++i;  // 5
  cout << ++i;  // 6
  cout << ++i;  // 7
  cout << ++i;  // 8
  cout << ++i;  // 9
  cout << ++i;  // 20
  }
Last edited by Puckdropper; Jul 29th, 2006 at 11:51 pm.
Reputation Points: 23
Solved Threads: 23
Posting Pro in Training
Puckdropper is offline Offline
494 posts
since Jul 2004
Aug 4th, 2006
0

Re: 500 ways to print [1..10]!

ForTran
        integer i
        do 100 i=1,10
100     write(5,200) i
200     format("i3")
        end
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,738 posts
since May 2006
Aug 4th, 2006
0

Re: 500 ways to print [1..10]!

8080 Assembler
code    segment                     ; Start of the code
        assume  cs:code             ; Load code segment register
                org     100h        ; and set program up as a .COM file
start:  
        assume  cs:code

        mov     ax,     1           ; Start at 1
loop:
        call    outdec              ; Output the AX register
        inc     ax                  ; next value
        cmp     ax,     10          ; Above 10 yet?
          jle     loop

        mov     al,     0
        mov     ah,     4Ch
        int     21h                 ; Exit 

; ******************************** ;

outdec:
        push    ax                  ; save AX
        mov     bx,     10          ; divisor
        mov     cx,      3          ; # characters
convloopd:
            mov     dx,      0      ; zero high word for div
            div     bx              ; divide by 10
            add     dl,     30h     ; convert remainder to char
            push    dx              ; Save character
            loop    convloopd       ; do it til cx = 0
        
        mov     cx,     3           ; start another loop
outloopd:
        pop     dx                  ; get the character
        mov     ah,     02h         ; output character fcn
        int     21h                 ; output it
        loop    outloopd

        mov     ah,     02h
        mov     dl,     ' ' 
        int     21h                 ; output 2 spaces
        int     21h

        pop     ax                  ; restore original number
        ret
code ends
end start
Moderator
Reputation Points: 3278
Solved Threads: 894
Posting Sage
WaltP is offline Offline
7,738 posts
since May 2006
Aug 4th, 2006
1

Re: 500 ways to print [1..10]!

Brain****:
+++[>++++[>++++<-]<-]>
++++++++++>
+.<.>+.<.>+.<.>+.<.>+.<.>
+.<.>+.<.>+.<.>+.<.>
--------.-.<.
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005
Aug 4th, 2006
2

Re: 500 ways to print [1..10]!

Haskell, with the integer datatype implemented from scratch:

First, let's implement a datatype for storing integers.

> data IntType = Zero
>              | Succ IntType
>              | Pred IntType
>                deriving Eq

Then we'll want to implement comparison for integers.

> instance Ord IntType where
>     compare (Pred xt) (Pred yt) = compare xt yt
>     compare (Pred _)  _         = LT
>     compare _         (Pred _)  = GT
>     compare Zero      Zero      = EQ
>     compare (Succ xt) (Succ yt) = compare xt yt
>     compare (Succ _)  _         = GT
>     compare _         (Succ _)  = LT

Integers should be enumerable too...

> instance Enum IntType where
>     toEnum 0 = Zero
>     toEnum n = accume n Zero
>         where accume 0 k = k
>               accume n k
>                   | n < 0 = accume (n+1) (Pred k)
>                   | n > 0 = accume (n-1) (Succ k)
>     fromEnum k = adder 0 k
>         where adder n Zero = n
>               adder n (Pred kt) = adder (n-1) kt
>               adder n (Succ kt) = adder (n+1) kt
>     succ (Pred kt) = kt
>     succ k         = Succ k
>     pred (Succ kt) = kt
>     pred k         = Pred k
>     enumFrom x = iterate succ x
>     enumFromTo x y
>         | x == y    = [x]
>         | otherwise = x : enumFromTo (succ x) y

And we need to perform arithmetic.

> instance Num IntType where
>     x         + Zero = x
>     Zero      + y    = y
>     (Pred xt) + y    = xt + pred y
>     (Succ xt) + y    = xt + succ y
>     x - Zero      = x
>     x - (Pred yt) = succ x - yt
>     x - (Succ yt) = pred x - yt
>     Zero      * y = Zero
>     (Pred xt) * y = xt * y - y
>     (Succ xt) * y = xt * y + y
>     abs x@(Pred _) = Zero - x
>     abs x          = x
>     signum (Pred _) = Pred Zero
>     signum (Succ _) = Succ Zero
>     signum Zero     = Zero
>     fromInteger n = accume n Zero
>         where accume 0 k = k
>               accume n k
>                   | n < 0 = accume (n+1) (Pred k)
>                   | n > 0 = accume (n-1) (Succ k)

They must be an instance of Real in order to be an instance of
Integral.

> instance Real IntType where
>     toRational = toRational . toInteger

Our integers are indeed integral :-)

> instance Integral IntType where
>     quotRem Zero _ = (Zero, Zero)
>     quotRem n d = appsig (signum n * signum d) (foo n d)
>         where appsig k (q,r) = (k*q, k*r)
>               foo n d = countup (abs n) (abs d) Zero
>               countup n@(Pred _) d c
>                   = (pred c, n + d)
>               countup n d c
>                   = countup (n - d) d (succ c)
>     toInteger k = acc 0 k
>         where acc n Zero      = n
>               acc n (Pred kt) = acc (n-1) kt
>               acc n (Succ kt) = acc (n+1) kt

Finally, implement an instance of the Show class, for converting
integers to strings.

> instance Show IntType where
>     show n | n == Zero = "0"
>            | n < Zero = '-' : show (negate n)
>            | n > Zero = onshow n ""
>         where ten = Succ $ Succ $ Succ $ Succ $ Succ five
>               five = Succ $ Succ $ Succ $ Succ $ Succ Zero
>               onshow Zero s = s
>               onshow k s = let (q,r) = quotRem k ten
>                            in onshow q (charate r : s)
>               charate Zero = '0'
>               charate (Succ kt) = succ (charate kt)

Might as well show off that it works...

> greaterroot :: IntType -> IntType
> greaterroot n = head [k | k <- [Zero ..], k * k >= n]

Finally, our main function!  Make a list of perfect squares from Zero
to (Succ (Succ (Succ ... (one hundred times) ... (Succ Zero) ... ))),
then find their square roots, and output them!

> main :: IO ()
> main = 
>   mapM_ (putStrLn . show) $ map greaterroot squares
>     where squares = map (\x -> x*x) [one .. ten]
>           one = Succ Zero
>           two = one + one
>           five = two + two + one
>           ten = two * five
Team Colleague
Reputation Points: 1135
Solved Threads: 172
Super Senior Demiposter
Rashakil Fol is offline Offline
2,478 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in IT Professionals' Lounge Forum Timeline: Merrrrryyyyy Christmas!
Next Thread in IT Professionals' Lounge Forum Timeline: creating skins





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC