WeixiangYan/CodeTransOcean huggingface dataset:
Task: Modular exponentiation






PHP: <?php
$a = '2988348162058574136915891421498819466320163312926952423791023078876139';
$b = '2351399303373464486466122544523690094744975233415544072992656881240319';
$m = '1' . str_repeat('0', 40);
echo bcpowmod($a, $b, $m), "\n";



VB: 
    Imports System.Numerics

    Private Sub Main()
        Dim a, b, m, x As BigInteger
        a = BigInteger.Parse("2988348162058574136915891421498819466320163312926952423791023078876139")
        b = BigInteger.Parse("2351399303373464486466122544523690094744975233415544072992656881240319")
        m = BigInteger.Pow(10, 40)   
        x = ModPowBig(a, b, m)
        Debug.Print("x=" & x.ToString)
    End Sub 

    Function ModPowBig(ByVal base As BigInteger, ByVal exponent As BigInteger, ByVal modulus As BigInteger) As BigInteger
        Dim result As BigInteger
        result = 1
        Do While exponent > 0
            If (exponent Mod 2) = 1 Then
                result = (result * base) Mod modulus
            End If
            exponent = exponent / 2
            base = (base * base) Mod modulus
        Loop
        Return result
    End Function
Task: Multiple distinct objects

C: foo *foos = malloc(n * sizeof(*foos));
for (int i = 0; i < n; i++)
  init_foo(&foos[i]);



C#: using System;
using System.Linq;
using System.Collections.Generic;

List<Foo> foos = Enumerable.Range(1, n).Select(x => new Foo()).ToList();
Task: Multiple distinct objects

C: foo *foos = malloc(n * sizeof(*foos));
for (int i = 0; i < n; i++)
  init_foo(&foos[i]);


C++: 
T* p = new T[n]; 


for(size_t i = 0; i != n; ++i)
   p[i] = make_a_T(); 


delete[] p;
Task: Multiple distinct objects

C: foo *foos = malloc(n * sizeof(*foos));
for (int i = 0; i < n; i++)
  init_foo(&foos[i]);




Go: func nxm(n, m int) [][]int {
    d2 := make([][]int, n)
    for i := range d2 {
        d2[i] = make([]int, m)
    }
    return d2
}
Task: Multiple distinct objects

C: foo *foos = malloc(n * sizeof(*foos));
for (int i = 0; i < n; i++)
  init_foo(&foos[i]);





Java: Foo[] foos = new Foo[n]; 
for (int i = 0; i < foos.length; i++)
    foos[i] = new Foo();


Foo[] foos_WRONG = new Foo[n];
Arrays.fill(foos, new Foo());
Task: Multiple distinct objects

C: foo *foos = malloc(n * sizeof(*foos));
for (int i = 0; i < n; i++)
  init_foo(&foos[i]);







Python: [Foo()] * n
Task: Multiple distinct objects


C++: 
T* p = new T[n]; 


for(size_t i = 0; i != n; ++i)
   p[i] = make_a_T(); 


delete[] p;


C#: using System;
using System.Linq;
using System.Collections.Generic;

List<Foo> foos = Enumerable.Range(1, n).Select(x => new Foo()).ToList();
Task: Multiple distinct objects


C++: 
T* p = new T[n]; 


for(size_t i = 0; i != n; ++i)
   p[i] = make_a_T(); 


delete[] p;



Go: func nxm(n, m int) [][]int {
    d2 := make([][]int, n)
    for i := range d2 {
        d2[i] = make([]int, m)
    }
    return d2
}
Task: Multiple distinct objects


C++: 
T* p = new T[n]; 


for(size_t i = 0; i != n; ++i)
   p[i] = make_a_T(); 


delete[] p;




Java: Foo[] foos = new Foo[n]; 
for (int i = 0; i < foos.length; i++)
    foos[i] = new Foo();


Foo[] foos_WRONG = new Foo[n];
Arrays.fill(foos, new Foo());
Task: Multiple distinct objects


