WeixiangYan/CodeTransOcean huggingface dataset:
Task: Harshad or Niven series


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

int sumDigits ( int number ) {
   int sum = 0 ;
   while ( number != 0 ) {
      sum += number % 10 ;
      number /= 10 ;
   }
   return sum ;
}

bool isHarshad ( int number ) {
   return number % ( sumDigits ( number ) ) == 0 ;
}

int main( ) {
   std::vector<int> harshads ;
   int i = 0 ;
   while ( harshads.size( ) != 20 ) {
      i++ ;
      if ( isHarshad ( i ) ) 
	 harshads.push_back( i ) ;
   }
   std::cout << "The first 20 Harshad numbers:\n" ;
   for ( int number : harshads )
      std::cout << number << " " ;
   std::cout << std::endl ;
   int start = 1001 ;
   while ( ! ( isHarshad ( start ) ) ) 
      start++ ;
   std::cout << "The smallest Harshad number greater than 1000 : " << start << '\n' ;
   return 0 ;
}








COBOL: identification division.
program-id. harshad.
environment division.
data division.
working-storage section.

01  harshads.
    03  harshad pic 9(5)    occurs 20 times indexed by niven.


01  first-num   pic 9(5).
01  second-num  pic 9(5).


01  i   pic 9(5).


01  div pic 9(5).
01  mod pic 9(5).
01  tot pic 9(5).


01  harshad-div pic 9(5).
01  harshad-mod pic 9(5).
    88  evenly-divisible    value 0.
01  harshad-disp    pic zzzz9.
01  harshad-result  pic 9(5).


01  pass        pic 9.
    88  first-pass  value 1.
    88  second-pass value 2.
    
procedure division.
10-main section.
    move 1 to pass.
    set niven to 1.
    perform 20-calculate-harshad with test before varying first-num from 1 by 1 until niven = 21.
    
    move 2 to pass.
    move first-num to second-num.
    perform 20-calculate-harshad with test after varying first-num from second-num by 1 until harshad-result > 1000.
    
    perform with test after varying i from 1 by 1 until i = 20
        move harshad(i) to harshad-disp
        display function trim(harshad-disp) space with no advancing
    end-perform.
    
    move harshad-result to harshad-disp.
    display "... " function trim(harshad-disp).
    stop run.
    
20-calculate-harshad.
    move first-num to div.
    move zero to harshad-result.
    perform 100-calculate-sum-of-digits.
    divide first-num by tot giving harshad-div remainder harshad-mod.
    if evenly-divisible
        if first-pass
            move first-num to harshad(niven)
            set niven up by 1
        else
            move first-num to harshad-result
        end-if
    end-if.
    exit paragraph.
        
100-calculate-sum-of-digits.
    move zero to tot.
    perform with test after until div = 0
        divide div by 10 giving div remainder mod
        add mod to tot
    end-perform.
    
    
    
    
    exit paragraph.
Task: Harshad or Niven series





Java: public class Harshad{
    private static long sumDigits(long n){
        long sum = 0;
        for(char digit:Long.toString(n).toCharArray()){
            sum += Character.digit(digit, 10);
        }
        return sum;
    }
    public static void main(String[] args){
        for(int count = 0, i = 1; count < 20;i++){
            if(i % sumDigits(i) == 0){
                System.out.println(i);
                count++;
            }
        }
        System.out.println();
        for(int i = 1001; ; i++){
            if(i % sumDigits(i) == 0){
                System.out.println(i);
                break;
            }
        }
    }
}





COBOL: identification division.
program-id. harshad.
environment division.
data division.
working-storage section.

01  harshads.
    03  harshad pic 9(5)    occurs 20 times indexed by niven.


01  first-num   pic 9(5).
01  second-num  pic 9(5).


01  i   pic 9(5).


01  div pic 9(5).
01  mod pic 9(5).
01  tot pic 9(5).


01  harshad-div pic 9(5).
01  harshad-mod pic 9(5).
    88  evenly-divisible    value 0.
01  harshad-disp    pic zzzz9.
01  harshad-result  pic 9(5).


