Integer sequence: Difference between revisions

From Rosetta Code
Content added Content deleted
(added Fantom example)
Line 211: Line 211:
<lang j> count=: (smoutput ] >:)^:_</lang>
<lang j> count=: (smoutput ] >:)^:_</lang>


The above works with both fixed sized integers and floating point numbers (fixed sized integers are automatically promoted to floating point, if they overflow), but also works with extended precision integers (which will not overflow, unless they get so large that they cannot be represented in memory, but that should exceed lifetime of the universe, let alone lifetime of the computer).
This adds support for extended precision and will display integers to ∞ (or at least until the machine is turned off or interrupted).

This adds support for extended precision (in that it converts non-extended precision arguments to extended precision arguments) and will display integers to ∞ (or at least until the machine is turned off or interrupted or crashes).
<lang j> count=: (smoutput ] >:)@x:^:_</lang>
<lang j> count=: (smoutput ] >:)@x:^:_</lang>



Revision as of 01:20, 21 February 2011

Task
Integer sequence
You are encouraged to solve this task according to the task description, using any language you may know.

Create a program that, when run, would display all integers from 1 to ∞ (or any relevant implementation limit), in sequence (i.e. 1, 2, 3, 4, etc) if given enough time.

An example may not be able to reach arbitrarily-large numbers based on implementations limits. For example, if integers are represented as a 32-bit unsigned value with 0 as the smallest representable value, the largest representable value would be 4,294,967,295. Some languages support arbitrarily-large numbers as a built-in feature, while others make use of a module or library.

If appropriate, provide an example which reflect the language implementation's common built-in limits as well as an example which supports arbitrarily large numbers, and describe the nature of such limitations—or lack thereof.

Ada

<lang Ada>with Ada.Text_IO; procedure Integers is

  Value : Integer := 1;