C++: 
T* p = new T[n]; 


for(size_t i = 0; i != n; ++i)
   p[i] = make_a_T(); 


delete[] p;






Python: [Foo()] * n
Task: Multiple distinct objects



C#: using System;
using System.Linq;
using System.Collections.Generic;

List<Foo> foos = Enumerable.Range(1, n).Select(x => new Foo()).ToList();


Go: func nxm(n, m int) [][]int {
    d2 := make([][]int, n)
    for i := range d2 {
        d2[i] = make([]int, m)
    }
    return d2
}
Task: Multiple distinct objects



C#: using System;
using System.Linq;
using System.Collections.Generic;

List<Foo> foos = Enumerable.Range(1, n).Select(x => new Foo()).ToList();





Python: [Foo()] * n
Task: Multiple distinct objects




Go: func nxm(n, m int) [][]int {
    d2 := make([][]int, n)
    for i := range d2 {
        d2[i] = make([]int, m)
    }
    return d2
}


Java: Foo[] foos = new Foo[n]; 
for (int i = 0; i < foos.length; i++)
    foos[i] = new Foo();


Foo[] foos_WRONG = new Foo[n];
Arrays.fill(foos, new Foo());
Task: Multiple distinct objects





Java: Foo[] foos = new Foo[n]; 
for (int i = 0; i < foos.length; i++)
    foos[i] = new Foo();


Foo[] foos_WRONG = new Foo[n];
Arrays.fill(foos, new Foo());  



Python: [Foo()] * n
Task: Multiple distinct objects




Go: func nxm(n, m int) [][]int {
    d2 := make([][]int, n)
    for i := range d2 {
        d2[i] = make([]int, m)
    }
    return d2
}




Python: [Foo()] * n
Task: Multiple distinct objects



C#: using System;
using System.Linq;
using System.Collections.Generic;

List<Foo> foos = Enumerable.Range(1, n).Select(x => new Foo()).ToList();



Java: Foo[] foos = new Foo[n]; 
for (int i = 0; i < foos.length; i++)
    foos[i] = new Foo();


Foo[] foos_WRONG = new Foo[n];
Arrays.fill(foos, new Foo());
Task: Narcissistic decimal number

C: #include <stdio.h>
#include <gmp.h>

#define MAX_LEN 81

mpz_t power[10];
mpz_t dsum[MAX_LEN + 1];
int cnt[10], len;

void check_perm(void)
{
	char s[MAX_LEN + 1];
	int i, c, out[10] = { 0 };

	mpz_get_str(s, 10, dsum[0]);
	for (i = 0; s[i]; i++) {
		c = s[i]-'0';
		if (++out[c] > cnt[c]) return;
	}

	if (i == len)
		gmp_printf(" %Zd", dsum[0]);
}

void narc_(int pos, int d)
{
	if (!pos) {
		check_perm();
		return;
	}

	do {
		mpz_add(dsum[pos-1], dsum[pos], power[d]);
		++cnt[d];
		narc_(pos - 1, d);
		--cnt[d];
	} while (d--);
}

void narc(int n)
{
	int i;
	len = n;
	for (i = 0; i < 10; i++)
		mpz_ui_pow_ui(power[i], i, n);

	mpz_init_set_ui(dsum[n], 0);

	printf("length %d:", n);
	narc_(n, 9);
	putchar('\n');
}

int main(void)
{
	int i;

	for (i = 0; i <= 10; i++)
		mpz_init(power[i]);
	for (i = 1; i <= MAX_LEN; i++) narc(i);

	return 0;
}



C#: using System;

namespace Narcissistic
{
    class Narcissistic
    {
        public bool isNarcissistic(int z)
        {
            if (z < 0) return false;
            string n = z.ToString();
            int t = 0, l = n.Length;
            foreach (char c in n)
                t += Convert.ToInt32(Math.Pow(Convert.ToDouble(c - 48), l));

            return t == z;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Narcissistic n = new Narcissistic();
            int c = 0, x = 0;
            while (c < 25)
            {
                if (n.isNarcissistic(x))
                {
                    if (c % 5 == 0) Console.WriteLine();
                    Console.Write("{0,7} ", x);
                    c++;
                }
                x++;
            }
            Console.WriteLine("\n\nPress any key to continue...");
            Console.ReadKey();
        }
    }
}
Task: Narcissistic decimal number