01  pass        pic 9.
    88  first-pass  value 1.
    88  second-pass value 2.
    
procedure division.
10-main section.
    move 1 to pass.
    set niven to 1.
    perform 20-calculate-harshad with test before varying first-num from 1 by 1 until niven = 21.
    
    move 2 to pass.
    move first-num to second-num.
    perform 20-calculate-harshad with test after varying first-num from second-num by 1 until harshad-result > 1000.
    
    perform with test after varying i from 1 by 1 until i = 20
        move harshad(i) to harshad-disp
        display function trim(harshad-disp) space with no advancing
    end-perform.
    
    move harshad-result to harshad-disp.
    display "... " function trim(harshad-disp).
    stop run.
    
20-calculate-harshad.
    move first-num to div.
    move zero to harshad-result.
    perform 100-calculate-sum-of-digits.
    divide first-num by tot giving harshad-div remainder harshad-mod.
    if evenly-divisible
        if first-pass
            move first-num to harshad(niven)
            set niven up by 1
        else
            move first-num to harshad-result
        end-if
    end-if.
    exit paragraph.
        
100-calculate-sum-of-digits.
    move zero to tot.
    perform with test after until div = 0
        divide div by 10 giving div remainder mod
        add mod to tot
    end-perform.
    
    
    
    
    exit paragraph.
Task: Harshad or Niven series







Python: >>> import itertools
>>> def harshad():
	for n in itertools.count(1):
		if n % sum(int(ch) for ch in str(n)) == 0:
			yield n

		
>>> list(itertools.islice(harshad(), 0, 20))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 18, 20, 21, 24, 27, 30, 36, 40, 42]
>>> for n in harshad():
	if n > 1000:
		print(n)
		break

	
1002
>>>



COBOL: identification division.
program-id. harshad.
environment division.
data division.
working-storage section.

01  harshads.
    03  harshad pic 9(5)    occurs 20 times indexed by niven.


01  first-num   pic 9(5).
01  second-num  pic 9(5).


01  i   pic 9(5).


01  div pic 9(5).
01  mod pic 9(5).
01  tot pic 9(5).


01  harshad-div pic 9(5).
01  harshad-mod pic 9(5).
    88  evenly-divisible    value 0.
01  harshad-disp    pic zzzz9.
01  harshad-result  pic 9(5).


01  pass        pic 9.
    88  first-pass  value 1.
    88  second-pass value 2.
    
procedure division.
10-main section.
    move 1 to pass.
    set niven to 1.
    perform 20-calculate-harshad with test before varying first-num from 1 by 1 until niven = 21.
    
    move 2 to pass.
    move first-num to second-num.
    perform 20-calculate-harshad with test after varying first-num from second-num by 1 until harshad-result > 1000.
    
    perform with test after varying i from 1 by 1 until i = 20
        move harshad(i) to harshad-disp
        display function trim(harshad-disp) space with no advancing
    end-perform.
    
    move harshad-result to harshad-disp.
    display "... " function trim(harshad-disp).
    stop run.
    
20-calculate-harshad.
    move first-num to div.
    move zero to harshad-result.
    perform 100-calculate-sum-of-digits.
    divide first-num by tot giving harshad-div remainder harshad-mod.
    if evenly-divisible
        if first-pass
            move first-num to harshad(niven)
            set niven up by 1
        else
            move first-num to harshad-result
        end-if
    end-if.
    exit paragraph.
        
100-calculate-sum-of-digits.
    move zero to tot.
    perform with test after until div = 0
        divide div by 10 giving div remainder mod
        add mod to tot
    end-perform.
    
    
    
    
    exit paragraph.
Task: Harshad or Niven series








VB: Option Explicit

Sub Main()
Dim i As Long, out As String, Count As Integer
   Do
      i = i + 1
      If IsHarshad(i) Then out = out & i & ", ": Count = Count + 1
   Loop While Count < 20
   Debug.Print "First twenty Harshad numbers are : " & vbCrLf & out & "..."
   
   i = 1000
   Do
      i = i + 1
   Loop While Not IsHarshad(i)
   Debug.Print "The first harshad number after 1000 is : " & i
