WeixiangYan/CodeTransOcean huggingface dataset:
Task: Here document





Java: package rosettacode.heredoc;
public class MainApp {
	public static void main(String[] args) {
		String hereDoc = """
				This is a multiline string.
				It includes all of this text,
				but on separate lines in the code.
				 """;
		System.out.println(hereDoc);
	}
}







Ada: with Ada.Containers.Indefinite_Vectors, Ada.Text_IO;

procedure Here_Doc is

   package String_Vec is new Ada.Containers.Indefinite_Vectors
     (Index_Type   => Positive,
      Element_Type => String);

   use type String_Vec.Vector;

   Document: String_Vec.Vector := String_Vec.Empty_Vector
    & "This is a vector of strings with the following properties:"
    & "  - indention is preserved, and"
    & "  - a quotation mark '""' must be ""escaped"" by a double-quote '""""'.";
begin
   Document := Document & "Have fun!";
   for I in Document.First_Index .. Document.Last_Index loop
      Ada.Text_IO.Put_Line(Document.Element(I));
   end loop;
end Here_Doc;
Task: Special characters





Java: & | ^ ~ 
>> << 
>>> 
+ - * / = % 







Ada: with Ada.Text_IO;  use Ada.Text_IO;

procedure Test is
begin
   Put ("Quote """ & ''' & """" & Character'Val (10));
end Test;
Task: Assertions in design by contract





Java: (...)
int feedForward(double[] inputs) {
    assert inputs.length == weights.length : "weights and input length mismatch";

    double sum = 0;
    for (int i = 0; i < weights.length; i++) {
        sum += inputs[i] * weights[i];
    }
    return activate(sum);
}
(...)







Ada: type Nums_Array is array (Integer range <>) of Integer;

procedure Sort(Arr : in out Nums_Array) with
    Pre => Arr'Length > 1,
    Post => (for all I in Arr'First .. Arr'Last -1 => Arr(I) <= Arr(I + 1));
Task: History variables





Java: public class HistoryVariable
{
    private Object value;

    public HistoryVariable(Object v)
    {
        value = v;
    }

    public void update(Object v)
    {
        value = v;
    }

    public Object undo()
    {
        return value;
    }

    @Override
    public String toString()
    {
        return value.toString();
    }

    public void dispose()
    {
    }
}







Ada: private with Ada.Containers.Indefinite_Vectors;
generic
   type Item_Type (<>) is private;
package History_Variables is

   type Variable is tagged limited private;

   
   procedure Set(V: in out Variable; Item: Item_Type);
   function Get(V: Variable) return Item_Type;

   
   function Defined(V: Variable) return Natural;

   
   function Peek(V: Variable; Generation: Natural := 1) return Item_Type;
   
   

   
   procedure Undo(V: in out Variable);
   
   
   

private
   package Vectors is new Ada.Containers.Indefinite_Vectors
     (Index_Type   => Positive,
      Element_Type => Item_Type);

   type Variable is tagged limited record
      History: Vectors.Vector;
   end record;
end History_Variables;
Task: Function definition





Java: module MultiplyExample
    {
    static <Value extends Number> Value multiply(Value n1, Value n2)
        {
        return n1 * n2;
        }

    void run()
        {
        (Int i1, Int i2) = (7, 3);
        Int i3 = multiply(i1, i2);
        (Double d1, Double d2) = (2.7182818, 3.1415);
        Double d3 = multiply(d1, d2);
        @Inject Console console;
        console.print($"{i1}*{i2}={i3}, {d1}*{d2}={d3}");
        }
    }







Ada: function Multiply (A, B : Float) return Float;
Task: Penta-power prime seeds




Go: package main

import (
    "fmt"
    big "github.com/ncw/gmp"
    "rcu"
)

var p, p2, q *big.Int

func isPentaPowerPrimeSeed(n uint64) bool {
    nn := new(big.Int).SetUint64(n)
    p.Set(nn)
    k := new(big.Int).SetUint64(n + 1)
    p2.Add(q, k)
    if !p2.ProbablyPrime(15) {
        return false
    }
    p2.Add(p, k)
    if !p2.ProbablyPrime(15) {
        return false
    }
    for i := 0; i < 3; i++ {
        p.Mul(p, nn)
        p2.Set(p)
        p2.Add(p2, k)
        if !p2.ProbablyPrime(15) {
            return false
        }
    }
    return true
}

func ord(c int) string {
    m := c % 100
    if m > 4 && m <= 20 {
        return "th"
    }
    m %= 10
    switch m {
    case 1:
        return "st"
    case 2:
        return "nd"
    case 3:
        return "rd"
    default:
        return "th"
    }
}

func main() {
    p = new(big.Int)
    p2 = new(big.Int)
    q = big.NewInt(1)
    c := 0
    m := 1
    n := uint64(1)
    fmt.Println("First thirty penta-power prime seeds:")
    for ; c < 30; n += 2 {
        if isPentaPowerPrimeSeed(n) {
            fmt.Printf("%9s ", rcu.Commatize(int(n)))
            c++
            if c%10 == 0 {
                fmt.Println()
            }
        }
    }

    n = 1
    c = 0
    fmt.Println("\nFirst penta-power prime seed greater than:")
    for {
        if isPentaPowerPrimeSeed(n) {
            c++
            if n > 1000000*uint64(m) {
                ns := rcu.Commatize(int(n))
                fmt.Printf(" %2d million is the %d%s: %10s\n", m, c, ord(c), ns)
                m++
                if m == 11 {
                    break
                }
            }
        }
        n += 2
    }
}
Task: Quad-power prime seeds




Go: package main

import (
    "fmt"
    big "github.com/ncw/gmp"
    "rcu"
)

var p, p2 *big.Int

func isQuadPowerPrimeSeed(n uint64) bool {
    nn := new(big.Int).SetUint64(n)
    p.Set(nn)
    k := new(big.Int).SetUint64(n + 1)
    p2.Add(p, k)
    if !p2.ProbablyPrime(15) {
        return false
    }
    for i := 0; i < 3; i++ {
        p.Mul(p, nn)
        p2.Set(p)
        p2.Add(p2, k)
        if !p2.ProbablyPrime(15) {
            return false
        }
    }
    return true
}

func ord(c int) string {
    m := c % 100
    if m > 4 && m <= 20 {
        return "th"
    }
    m %= 10
    switch m {
    case 1:
        return "st"
    case 2:
        return "nd"
    case 3:
        return "rd"
    default:
        return "th"
    }
}

func main() {
    p = new(big.Int)
    p2 = new(big.Int)
    c := 0
    m := 1
    n := uint64(1)
    fmt.Println("First fifty quad-power prime seeds:")
    for ; c < 50; n++ {
        if isQuadPowerPrimeSeed(n) {
            fmt.Printf("%7s  ", rcu.Commatize(int(n)))
            c++
            if c%10 == 0 {
                fmt.Println()
            }
        }
    }

    fmt.Println("\nFirst quad-power prime seed greater than:")
    for {
        if isQuadPowerPrimeSeed(n) {
            c++
            if n > 1000000*uint64(m) {
                ns := rcu.Commatize(int(n))
                fmt.Printf(" %2d million is the %d%s: %10s\n", m, c, ord(c), ns)
                m++
                if m == 11 {
                    break
                }
            }
        }
        n++
    }
}
Task: Strange numbers





Java: import java.util.LinkedList;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class StrangeNumbers {
    private static List<Integer> digits(int n) {
        var result = new LinkedList<Integer>();
        while (n > 0) {
            var rem = n % 10;
            result.addFirst(rem);
            n /= 10;
        }
        return result;
    }

    private static boolean isStrange(int n) {
        BiPredicate<Integer, Integer> test = (a, b) -> {
            var abs = Math.abs(a - b);
            return abs == 2 || abs == 3 || abs == 5 || abs == 7;
        };

        var xs = digits(n);
        for (int i = 1; i < xs.size(); i++) {
            if (test.negate().test(xs.get(i - 1), xs.get(i))) {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        var xs = IntStream.rangeClosed(100, 500)
            .filter(StrangeNumbers::isStrange)
            .boxed()
            .collect(Collectors.toList());

        System.out.println("Strange numbers in range [100..500]");
        System.out.printf("(Total: %d)\n\n", xs.size());

        for (int i = 0; i < xs.size(); i++) {
            Integer x = xs.get(i);
            System.out.print(x);

            if ((i + 1) % 10 == 0) {
                System.out.println();
            } else {
                System.out.print(' ');
            }
        }
    }
}
Task: Extra primes





Java: public class ExtraPrimes {
    private static int nextPrimeDigitNumber(int n) {
        if (n == 0) {
            return 2;
        }
        switch (n % 10) {
            case 2:
                return n + 1;
            case 3:
            case 5:
                return n + 2;
            default:
                return 2 + nextPrimeDigitNumber(n / 10) * 10;
        }
    }

    private static boolean isPrime(int n) {
        if (n < 2) {
            return false;
        }
        if ((n & 1) == 0) {
            return n == 2;
        }
        if (n % 3 == 0) {
            return n == 3;
        }
        if (n % 5 == 0) {
            return n == 5;
        }

        int[] wheel = new int[]{4, 2, 4, 2, 4, 6, 2, 6};
        int p = 7;
        while (true) {
            for (int w : wheel) {
                if (p * p > n) {
                    return true;
                }
                if (n % p == 0) {
                    return false;
                }
                p += w;
            }
        }
    }

    private static int digitSum(int n) {
        int sum = 0;
        for (; n > 0; n /= 10) {
            sum += n % 10;
        }
        return sum;
    }

    public static void main(String[] args) {
        final int limit = 10_000;
        int p = 0, n = 0;

        System.out.printf("Extra primes under %d:\n", limit);
        while (p < limit) {
            p = nextPrimeDigitNumber(p);
            if (isPrime(p) && isPrime(digitSum(p))) {
                n++;
                System.out.printf("%2d: %d\n", n, p);
            }
        }
        System.out.println();
    }
}
Task: Keyboard input_Obtain a Y or N response


C++: #include <conio.h>
#include <iostream>

using namespace std;

int main()
{
	char ch;
	_cputs( "Yes or no?" );
	do
	{
		ch = _getch();
		ch = toupper( ch );
	} while(ch!='Y'&&ch!='N');

	if(ch=='N')
	{
		cout << "You said no" << endl;
	}
	else
	{
		cout << "You said yes" << endl;
	}
	return 0;
}
Task: Verify distribution uniformity_Naive


C++: #include <map>
#include <iostream>
#include <cmath>

template<typename F>
 bool test_distribution(F f, int calls, double delta)
{
  typedef std::map<int, int> distmap;
  distmap dist;

  for (int i = 0; i < calls; ++i)
    ++dist[f()];

  double mean = 1.0/dist.size();

  bool good = true;

  for (distmap::iterator i = dist.begin(); i != dist.end(); ++i)
  {
    if (std::abs((1.0 * i->second)/calls - mean) > delta)
    {
      std::cout << "Relative frequency " << i->second/(1.0*calls)
                << " of result " << i->first
                << " deviates by more than " << delta
                << " from the expected value " << mean << "\n";
      good = false;
    }
  }

  return good;
}
Task: Colour bars_Display


C++: #ifndef MYWIDGET_H
#define MYWIDGET_H
#include <QWidget>

class QPaintEvent ;

class MyWidget : public QWidget {
public :
   MyWidget( ) ;

protected :
   void paintEvent( QPaintEvent * ) ;
private :
   int width ;
   int height ;
   const int colornumber ;
} ;
#endif
Task: Exceptions


C++: struct MyException
{
  
};
Task: Create an object at a given address


C++: #include <string>
#include <iostream>

int main()
{
    
    char* data = new char[sizeof(std::string)];

    
    std::string* stringPtr = new (data) std::string("ABCD");

    std::cout << *stringPtr << " 0x" << stringPtr << std::endl;

    
    
    stringPtr->~basic_string();
    stringPtr = new (data) std::string("123456");

    std::cout << *stringPtr << " 0x" << stringPtr << std::endl;

    
    stringPtr->~basic_string();
    delete[] data;
}
Task: Jacobsthal numbers








VB: Public n As New Long[2]

Public Sub Main()  
  
  Dim i0 As Integer = 0, i1 As Integer = 1 
  Dim j As Integer, c As Integer, P As Integer = 1, Q As Integer = -2 
  
  Print "First 30 Jacobsthal numbers:" 
  c = 0
  n[i0] = 0
  n[i1] = 1 
  For j = 0 To 29 
    c += 1 
    Print Format$(n[i0], " #########");
    If (c Mod 5) Then 
      Print ""; 
    Else 
      Print Chr(10);
    End If
    n[i0] = P * n[i1] - Q * n[i0] 
    Swap i0, i1
  Next
  
  Print "\n\nFirst 30 Jacobsthal-Lucas numbers: " 
  c = 0
  n[i0] = 2
  n[i1] = 1 
  For j = 0 To 29 
    c += 1 
    Print Format$(n[i0], " #########");
    If (c Mod 5) Then 
      Print ""; 
    Else 
      Print Chr(10);
    End If
    n[i0] = P * n[i1] - Q * n[i0] 
    Swap i0, i1     
  Next
  
  Print "\n\nFirst 20 Jacobsthal oblong numbers: " 
  c = 0
  n[i0] = 0
  n[i1] = 1 
  For j = 0 To 19 
    c += 1 
    Print Format$(n[i0] * n[i1], " ###########");
    If (c Mod 5) Then 
      Print ""; 
    Else 
      Print Chr(10);
    End If
    n[i0] = P * n[i1] - Q * n[i0] 
    Swap i0, i1 
  Next
  
  Print "\n\nFirst 10 Jacobsthal primes: " 
  c = 0
  n[i0] = 0
  n[i1] = 1 
  Do 
    If isPrime(n[i0]) Then 
      c += 1
      Print n[i0] 
    End If
    n[i0] = P * n[i1] - Q * n[i0] 
    Swap i0, i1 
  Loop Until c = 10  
  
End

Public Sub isPrime(ValorEval As Long) As Boolean 
  
  If ValorEval < 2 Then Return False 
  If ValorEval Mod 2 = 0 Then Return ValorEval = 2 
  If ValorEval Mod 3 = 0 Then Return ValorEval = 3 
  Dim d As Long = 5
  While d * d <= ValorEval
    If ValorEval Mod d = 0 Then Return False Else d += 2
  Wend 
  Return True
  
End Function
Task: Append numbers at same position in strings







Python: list1 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
list2 = [10, 11, 12, 13, 14, 15, 16, 17, 18]
list3 = [19, 20, 21, 22, 23, 24, 25, 26, 27]

print([
    ''.join(str(n) for n in z) for z
    in zip(list1, list2, list3)
])
Task: Enforced immutability


C++: #include <iostream>

class MyOtherClass
{
public:
  const int m_x;
  MyOtherClass(const int initX = 0) : m_x(initX) { }

};

int main()
{
  MyOtherClass mocA, mocB(7);

  std::cout << mocA.m_x << std::endl; 
  std::cout << mocB.m_x << std::endl; 

  
  

  return 0;
}









Fortran: real, parameter :: pi = 3.141593
Task: Hofstadter Q sequence


C++: #include <iostream>
 
int main() {
   const int size = 100000;
   int hofstadters[size] = { 1, 1 };  
   for (int i = 3 ; i < size; i++) 
      hofstadters[ i - 1 ] = hofstadters[ i - 1 - hofstadters[ i - 1 - 1 ]] +
                             hofstadters[ i - 1 - hofstadters[ i - 2 - 1 ]];
   std::cout << "The first 10 numbers are: ";
   for (int i = 0; i < 10; i++) 
      std::cout << hofstadters[ i ] << ' ';
   std::cout << std::endl << "The 1000'th term is " << hofstadters[ 999 ] << " !" << std::endl;
   int less_than_preceding = 0;
   for (int i = 0; i < size - 1; i++)
      if (hofstadters[ i + 1 ] < hofstadters[ i ]) 
	     less_than_preceding++;
   std::cout << "In array of size: " << size << ", ";
   std::cout << less_than_preceding << " times a number was preceded by a greater number!" << std::endl;
   return 0;
}









Fortran: Calculate the Hofstadter Q-sequence, using a big array rather than recursion.
      INTEGER ENUFF
      PARAMETER (ENUFF = 100000)
      INTEGER Q(ENUFF)	

      Q(1) = 1		
      Q(2) = 1
      Q(3:) = -123456789
      DO I = 3,ENUFF	
        Q(I) = Q(I - Q(I - 1)) + Q(I - Q(I - 2))	
      END DO
Cast forth results as per the specification.
      WRITE (6,1) Q(1:10)		
    1 FORMAT ("First ten values:",10I2)	
      WRITE (6,*) "Q(1000) =",Q(1000)	
      WRITE (6,3) ENUFF,COUNT(Q(2:ENUFF) < Q(1:ENUFF - 1))	
    3 FORMAT ("Count of those elements 2:",I0,
     1 " which are less than their predecessor: ",I0)	
Curry favour by allowing enquiries.
   10 WRITE (6,11) ENUFF
   11 FORMAT ("Nominate an index (in 1:",I0,"): ",$)	
      READ (5,*,END = 999, ERR = 999) I	
      IF (I.GT.0 .AND. I.LE.ENUFF) THEN	
        WRITE (6,12) I,Q(I)		
   12   FORMAT ("Q(",I0,") = ",I0)	
        GO TO 10			
      END IF		
Closedown.
  999 WRITE (6,*) "Bye."
      END
Task: Count occurrences of a substring


C++: #include <iostream>
#include <string>


int countSubstring(const std::string& str, const std::string& sub)
{
    if (sub.length() == 0) return 0;
    int count = 0;
    for (size_t offset = str.find(sub); offset != std::string::npos;
	 offset = str.find(sub, offset + sub.length()))
    {
        ++count;
    }
    return count;
}

int main()
{
    std::cout << countSubstring("the three truths", "th")    << '\n';
    std::cout << countSubstring("ababababab", "abab")        << '\n';
    std::cout << countSubstring("abaabba*bbaba*bbab", "a*b") << '\n';

    return 0;
}









Fortran: program Example
  implicit none
  integer :: n
  
  n = countsubstring("the three truths", "th")
  write(*,*) n
  n = countsubstring("ababababab", "abab")
  write(*,*) n
  n = countsubstring("abaabba*bbaba*bbab", "a*b")
  write(*,*) n
 
contains

function countsubstring(s1, s2) result(c)
  character(*), intent(in) :: s1, s2
  integer :: c, p, posn
 
  c = 0
  if(len(s2) == 0) return
  p = 1
  do 
    posn = index(s1(p:), s2)
    if(posn == 0) return
    c = c + 1
    p = p + posn + len(s2) - 1
  end do
end function
end program
Task: Carmichael 3 strong pseudoprimes


C++: #include <iomanip>
#include <iostream>

int mod(int n, int d) {
    return (d + n % d) % d;
}

bool is_prime(int n) {
    if (n < 2)
        return false;
    if (n % 2 == 0)
        return n == 2;
    if (n % 3 == 0)
        return n == 3;
    for (int p = 5; p * p <= n; p += 4) {
        if (n % p == 0)
            return false;
        p += 2;
        if (n % p == 0)
            return false;
    }
    return true;
}

void print_carmichael_numbers(int prime1) {
    for (int h3 = 1; h3 < prime1; ++h3) {
        for (int d = 1; d < h3 + prime1; ++d) {
            if (mod((h3 + prime1) * (prime1 - 1), d) != 0
                || mod(-prime1 * prime1, h3) != mod(d, h3))
                continue;
            int prime2 = 1 + (prime1 - 1) * (h3 + prime1)/d;
            if (!is_prime(prime2))
                continue;
            int prime3 = 1 + prime1 * prime2/h3;
            if (!is_prime(prime3))
                continue;
            if (mod(prime2 * prime3, prime1 - 1) != 1)
                continue;
            unsigned int c = prime1 * prime2 * prime3;
            std::cout << std::setw(2) << prime1 << " x "
                << std::setw(4) << prime2 << " x "
                << std::setw(5) << prime3 << " = "
                << std::setw(10) << c << '\n';
        }
    }
}

int main() {
    for (int p = 2; p <= 61; ++p) {
        if (is_prime(p))
            print_carmichael_numbers(p);
    }
    return 0;
}









Fortran:       LOGICAL FUNCTION ISPRIME(N)	
       INTEGER N			
       INTEGER F			
        ISPRIME = .FALSE.		
        DO F = 2,SQRT(DFLOAT(N))	
          IF (MOD(N,F).EQ.0) RETURN	
        END DO				
        ISPRIME = .TRUE.		
      END FUNCTION ISPRIME		

      PROGRAM CHASE
      INTEGER P1,P2,P3	
      INTEGER H3,D	
      INTEGER MSG	
      MSG = 6		
      WRITE (MSG,1)	
    1 FORMAT ("Carmichael numbers that are the product of three primes:"
     & /"    P1  x P2  x P3 =",9X,"C")
      DO P1 = 2,61	
        IF (ISPRIME(P1)) THEN	
          DO H3 = 2,P1 - 1		
            DO D = 1,H3 + P1 - 1		
              IF (MOD((H3 + P1)*(P1 - 1),D).EQ.0	
     &        .AND. (MOD(H3 + MOD(-P1**2,H3),H3) .EQ. MOD(D,H3))) THEN	
                P2 = 1 + (P1 - 1)*(H3 + P1)/D	
                IF (ISPRIME(P2)) THEN		
                  P3 = 1 + P1*P2/H3			
                  IF (ISPRIME(P3)) THEN			
                    IF (MOD(P2*P3,P1 - 1).EQ.1) THEN		
                      WRITE (MSG,2) P1,P2,P3, INT8(P1)*P2*P3		
    2                 FORMAT (3I6,I12)
                    END IF
                  END IF
                END IF
              END IF
            END DO
          END DO
        END IF
      END DO
      END
Task: Benford's law


C++: 

#include <cln/integer.h>
#include <cln/integer_io.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <sstream>
#include <string>
#include <cstdlib>
#include <cmath>
#include <map>
using namespace cln ;

class NextNum {
public :
   NextNum ( cl_I & a , cl_I & b ) : first( a ) , second ( b ) { }
   cl_I operator( )( ) {
      cl_I result = first + second ;
      first = second ;
      second = result ;
      return result ;
   }
private :
   cl_I first ;
   cl_I second ;
} ;

void findFrequencies( const std::vector<cl_I> & fibos , std::map<int , int> &numberfrequencies  ) {
   for ( cl_I bignumber : fibos ) {
      std::ostringstream os ;
      fprintdecimal ( os , bignumber ) ;
      int firstdigit = std::atoi( os.str( ).substr( 0 , 1 ).c_str( )) ;
      auto result = numberfrequencies.insert( std::make_pair( firstdigit , 1 ) ) ;
      if ( ! result.second ) 
	 numberfrequencies[ firstdigit ]++ ;
   }
}

int main( ) {
   std::vector<cl_I> fibonaccis( 1000 ) ;
   fibonaccis[ 0 ] = 0 ;
   fibonaccis[ 1 ] = 1 ;
   cl_I a = 0 ;
   cl_I b = 1 ;
   
   
   std::generate_n( fibonaccis.begin( ) + 2 , 998 , NextNum( a , b ) ) ;
   std::cout << std::endl ;
   std::map<int , int> frequencies ;
   findFrequencies( fibonaccis , frequencies ) ;
   std::cout << "                found                    expected\n" ;
   for ( int i = 1 ; i < 10 ; i++ ) {
      double found = static_cast<double>( frequencies[ i ] ) / 1000 ;
      double expected = std::log10( 1 + 1 / static_cast<double>( i )) ;
      std::cout << i << " :" << std::setw( 16 ) << std::right << found * 100 << " %" ;
      std::cout.precision( 3 ) ;
      std::cout << std::setw( 26 ) << std::right << expected * 100 << " %\n" ;
   }
   return 0 ;
}









Fortran: -*- mode: compilation; default-directory: "/tmp/" -*-
Compilation started at Sat May 18 01:13:00

a=./f && make $a && $a
f95 -Wall -ffree-form f.F -o f
  0.301030010      0.176091254      0.124938756       9.69100147E-02   7.91812614E-02   6.69467747E-02   5.79919666E-02   5.11525236E-02   4.57575098E-02 THE LAW
  0.300999999      0.177000001      0.125000000       9.60000008E-02   7.99999982E-02   6.70000017E-02   5.70000000E-02   5.29999994E-02   4.50000018E-02 LEADING FIBONACCI DIGIT

Compilation finished at Sat May 18 01:13:00
Task: Hickerson series of almost integers


C++: #include <iostream>
#include <iomanip>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <boost/math/constants/constants.hpp>
typedef boost::multiprecision::cpp_dec_float_50 decfloat;

int main()
{
    const decfloat ln_two = boost::math::constants::ln_two<decfloat>();
    decfloat numerator = 1, denominator = ln_two;
    
    for(int n = 1; n <= 17; n++) {
        decfloat h = (numerator *= n) / (denominator *= ln_two) / 2;
        decfloat tenths_dig = floor((h - floor(h)) * 10);
        std::cout << "h(" << std::setw(2) << n << ") = " << std::setw(25) << std::fixed << h << 
            (tenths_dig == 0 || tenths_dig == 9 ? " is " : " is NOT ") << "an almost-integer.\n";
    }
}









Fortran: program hickerson
    implicit none
    integer, parameter :: q = selected_real_kind(30)
    integer, parameter :: l = selected_int_kind(15)
    real(q) :: s, l2
    integer :: i, n, k

    l2 = log(2.0_q)
    do n = 1, 17
        s = 0.5_q / l2
        do i = 1, n
            s = (s * i) / l2
        end do

        k = floor((s - floor(s, l)) * 10)
        if (k == 0 .or. k == 9) then
            print 1, n, s, ""
        else
            print 1, n, s, " NOT"
        endif
    end do
  1 format('h(',I2,') = ',F23.3,' is',A,' an almost-integer')
end program
Task: Case-sensitivity of identifiers


C++: #include <iostream>
#include <string>
using namespace std;

int main() {
    string dog = "Benjamin", Dog = "Samba", DOG = "Bernie";
    
    cout << "The three dogs are named " << dog << ", " << Dog << ", and " << DOG << endl;
}









Fortran: program Example
  implicit none

  character(8) :: dog, Dog, DOG

  dog = "Benjamin"
  Dog = "Samba"
  DOG = "Bernie"

  if (dog == DOG) then
    write(*,*) "There is just one dog named ", dog
  else
    write(*,*) "The three dogs are named ", dog, Dog, " and ", DOG
  end if

end program Example