C: #include <stdio.h>
#include <gmp.h>

#define MAX_LEN 81

mpz_t power[10];
mpz_t dsum[MAX_LEN + 1];
int cnt[10], len;

void check_perm(void)
{
	char s[MAX_LEN + 1];
	int i, c, out[10] = { 0 };

	mpz_get_str(s, 10, dsum[0]);
	for (i = 0; s[i]; i++) {
		c = s[i]-'0';
		if (++out[c] > cnt[c]) return;
	}

	if (i == len)
		gmp_printf(" %Zd", dsum[0]);
}

void narc_(int pos, int d)
{
	if (!pos) {
		check_perm();
		return;
	}

	do {
		mpz_add(dsum[pos-1], dsum[pos], power[d]);
		++cnt[d];
		narc_(pos - 1, d);
		--cnt[d];
	} while (d--);
}

void narc(int n)
{
	int i;
	len = n;
	for (i = 0; i < 10; i++)
		mpz_ui_pow_ui(power[i], i, n);

	mpz_init_set_ui(dsum[n], 0);

	printf("length %d:", n);
	narc_(n, 9);
	putchar('\n');
}

int main(void)
{
	int i;

	for (i = 0; i <= 10; i++)
		mpz_init(power[i]);
	for (i = 1; i <= MAX_LEN; i++) narc(i);

	return 0;
}


C++: #include <iostream>
#include <vector>
using namespace std;
typedef unsigned int uint;

class NarcissisticDecs
{
public:
    void makeList( int mx )
    {
	uint st = 0, tl; int pwr = 0, len;
        while( narc.size() < mx )
	{
	    len = getDigs( st );
	    if( pwr != len )
	    {
		pwr = len;
		fillPower( pwr );
	    }
            tl = 0;
	    for( int i = 1; i < 10; i++ )
		tl += static_cast<uint>( powr[i] * digs[i] );

	    if( tl == st ) narc.push_back( st );
	    st++;
	}
    }

    void display()
    {
	for( vector<uint>::iterator i = narc.begin(); i != narc.end(); i++ )
	    cout << *i << " ";
	cout << "\n\n";
    }

private:
    int getDigs( uint st )
    {
	memset( digs, 0, 10 * sizeof( int ) );
	int r = 0;
	while( st )
	{
	    digs[st % 10]++;
	    st /= 10;
	    r++;
	}
        return r;
    }

    void fillPower( int z )
    {
	for( int i = 1; i < 10; i++ )
	    powr[i] = pow( static_cast<float>( i ), z );
    }

    vector<uint> narc;
    uint powr[10];
    int digs[10];
};

int main( int argc, char* argv[] )
{
    NarcissisticDecs n;
    n.makeList( 25 );
    n.display();
    return system( "pause" );
}
Task: Narcissistic decimal number

C: #include <stdio.h>
#include <gmp.h>

#define MAX_LEN 81

mpz_t power[10];
mpz_t dsum[MAX_LEN + 1];
int cnt[10], len;

void check_perm(void)
{
	char s[MAX_LEN + 1];
	int i, c, out[10] = { 0 };

	mpz_get_str(s, 10, dsum[0]);
	for (i = 0; s[i]; i++) {
		c = s[i]-'0';
		if (++out[c] > cnt[c]) return;
	}

	if (i == len)
		gmp_printf(" %Zd", dsum[0]);
}

void narc_(int pos, int d)
{
	if (!pos) {
		check_perm();
		return;
	}

	do {
		mpz_add(dsum[pos-1], dsum[pos], power[d]);
		++cnt[d];
		narc_(pos - 1, d);
		--cnt[d];
	} while (d--);
}