End Sub

Function IsHarshad(sNumber As Long) As Boolean
Dim Summ As Long, i As Long, temp
   temp = Split(StrConv(sNumber, vbUnicode), Chr(0))
   For i = LBound(temp) To UBound(temp) - 1
      Summ = Summ + temp(i)
   Next i
   IsHarshad = sNumber Mod Summ = 0
End Function


COBOL: identification division.
program-id. harshad.
environment division.
data division.
working-storage section.

01  harshads.
    03  harshad pic 9(5)    occurs 20 times indexed by niven.


01  first-num   pic 9(5).
01  second-num  pic 9(5).


01  i   pic 9(5).


01  div pic 9(5).
01  mod pic 9(5).
01  tot pic 9(5).


01  harshad-div pic 9(5).
01  harshad-mod pic 9(5).
    88  evenly-divisible    value 0.
01  harshad-disp    pic zzzz9.
01  harshad-result  pic 9(5).


01  pass        pic 9.
    88  first-pass  value 1.
    88  second-pass value 2.
    
procedure division.
10-main section.
    move 1 to pass.
    set niven to 1.
    perform 20-calculate-harshad with test before varying first-num from 1 by 1 until niven = 21.
    
    move 2 to pass.
    move first-num to second-num.
    perform 20-calculate-harshad with test after varying first-num from second-num by 1 until harshad-result > 1000.
    
    perform with test after varying i from 1 by 1 until i = 20
        move harshad(i) to harshad-disp
        display function trim(harshad-disp) space with no advancing
    end-perform.
    
    move harshad-result to harshad-disp.
    display "... " function trim(harshad-disp).
    stop run.
    
20-calculate-harshad.
    move first-num to div.
    move zero to harshad-result.
    perform 100-calculate-sum-of-digits.
    divide first-num by tot giving harshad-div remainder harshad-mod.
    if evenly-divisible
        if first-pass
            move first-num to harshad(niven)
            set niven up by 1
        else
            move first-num to harshad-result
        end-if
    end-if.
    exit paragraph.
        
100-calculate-sum-of-digits.
    move zero to tot.
    perform with test after until div = 0
        divide div by 10 giving div remainder mod
        add mod to tot
    end-perform.
    
    
    
    
    exit paragraph.
Task: Harshad or Niven series




Go: package main

import "fmt"

type is func() int

func newSum() is {
    var ms is
    ms = func() int {
        ms = newSum()
        return ms()
    }
    var msd, d int
    return func() int {
        if d < 9 {
            d++
        } else {
            d = 0
            msd = ms()
        }
        return msd + d
    }
}

func newHarshard() is {
    i := 0
    sum := newSum()
    return func() int {
        for i++; i%sum() != 0; i++ {
        }
        return i
    }
}

func main() {
    h := newHarshard()
    fmt.Print(h())
    for i := 1; i < 20; i++ {
        fmt.Print(" ", h())
    }
    fmt.Println()
    h = newHarshard()
    n := h()
    for ; n <= 1000; n = h() {
    }
    fmt.Println(n)
}






COBOL: identification division.
program-id. harshad.
environment division.
data division.
working-storage section.

01  harshads.
    03  harshad pic 9(5)    occurs 20 times indexed by niven.


01  first-num   pic 9(5).
01  second-num  pic 9(5).


01  i   pic 9(5).


01  div pic 9(5).
01  mod pic 9(5).
01  tot pic 9(5).


01  harshad-div pic 9(5).
01  harshad-mod pic 9(5).
    88  evenly-divisible    value 0.
01  harshad-disp    pic zzzz9.
01  harshad-result  pic 9(5).


01  pass        pic 9.
    88  first-pass  value 1.
    88  second-pass value 2.
    
procedure division.
10-main section.
    move 1 to pass.
    set niven to 1.
    perform 20-calculate-harshad with test before varying first-num from 1 by 1 until niven = 21.
    
    move 2 to pass.
    move first-num to second-num.
    perform 20-calculate-harshad with test after varying first-num from second-num by 1 until harshad-result > 1000.
    
    perform with test after varying i from 1 by 1 until i = 20
        move harshad(i) to harshad-disp
        display function trim(harshad-disp) space with no advancing
    end-perform.
    
    move harshad-result to harshad-disp.
    display "... " function trim(harshad-disp).
    stop run.
    