begin

  loop
     Ada.Text_IO.Put_Line (Integer'Image (Value));
     Value := Value + 1;
  end loop;

end Integers;</lang> alternative (iterating through all values of Positive (positive part of Integer) without endless loop): <lang Ada>with Ada.Text_IO; procedure Positives is begin

  for Value in Positive'Range loop
     Ada.Text_IO.Put_Line (Positive'Image (Value));
  end loop;

end Positives;</lang>

BASIC

<lang basic>5 LET A = 0 10 LET A = A + 1 20 PRINT A 30 GOTO 10</lang>

Works with: QBasic

<lang qbasic>A = 0 DO: A = A + 1: PRINT A: LOOP 1</lang>

C

<lang c>#include <stdio.h>

  1. include <stdint.h>

int main() {

 uint32_t i = 0;
 while (1)
 {
   printf("%u\n", ++i);
 }
 return 0;

}</lang>

Alternatively: <lang c>#include <stdio.h>

  1. include <stdint.h>

int main() {

 for (uint32_t i = 1; 1; i++)
   printf("%u\n", i);
 return 0;

}</lang>

C#

<lang csharp>using System; using System.Numerics;

class Program {

   static void Main()
   {
       BigInteger i = 1;
       while (true)
       {
           Console.WriteLine(i++);
       }
   }

}</lang>

C++

<lang cpp>#include <iostream>

  1. include <cstdint>

int main() {

 uint32_t i = 0;
 while(true)
   std::cout << ++i << std::endl;
 return 0;

}</lang>

Common Lisp

<lang lisp>(loop for i from 1 do (print i))</lang>

D

<lang d>import std.stdio, std.bigint;

void main() {

   BigInt i;
   while (true)
       writeln(++i);

}</lang> Alternative: <lang d>import std.stdio, std.traits, std.bigint, std.string ;

void integerSequence(T)() if(is(T == BigInt) || isIntegral!T ) {

   static if(is(T == BigInt)) {
       BigInt now = BigInt(1) ;
       BigInt max = BigInt(0) ;
   } else {
       T now = 1 ;
       T max = T.max ;
   }
   do
       write(now, " ") ;
   while (now++ != max) ;
   writeln("\nDone!") ;

}

void main() {

   string answer ;
   while(answer.length == 0) {
       writeln("Do you have time?") ;
       writeln(" 1. I'm in hurry.") ;
       writeln(" 2. I've some time.") ;
       writeln(" 3. I'm on vacation.") ;
       writeln(" 4. I'm unemployed...") ;
       write(" 0. I'm immortal!\nEnter 0-4 or q for quit > ") ;
       readf("%s\n", &answer) ;
       switch (answer.tolower) {
           case "1": return integerSequence!ubyte ;
           case "2": return integerSequence!short ;
           case "3": return integerSequence!uint ;
           case "4": return integerSequence!long ;
           case "0": return integerSequence!BigInt ;
           case "q": return writeln("Bye bye!") ;
           default:
               writeln("Pardon? try again...") ;
               answer = "" ;
       }
   }

}</lang>

E

<lang e>for i in int > 0 { println(i) }</lang>

F#

<lang fsharp>// lazy sequence of integers starting with i let rec integers i =

 seq { yield i
       yield! integers (i+1) }

Seq.iter (printfn "%d") (integers 1)</lang>

Fantom

<lang fantom> class Main {

 public static Void main()
 {
   i := 1
   while (true)
   {
     echo (i)
     i += 1
   }
 }

} </lang>

Fantom's integers are 64-bit signed, and so the numbers will return to 0 and continue again, if you wait long enough!

Forth

<lang forth>: ints 0 begin 1+ dup u. cr dup 0= until drop ;</lang>

Go

Size of int type is implementation dependent, but I think all implementations use 32 bits currently. After the maximum positive value, it rolls over to maximum negative, without error. <lang go>package main

import "fmt"

func main() {

   for i := 1;; i++ {
       fmt.Println(i)
   }

}</lang> The big number type does not roll over and is limited only by available memory, or practically, by whatever external factor halts cpu execution: human operator, lightning storm, cpu fan failure, heat death of universe, etc. <lang go>package main

import (

   "big"
   "fmt"

)

func main() {

   one := big.NewInt(1)
   for i := big.NewInt(1);; i.Add(i, one) {
       fmt.Println(i)
   }

}</lang>

Haskell

<lang haskell>mapM_ print [1..]</lang>

Or less imperatively:

<lang haskell>(putStr . unlines . map show) [1..]</lang>

J

The following will count forever but once the 32-bit (or 64-bit depending on J engine version) limit is reached, the results will be reported as floating point values. <lang j> count=: (smoutput ] >:)^:_</lang>

The above works with both fixed sized integers and floating point numbers (fixed sized integers are automatically promoted to floating point, if they overflow), but also works with extended precision integers (which will not overflow, unless they get so large that they cannot be represented in memory, but that should exceed lifetime of the universe, let alone lifetime of the computer).

This adds support for extended precision (in that it converts non-extended precision arguments to extended precision arguments) and will display integers to ∞ (or at least until the machine is turned off or interrupted or crashes). <lang j> count=: (smoutput ] >:)@x:^:_</lang>

Java

Long limit: <lang java>public class Count{

   public static void main(String[] args){
       for(long i = 1; ;i++) System.out.println(i);
   }

}</lang> "Forever": <lang java>import java.math.BigInteger;

public class Count{

   public static void main(String[] args){
       for(BigInteger i = BigInteger.ONE; ;i = i.add(BigInteger.ONE)) System.out.println(i);
   }

}</lang>

OCaml

with an imperative style: <lang ocaml>let () =

 let i = ref 0 in
 while true do
   print_int !i;
   print_newline ();
   incr i;
 done</lang>

with a functional style: <lang ocaml>let () =

 let rec aux i =
   print_int i;
   print_newline ();
   aux (succ i)
 in
 aux 0</lang>

Perl

<lang perl>my $i = 0; print ++$i, "\n" while 1;</lang>

Perl 6

<lang perl6>.say for 1..*</lang>

PicoLisp

<lang PicoLisp>(for (I 1 T (inc I))

  (printsp I) )</lang>

PureBasic

<lang PureBasic>OpenConsole() Repeat

 a.q+1
 PrintN(Str(a))

ForEver</lang>

Python

<lang python>i=1 while i:

   print(i)
   i += 1</lang>

Or, alternatively: <lang python>from itertools import count

for i in count():

   print(i)</lang>

Pythons integers are of arbitrary large precision and so programs would probably keep going until OS or hardware system failure.

Ruby

<lang ruby> i = 0 puts (i += 1) while true </lang>

Ruby does not limit the size of numbers.

Scheme

<lang scheme> (let loop ((i 1))

 (display i) (newline)
 (loop (+ 1 i)))

</lang>

Scheme does not limit the size of numbers.

Tcl

<lang tcl>package require Tcl 8.5 while true {puts [incr i]}</lang>