void narc(int n)
{
	int i;
	len = n;
	for (i = 0; i < 10; i++)
		mpz_ui_pow_ui(power[i], i, n);

	mpz_init_set_ui(dsum[n], 0);

	printf("length %d:", n);
	narc_(n, 9);
	putchar('\n');
}

int main(void)
{
	int i;

	for (i = 0; i <= 10; i++)
		mpz_init(power[i]);
	for (i = 1; i <= MAX_LEN; i++) narc(i);

	return 0;
}




Go: package main

import "fmt"

func narc(n int) []int {
	power := [...]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
	limit := 10
	result := make([]int, 0, n)
	for x := 0; len(result) < n; x++ {
		if x >= limit {
			for i := range power {
				power[i] *= i 
			}
			limit *= 10
		}
		sum := 0
		for xx := x; xx > 0; xx /= 10 {
			sum += power[xx%10]
		}
		if sum == x {
			result = append(result, x)
		}
	}
	return result
}

func main() {
	fmt.Println(narc(25))
}
Task: Narcissistic decimal number

C: #include <stdio.h>
#include <gmp.h>

#define MAX_LEN 81

mpz_t power[10];
mpz_t dsum[MAX_LEN + 1];
int cnt[10], len;

void check_perm(void)
{
	char s[MAX_LEN + 1];
	int i, c, out[10] = { 0 };

	mpz_get_str(s, 10, dsum[0]);
	for (i = 0; s[i]; i++) {
		c = s[i]-'0';
		if (++out[c] > cnt[c]) return;
	}

	if (i == len)
		gmp_printf(" %Zd", dsum[0]);
}

void narc_(int pos, int d)
{
	if (!pos) {
		check_perm();
		return;
	}

	do {
		mpz_add(dsum[pos-1], dsum[pos], power[d]);
		++cnt[d];
		narc_(pos - 1, d);
		--cnt[d];
	} while (d--);
}

void narc(int n)
{
	int i;
	len = n;
	for (i = 0; i < 10; i++)
		mpz_ui_pow_ui(power[i], i, n);

	mpz_init_set_ui(dsum[n], 0);

	printf("length %d:", n);
	narc_(n, 9);
	putchar('\n');
}

int main(void)
{
	int i;

	for (i = 0; i <= 10; i++)
		mpz_init(power[i]);
	for (i = 1; i <= MAX_LEN; i++) narc(i);

	return 0;
}







Python: from __future__ import print_function
from itertools import count, islice

def narcissists():
    for digits in count(0):
        digitpowers = [i**digits for i in range(10)]
        for n in range(int(10**(digits-1)), 10**digits):
            div, digitpsum = n, 0
            while div:
                div, mod = divmod(div, 10)
                digitpsum += digitpowers[mod]
            if n == digitpsum:
                yield n

for i, n in enumerate(islice(narcissists(), 25), 1):
    print(n, end=' ')
    if i % 5 == 0: print() 
print()
Task: Narcissistic decimal number


C++: #include <iostream>
#include <vector>
using namespace std;
typedef unsigned int uint;

class NarcissisticDecs
{
public:
    void makeList( int mx )
    {
	uint st = 0, tl; int pwr = 0, len;
        while( narc.size() < mx )
	{
	    len = getDigs( st );
	    if( pwr != len )
	    {
		pwr = len;
		fillPower( pwr );
	    }
            tl = 0;
	    for( int i = 1; i < 10; i++ )
		tl += static_cast<uint>( powr[i] * digs[i] );

	    if( tl == st ) narc.push_back( st );
	    st++;
	}
    }

    void display()
    {
	for( vector<uint>::iterator i = narc.begin(); i != narc.end(); i++ )
	    cout << *i << " ";
	cout << "\n\n";
    }

private:
    int getDigs( uint st )
    {
	memset( digs, 0, 10 * sizeof( int ) );
	int r = 0;
	while( st )
	{
	    digs[st % 10]++;
	    st /= 10;
	    r++;
	}
        return r;
    }