20-calculate-harshad.
    move first-num to div.
    move zero to harshad-result.
    perform 100-calculate-sum-of-digits.
    divide first-num by tot giving harshad-div remainder harshad-mod.
    if evenly-divisible
        if first-pass
            move first-num to harshad(niven)
            set niven up by 1
        else
            move first-num to harshad-result
        end-if
    end-if.
    exit paragraph.
        
100-calculate-sum-of-digits.
    move zero to tot.
    perform with test after until div = 0
        divide div by 10 giving div remainder mod
        add mod to tot
    end-perform.
    
    
    
    
    exit paragraph.
Task: Arithmetic_Rational


C++: #include <iostream>
#include "math.h"
#include "boost/rational.hpp"

typedef  boost::rational<int> frac;

bool is_perfect(int c)
{
    frac sum(1, c);
    for (int f = 2;f < sqrt(static_cast<float>(c)); ++f){

        if (c % f == 0) sum += frac(1,f) + frac(1, c/f);
    }
    if (sum.denominator() == 1){
 	return (sum == 1);
    }
    return false;
}

int main()
{
    for (int candidate = 2; candidate < 0x80000; ++candidate){
        if (is_perfect(candidate)) 
	        std::cout << candidate << " is perfect" << std::endl;
    }
    return 0;
}










Ada: generic
   type Number is range <>;
package Generic_Rational is
   type Rational is private;
   
   function "abs"   (A : Rational) return Rational;
   function "+"     (A : Rational) return Rational;
   function "-"     (A : Rational) return Rational;
   function Inverse (A : Rational) return Rational;
   
   function "+" (A : Rational; B : Rational) return Rational;
   function "+" (A : Rational; B : Number  ) return Rational;
   function "+" (A : Number;   B : Rational) return Rational;

   function "-" (A : Rational; B : Rational) return Rational;
   function "-" (A : Rational; B : Number  ) return Rational;
   function "-" (A : Number;   B : Rational) return Rational;

   function "*" (A : Rational; B : Rational) return Rational;
   function "*" (A : Rational; B : Number  ) return Rational;
   function "*" (A : Number;   B : Rational) return Rational;

   function "/" (A : Rational; B : Rational) return Rational;
   function "/" (A : Rational; B : Number  ) return Rational;
   function "/" (A : Number;   B : Rational) return Rational;
   function "/" (A : Number;   B : Number)   return Rational;
   
   function ">"  (A : Rational; B : Rational) return Boolean;
   function ">"  (A : Number;   B : Rational) return Boolean;
   function ">"  (A : Rational; B : Number)   return Boolean;

   function "<"  (A : Rational; B : Rational) return Boolean;
   function "<"  (A : Number;   B : Rational) return Boolean;
   function "<"  (A : Rational; B : Number)   return Boolean;

   function ">=" (A : Rational; B : Rational) return Boolean;
   function ">=" (A : Number;   B : Rational) return Boolean;
   function ">=" (A : Rational; B : Number)   return Boolean;

   function "<=" (A : Rational; B : Rational) return Boolean;
   function "<=" (A : Number;   B : Rational) return Boolean;
   function "<=" (A : Rational; B : Number)   return Boolean;

   function "="  (A : Number;   B : Rational) return Boolean;
   function "="  (A : Rational; B : Number)   return Boolean;

   function Numerator   (A : Rational) return Number;
   function Denominator (A : Rational) return Number;
             
   Zero : constant Rational;
   One  : constant Rational;
private
   type Rational is record
      Numerator   : Number;
      Denominator : Number;
   end record;

   Zero : constant Rational := (0, 1);
   One  : constant Rational := (1, 1);
end Generic_Rational;
Task: Arithmetic_Rational







Python: from fractions import Fraction

