Integer sequence: Difference between revisions

From Rosetta Code
Content added Content deleted
(+Java, alphabetize)
(→‎{{header|C}}: uint32_t, include stdint, use %u instead of %d.)
Line 9: Line 9:
=={{header|C}}==
=={{header|C}}==
<lang c>#include <stdio.h>
<lang c>#include <stdio.h>
#include <stdint.h>


int main()
int main()
{
{
uint_32 i = 0;
uint32_t i = 0;
while(1)
while(1)
{
{
Line 23: Line 24:
Alternatively:
Alternatively:
<lang c>#include <stdio.h>
<lang c>#include <stdio.h>
#include <stdint.h>


int main()
int main()
{
{
for(uint i = 1; 1; i++)
for(uint32_t i = 1; 1; i++)
printf("%d\n", i);
printf("%u\n", i);


return 0;
return 0;

Revision as of 00:44, 13 February 2011

Integer sequence is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

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.

BASIC

<lang basic>5 LET A = 0 10 LET A = A + 1 20 PRINT A 30 GOTO 10</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 cpp>#include <iostream>

int main() {

 uint_32 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>

Haskell

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

Or less imperatively:

<lang haskell>(putStr . unlines . map show) [1..]</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>

Perl

<lang perl>my $i = 0; while(1) {

 print ++$i . "\n";

} </lang>