    void fillPower( int z )
    {
	for( int i = 1; i < 10; i++ )
	    powr[i] = pow( static_cast<float>( i ), z );
    }

    vector<uint> narc;
    uint powr[10];
    int digs[10];
};

int main( int argc, char* argv[] )
{
    NarcissisticDecs n;
    n.makeList( 25 );
    n.display();
    return system( "pause" );
}


C#: using System;

namespace Narcissistic
{
    class Narcissistic
    {
        public bool isNarcissistic(int z)
        {
            if (z < 0) return false;
            string n = z.ToString();
            int t = 0, l = n.Length;
            foreach (char c in n)
                t += Convert.ToInt32(Math.Pow(Convert.ToDouble(c - 48), l));

            return t == z;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Narcissistic n = new Narcissistic();
            int c = 0, x = 0;
            while (c < 25)
            {
                if (n.isNarcissistic(x))
                {
                    if (c % 5 == 0) Console.WriteLine();
                    Console.Write("{0,7} ", x);
                    c++;
                }
                x++;
            }
            Console.WriteLine("\n\nPress any key to continue...");
            Console.ReadKey();
        }
    }
}
Task: Narcissistic decimal number


C++: #include <iostream>
#include <vector>
using namespace std;
typedef unsigned int uint;

class NarcissisticDecs
{
public:
    void makeList( int mx )
    {
	uint st = 0, tl; int pwr = 0, len;
        while( narc.size() < mx )
	{
	    len = getDigs( st );
	    if( pwr != len )
	    {
		pwr = len;
		fillPower( pwr );
	    }
            tl = 0;
	    for( int i = 1; i < 10; i++ )
		tl += static_cast<uint>( powr[i] * digs[i] );

	    if( tl == st ) narc.push_back( st );
	    st++;
	}
    }

    void display()
    {
	for( vector<uint>::iterator i = narc.begin(); i != narc.end(); i++ )
	    cout << *i << " ";
	cout << "\n\n";
    }

private:
    int getDigs( uint st )
    {
	memset( digs, 0, 10 * sizeof( int ) );
	int r = 0;
	while( st )
	{
	    digs[st % 10]++;
	    st /= 10;
	    r++;
	}
        return r;
    }

    void fillPower( int z )
    {
	for( int i = 1; i < 10; i++ )
	    powr[i] = pow( static_cast<float>( i ), z );
    }

    vector<uint> narc;
    uint powr[10];
    int digs[10];
};

int main( int argc, char* argv[] )
{
    NarcissisticDecs n;
    n.makeList( 25 );
    n.display();
    return system( "pause" );
}



Go: package main

import "fmt"

func narc(n int) []int {
	power := [...]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
	limit := 10
	result := make([]int, 0, n)
	for x := 0; len(result) < n; x++ {
		if x >= limit {
			for i := range power {
				power[i] *= i 
			}
			limit *= 10
		}
		sum := 0
		for xx := x; xx > 0; xx /= 10 {
			sum += power[xx%10]
		}
		if sum == x {
			result = append(result, x)
		}
	}
	return result
}

func main() {
	fmt.Println(narc(25))
}
Task: Narcissistic decimal number


C++: #include <iostream>
#include <vector>
using namespace std;
typedef unsigned int uint;

class NarcissisticDecs
{
public:
    void makeList( int mx )
    {
	uint st = 0, tl; int pwr = 0, len;
        while( narc.size() < mx )
	{
	    len = getDigs( st );
	    if( pwr != len )
	    {
		pwr = len;
		fillPower( pwr );
	    }
            tl = 0;
	    for( int i = 1; i < 10; i++ )
		tl += static_cast<uint>( powr[i] * digs[i] );

	    if( tl == st ) narc.push_back( st );
	    st++;
	}
    }