for candidate in range(2, 2**19):
  sum = Fraction(1, candidate)
  for factor in range(2, int(candidate**0.5)+1):
    if candidate % factor == 0:
      sum += Fraction(1, factor) + Fraction(1, candidate // factor)
  if sum.denominator == 1:
    print("Sum of recipr. factors of %d = %d exactly %s" %
           (candidate, int(sum), "perfect!" if sum == 1 else ""))





Ada: generic
   type Number is range <>;
package Generic_Rational is
   type Rational is private;
   
   function "abs"   (A : Rational) return Rational;
   function "+"     (A : Rational) return Rational;
   function "-"     (A : Rational) return Rational;
   function Inverse (A : Rational) return Rational;
   
   function "+" (A : Rational; B : Rational) return Rational;
   function "+" (A : Rational; B : Number  ) return Rational;
   function "+" (A : Number;   B : Rational) return Rational;

   function "-" (A : Rational; B : Rational) return Rational;
   function "-" (A : Rational; B : Number  ) return Rational;
   function "-" (A : Number;   B : Rational) return Rational;

   function "*" (A : Rational; B : Rational) return Rational;
   function "*" (A : Rational; B : Number  ) return Rational;
   function "*" (A : Number;   B : Rational) return Rational;

   function "/" (A : Rational; B : Rational) return Rational;
   function "/" (A : Rational; B : Number  ) return Rational;
   function "/" (A : Number;   B : Rational) return Rational;
   function "/" (A : Number;   B : Number)   return Rational;
   
   function ">"  (A : Rational; B : Rational) return Boolean;
   function ">"  (A : Number;   B : Rational) return Boolean;
   function ">"  (A : Rational; B : Number)   return Boolean;

   function "<"  (A : Rational; B : Rational) return Boolean;
   function "<"  (A : Number;   B : Rational) return Boolean;
   function "<"  (A : Rational; B : Number)   return Boolean;

   function ">=" (A : Rational; B : Rational) return Boolean;
   function ">=" (A : Number;   B : Rational) return Boolean;
   function ">=" (A : Rational; B : Number)   return Boolean;

   function "<=" (A : Rational; B : Rational) return Boolean;
   function "<=" (A : Number;   B : Rational) return Boolean;
   function "<=" (A : Rational; B : Number)   return Boolean;

   function "="  (A : Number;   B : Rational) return Boolean;
   function "="  (A : Rational; B : Number)   return Boolean;

   function Numerator   (A : Rational) return Number;
   function Denominator (A : Rational) return Number;
             
   Zero : constant Rational;
   One  : constant Rational;
private
   type Rational is record
      Numerator   : Number;
      Denominator : Number;
   end record;

   Zero : constant Rational := (0, 1);
   One  : constant Rational := (1, 1);
end Generic_Rational;
Task: Arithmetic_Rational

C: #include <stdio.h>
#include <stdlib.h>
#define FMT "%lld"
typedef long long int fr_int_t;
typedef struct { fr_int_t num, den; } frac;

fr_int_t gcd(fr_int_t m, fr_int_t n)
{
	fr_int_t t;
	while (n) { t = n; n = m % n; m = t; }
	return m;
}

frac frac_new(fr_int_t num, fr_int_t den)
{
	frac a;
	if (!den) {
		printf("divide by zero: "FMT"/"FMT"\n", num, den);
		abort();
	}

	int g = gcd(num, den);

	if (g)	{ num /= g; den /= g; }
	else	{ num = 0; den = 1;   }

	if (den < 0) {
		den = -den;
		num = -num;
	}
	a.num = num; a.den = den;
	return a;
}

#define BINOP(op, n, d) frac frac_##op(frac a, frac b) { return frac_new(n,d); }
BINOP(add, a.num * b.den + b.num * a.den, a.den * b.den);
BINOP(sub, a.num * b.den - b.num + a.den, a.den * b.den);
BINOP(mul, a.num * b.num, a.den * b.den);
BINOP(div, a.num * b.den, a.den * b.num);

int frac_cmp(frac a, frac b) {
	int l = a.num * b.den, r = a.den * b.num;
	return l < r ? -1 : l > r;
}
#define frac_cmp_int(a, b) frac_cmp(a, frac_new(b, 1))
int frtoi(frac a) { return a.den / a.num; }
double frtod(frac a) { return (double)a.den / a.num; }

int main()
{
	int n, k;
	frac sum, kf;

	for (n = 2; n < 1<<19; n++) {
		sum = frac_new(1, n);

		for (k = 2; k * k < n; k++) {
			if (n % k) continue;
			kf = frac_new(1, k);
			sum = frac_add(sum, kf);

			kf = frac_new(1, n / k);
			sum = frac_add(sum, kf);
		}
		if (frac_cmp_int(sum, 1) == 0) printf("%d\n", n);
	}

	return 0;
}
Task: Arithmetic_Rational


C++: #include <iostream>
#include "math.h"
#include "boost/rational.hpp"

typedef  boost::rational<int> frac;

bool is_perfect(int c)
{
    frac sum(1, c);
    for (int f = 2;f < sqrt(static_cast<float>(c)); ++f){

        if (c % f == 0) sum += frac(1,f) + frac(1, c/f);
    }
    if (sum.denominator() == 1){
 	return (sum == 1);
    }
    return false;
}

int main()
{
    for (int candidate = 2; candidate < 0x80000; ++candidate){
        if (is_perfect(candidate)) 
	        std::cout << candidate << " is perfect" << std::endl;
    }
    return 0;
}
Task: Arithmetic_Rational





Java: public class BigRationalFindPerfectNumbers {
    public static void main(String[] args) {
        int MAX_NUM = 1 << 19;
        System.out.println("Searching for perfect numbers in the range [1, " + (MAX_NUM - 1) + "]");

        BigRational TWO = BigRational.valueOf(2);
        for (int i = 1; i < MAX_NUM; i++) {
            BigRational reciprocalSum = BigRational.ONE;
            if (i > 1)
                reciprocalSum = reciprocalSum.add(BigRational.valueOf(i).reciprocal());
            int maxDivisor = (int) Math.sqrt(i);
            if (maxDivisor >= i)
                maxDivisor--;

            for (int divisor = 2; divisor <= maxDivisor; divisor++) {
                if (i % divisor == 0) {
                    reciprocalSum = reciprocalSum.add(BigRational.valueOf(divisor).reciprocal());
                    int dividend = i / divisor;
                    if (divisor != dividend)
                        reciprocalSum = reciprocalSum.add(BigRational.valueOf(dividend).reciprocal());
                }
            }
            if (reciprocalSum.equals(TWO))
                System.out.println(String.valueOf(i) + " is a perfect number");
        }
    }
}
Task: Arithmetic_Rational







Python: from fractions import Fraction

for candidate in range(2, 2**19):
  sum = Fraction(1, candidate)
  for factor in range(2, int(candidate**0.5)+1):
    if candidate % factor == 0:
      sum += Fraction(1, factor) + Fraction(1, candidate // factor)
  if sum.denominator == 1:
    print("Sum of recipr. factors of %d = %d exactly %s" %
           (candidate, int(sum), "perfect!" if sum == 1 else ""))
Task: Arithmetic_Rational




Go: package main

import (
    "fmt"
    "math"
    "math/big"
)

func main() {
    var recip big.Rat
    max := int64(1 << 19)
    for candidate := int64(2); candidate < max; candidate++ {
        sum := big.NewRat(1, candidate)
        max2 := int64(math.Sqrt(float64(candidate)))
        for factor := int64(2); factor <= max2; factor++ {
            if candidate%factor == 0 {
                sum.Add(sum, recip.SetFrac64(1, factor))
                if f2 := candidate / factor; f2 != factor {
                    sum.Add(sum, recip.SetFrac64(1, f2))
                }
            }
        }
        if sum.Denom().Int64() == 1 {
            perfectstring := ""
            if sum.Num().Int64() == 1 {
                perfectstring = "perfect!"
            }
            fmt.Printf("Sum of recipr. factors of %d = %d exactly %s\n",
                candidate, sum.Num().Int64(), perfectstring)
        }
    }
}