    void display()
    {
	for( vector<uint>::iterator i = narc.begin(); i != narc.end(); i++ )
	    cout << *i << " ";
	cout << "\n\n";
    }

private:
    int getDigs( uint st )
    {
	memset( digs, 0, 10 * sizeof( int ) );
	int r = 0;
	while( st )
	{
	    digs[st % 10]++;
	    st /= 10;
	    r++;
	}
        return r;
    }

    void fillPower( int z )
    {
	for( int i = 1; i < 10; i++ )
	    powr[i] = pow( static_cast<float>( i ), z );
    }

    vector<uint> narc;
    uint powr[10];
    int digs[10];
};

int main( int argc, char* argv[] )
{
    NarcissisticDecs n;
    n.makeList( 25 );
    n.display();
    return system( "pause" );
}







VB: Private Function narcissistic(n As Long) As Boolean
    Dim d As String: d = CStr(n)
    Dim l As Integer: l = Len(d)
    Dim sumn As Long: sumn = 0
    For i = 1 To l
        sumn = sumn + (Mid(d, i, 1) - "0") ^ l
    Next i
    narcissistic = sumn = n
End Function

Public Sub main()
    Dim s(24) As String
    Dim n As Long: n = 0
    Dim found As Integer: found = 0
    Do While found < 25
        If narcissistic(n) Then
            s(found) = CStr(n)
            found = found + 1
        End If
        n = n + 1
    Loop
    Debug.Print Join(s, ", ")
End Sub
Task: Narcissistic decimal number



C#: using System;

namespace Narcissistic
{
    class Narcissistic
    {
        public bool isNarcissistic(int z)
        {
            if (z < 0) return false;
            string n = z.ToString();
            int t = 0, l = n.Length;
            foreach (char c in n)
                t += Convert.ToInt32(Math.Pow(Convert.ToDouble(c - 48), l));

            return t == z;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Narcissistic n = new Narcissistic();
            int c = 0, x = 0;
            while (c < 25)
            {
                if (n.isNarcissistic(x))
                {
                    if (c % 5 == 0) Console.WriteLine();
                    Console.Write("{0,7} ", x);
                    c++;
                }
                x++;
            }
            Console.WriteLine("\n\nPress any key to continue...");
            Console.ReadKey();
        }
    }
}


Go: package main

import "fmt"

func narc(n int) []int {
	power := [...]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
	limit := 10
	result := make([]int, 0, n)
	for x := 0; len(result) < n; x++ {
		if x >= limit {
			for i := range power {
				power[i] *= i 
			}
			limit *= 10
		}
		sum := 0
		for xx := x; xx > 0; xx /= 10 {
			sum += power[xx%10]
		}
		if sum == x {
			result = append(result, x)
		}
	}
	return result
}

func main() {
	fmt.Println(narc(25))
}
Task: Narcissistic decimal number



C#: using System;

namespace Narcissistic
{
    class Narcissistic
    {
        public bool isNarcissistic(int z)
        {
            if (z < 0) return false;
            string n = z.ToString();
            int t = 0, l = n.Length;
            foreach (char c in n)
                t += Convert.ToInt32(Math.Pow(Convert.ToDouble(c - 48), l));

            return t == z;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Narcissistic n = new Narcissistic();
            int c = 0, x = 0;
            while (c < 25)
            {
                if (n.isNarcissistic(x))
                {
                    if (c % 5 == 0) Console.WriteLine();
                    Console.Write("{0,7} ", x);
                    c++;
                }
                x++;
            }
            Console.WriteLine("\n\nPress any key to continue...");
            Console.ReadKey();
        }
    }
}



Java: public class Narc{
	public static boolean isNarc(long x){
		if(x < 0) return false;
		
		String xStr = Long.toString(x);
		int m = xStr.length();
		long sum = 0;
		
		for(char c : xStr.toCharArray()){
			sum += Math.pow(Character.digit(c, 10), m);
		}
		return sum == x;
	}

	public static void main(String[] args){
		for(long x = 0, count = 0; count < 25; x++){
			if(isNarc(x)){
				System.out.print(x + " ");
				count++;
			}
		}
	}
}
Task: Narcissistic decimal number



C#: using System;

namespace Narcissistic
{
    class Narcissistic
    {
        public bool isNarcissistic(int z)
        {
            if (z < 0) return false;
            string n = z.ToString();
            int t = 0, l = n.Length;
            foreach (char c in n)
                t += Convert.ToInt32(Math.Pow(Convert.ToDouble(c - 48), l));

            return t == z;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Narcissistic n = new Narcissistic();
            int c = 0, x = 0;
            while (c < 25)
            {
                if (n.isNarcissistic(x))
                {
                    if (c % 5 == 0) Console.WriteLine();
                    Console.Write("{0,7} ", x);
                    c++;
                }
                x++;
            }
            Console.WriteLine("\n\nPress any key to continue...");
            Console.ReadKey();
        }
    }
}





Python: from __future__ import print_function
from itertools import count, islice

def narcissists():
    for digits in count(0):
        digitpowers = [i**digits for i in range(10)]
        for n in range(int(10**(digits-1)), 10**digits):
            div, digitpsum = n, 0
            while div:
                div, mod = divmod(div, 10)
                digitpsum += digitpowers[mod]
            if n == digitpsum:
                yield n

for i, n in enumerate(islice(narcissists(), 25), 1):
    print(n, end=' ')
    if i % 5 == 0: print() 
print()
Task: Narcissistic decimal number



C#: using System;

namespace Narcissistic
{
    class Narcissistic
    {
        public bool isNarcissistic(int z)
        {
            if (z < 0) return false;
            string n = z.ToString();
            int t = 0, l = n.Length;
            foreach (char c in n)
                t += Convert.ToInt32(Math.Pow(Convert.ToDouble(c - 48), l));

            return t == z;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Narcissistic n = new Narcissistic();
            int c = 0, x = 0;
            while (c < 25)
            {
                if (n.isNarcissistic(x))
                {
                    if (c % 5 == 0) Console.WriteLine();
                    Console.Write("{0,7} ", x);
                    c++;
                }
                x++;
            }
            Console.WriteLine("\n\nPress any key to continue...");
            Console.ReadKey();
        }
    }
}






VB: Private Function narcissistic(n As Long) As Boolean
    Dim d As String: d = CStr(n)
    Dim l As Integer: l = Len(d)
    Dim sumn As Long: sumn = 0
    For i = 1 To l
        sumn = sumn + (Mid(d, i, 1) - "0") ^ l
    Next i
    narcissistic = sumn = n
End Function

Public Sub main()
    Dim s(24) As String
    Dim n As Long: n = 0
    Dim found As Integer: found = 0
    Do While found < 25
        If narcissistic(n) Then
            s(found) = CStr(n)
            found = found + 1
        End If
        n = n + 1
    Loop
    Debug.Print Join(s, ", ")
End Sub
Task: Narcissistic decimal number




Go: package main

import "fmt"

func narc(n int) []int {
	power := [...]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
	limit := 10
	result := make([]int, 0, n)
	for x := 0; len(result) < n; x++ {
		if x >= limit {
			for i := range power {
				power[i] *= i 
			}
			limit *= 10
		}
		sum := 0
		for xx := x; xx > 0; xx /= 10 {
			sum += power[xx%10]
		}
		if sum == x {
			result = append(result, x)
		}
	}
	return result
}

func main() {
	fmt.Println(narc(25))
}


Java: public class Narc{
	public static boolean isNarc(long x){
		if(x < 0) return false;
		
		String xStr = Long.toString(x);
		int m = xStr.length();
		long sum = 0;
		
		for(char c : xStr.toCharArray()){
			sum += Math.pow(Character.digit(c, 10), m);
		}
		return sum == x;
	}

	public static void main(String[] args){
		for(long x = 0, count = 0; count < 25; x++){
			if(isNarc(x)){
				System.out.print(x + " ");
				count++;
			}
		}
	}
}