## Very Simple Programming Challenge

This is the place for ALL of the user submitted challenges. If you create a little challenge/mission/riddle/whatever, post it here.
Forum rules
Do not post missions that you did NOT create without proper citing.

### Very Simple Programming Challenge

For those of you that are taking up a new language or have not yet become fluent in one. Here is a very very simple challenge, just to knock dust off.

Problem: Find the first 25 power numbers.

Details: A number is considered a power number if the sum of each digit in the number raised to the power of the length of the number is the number. I will give you two examples:

Code: Select all
`0 => 0 ** 1 = 0      + 0      ____      0`

Code: Select all
`153 => 1 ** 3 = 1       5 ** 3 = 125       3 ** 3 = 27       ___________       + 3       + 125       + 27       _____       153`
Last edited by Turn on Fri May 15, 2015 4:41 am, edited 1 time in total.
Social Engineering:
<cen> .lua print ('Tsyn 9.47.-u 3 12 5')
* slickery has quit (User has been banned from HackThisSite (Attempting to use a SpyBot))
* cen has quit (User has been banned from HackThisSite (Attempting to use a SpyBot))

Turn
Poster

Posts: 120
Joined: Tue Feb 17, 2015 5:42 am
Blog: View Blog (0)

### Re: Very Simple Programming Challenge

You example of 153 is wrong.. i mean 153 is write but 1 => 3 == 1 not 3. your math with 3 comes out to 155.

I'm going to do this.. but with a twist.. i'm going to use BATCH

edit: are we starting at 0 or 1?

tremor77

Posts: 1098
Joined: Wed Mar 31, 2010 12:00 pm
Location: New York
Blog: View Blog (0)

### Re: Very Simple Programming Challenge

Fun simple little challenge that, as a somewhat new programmer, challenged me just enough. Not sure, should we post the scripts we write or answers? I'll wait before I do.

Now to take what I'm learning about recursion and convert my for loop to a recursive function.

P.S. Thanks for this. Challenges likes this are what, imho, make me a better programmer. Plus I'm bad at coming up with things to code.

EDIT: So, I'm new to recursion...apparently calling a function recursively an amount of times equal to more than the 17th power number crashes python (Its around 10k). I set sys.setrecursionlimit to bigger numbers, but alas, it still crashes. Any clues that might make a recursive version of this work?
Program your brain, or someone will do it for you.

v_freak
New User

Posts: 49
Joined: Mon Jan 19, 2015 9:48 pm
Blog: View Blog (0)

### Re: Very Simple Programming Challenge

tremor77 wrote:You example of 153 is wrong.. i mean 153 is write but 1 => 3 == 1 not 3. your math with 3 comes out to 155.

I'm going to do this.. but with a twist.. i'm going to use BATCH

edit: are we starting at 0 or 1?

Oh, fixed, thanks for the call out on that.
You're first power number will be 0, so start there.

v_freak wrote:Not sure, should we post the scripts we write or answers?

Feel free to post whatever you like. Answers, code, thoughts...

v_freak wrote:P.S. Thanks for this. Challenges likes this are what, imho, make me a better programmer. Plus I'm bad at coming up with things to code.

Not a problem at all. I know there are some virgin coders that linger around, so it gives them something they can hopefully accomplish without feeling overwhelmed.

v_freak wrote:EDIT: So, I'm new to recursion...apparently calling a function recursively an amount of times equal to more than the 17th power number crashes python (Its around 10k). I set sys.setrecursionlimit to bigger numbers, but alas, it still crashes. Any clues that might make a recursive version of this work?

This is known as a stack overflow.
It could be likely that you surpass your given stack level for your shell (can be modified with ulimit -s). However, this function will call itself way too many times (millions of times) and would make things a hassle to do that way anyways. It's also not a good idea to tamper with the amount of information that can be on the stack. It is set there as a preventive measure so that the entire system does not crash instead. You might also enjoy looking into stack smashing which uses this kind of stuff to cause buffer overflows. I would recommend just creating a simple for or while loop and calling a method from inside of it. This will make it so that it finishes the function and removes the tedious stack error (non recursive approach). My approach would be to create a list to store your found power numbers and create a while loop that remains true until the list reaches the 25th number.
Social Engineering:
<cen> .lua print ('Tsyn 9.47.-u 3 12 5')
* slickery has quit (User has been banned from HackThisSite (Attempting to use a SpyBot))
* cen has quit (User has been banned from HackThisSite (Attempting to use a SpyBot))

Turn
Poster

Posts: 120
Joined: Tue Feb 17, 2015 5:42 am
Blog: View Blog (0)

### Re: Very Simple Programming Challenge

I got the following 25 numbers:

Code: Select all
`0123456789153370371407163482089474547489272793084548834174172542108189800817992631524678050`

Here's my code (i used c#):
Code: Select all
`using System;namespace PowerNumberChallenge{    class Program    {        static void Main(string[] args)        {             PowerNumOps powerNum = new PowerNumOps();             powerNum.displayFirst25PowerNums();             Console.ReadKey();        }    }    class PowerNumOps    {        //get sum of each digit in the number raised to the power of the length        private int numTotal(string currentNumber)        {            int total = 0;            int length = currentNumber.Trim().Length;            foreach (char num in currentNumber.Trim())            {                // -48 to offset ascii char value                total += Convert.ToInt32(Math.Pow(Convert.ToInt32(num) - 48, length));            }            return total;        }        //check if current number is a power number        private bool isPowerNumber(string currentNumber)        {            if(numTotal(currentNumber).Equals(Convert.ToInt32(currentNumber.Trim())))                return true;            else                return false;        }        //iterate through numbers        public void displayFirst25PowerNums()        {            int count = 0;            for(int i = 0; count <= 25; i++)            {                if(isPowerNumber(Convert.ToString(i)))                {                    Console.WriteLine(i);                    count++;                }            }        }    }}`

LoGiCaL__

Posts: 1080
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)

### Re: Very Simple Programming Challenge

LoGiCaL__ wrote:I got the following 25 numbers

Of course these numbers checked out, good work.

LoGiCaL__ wrote:Here's my code (i used c#):

Nice, I'm glad to see someone using a non interpreted language. Given the task at hand, it helps speed things up too.
Social Engineering:
<cen> .lua print ('Tsyn 9.47.-u 3 12 5')
* slickery has quit (User has been banned from HackThisSite (Attempting to use a SpyBot))
* cen has quit (User has been banned from HackThisSite (Attempting to use a SpyBot))

Turn
Poster

Posts: 120
Joined: Tue Feb 17, 2015 5:42 am
Blog: View Blog (0)

### Re: Very Simple Programming Challenge

Same numbers as logic, in haskell

Code: Select all
`splitupRun d = splitup (d `div` 10) (d `mod` 10)splitup 0 m = [m]splitup d m = m : splitup (d `div` 10) (d `mod` 10)getSum p = sum . map (^p)isPowNumRun num = isPowNum num numlist (length numlist)   where numlist = splitupRun numisPowNum n nl len = (== n) \$ getSum len nlmain = do    let result = take 25 \$ filter isPowNumRun [0..]   print result`
sysopfb
New User

Posts: 17
Joined: Tue Sep 09, 2014 9:36 pm
Blog: View Blog (0)

### Re: Very Simple Programming Challenge

I've become frustrated with BATCH for this operation........ lol, like I originally stated I would try but It keeps crashing.. I will give it a few more hours....

on another note - what kind of calculatation time are your solutions taking? I'll like to see some performance analysis...

I decided to try out the HTML5 web worker and see what kind of performance I could get out of pseudo-threading javascript. It does the job pretty spiffy.
Last edited by tremor77 on Thu Aug 20, 2015 8:49 pm, edited 1 time in total.

tremor77

Posts: 1098
Joined: Wed Mar 31, 2010 12:00 pm
Location: New York
Blog: View Blog (0)

### Re: Very Simple Programming Challenge

I made my solution in c, under linux

Code: Select all
`// program: powernumers// compile with: gcc -lm -o powernumbers powernumbers.c#include <stdio.h>#include <string.h>#include <math.h>int isPowerNumber(int pn);int main() {     int pnMax = 26;    int cnt = 0;   int nr = 0;      while (cnt < pnMax) {        if ( isPowerNumber(nr) ) {      printf("%d is a power number\n",nr);       cnt++;     }         nr++;        }}int isPowerNumber(int pn) {    char str[15];  sprintf(str,"%d\0",pn);      int nrLength = strlen(str);   char* pt = str;  int i;  int total = 0;    for ( i=0;i<sizeof(str);i++) {        if ( pt[i] == '\0') {      if (total == pn) return 1;      return 0;      break;    }                  int nr = str[i] - '0';        total += pow(nr,nrLength);          } }`
boriz666
Experienced User

Posts: 99
Joined: Tue Mar 24, 2015 11:53 am
Blog: View Blog (0)

### Re: Very Simple Programming Challenge

Tremor, your code really impressed me
boriz666, great job as well
Social Engineering:
<cen> .lua print ('Tsyn 9.47.-u 3 12 5')
* slickery has quit (User has been banned from HackThisSite (Attempting to use a SpyBot))
* cen has quit (User has been banned from HackThisSite (Attempting to use a SpyBot))

Turn
Poster

Posts: 120
Joined: Tue Feb 17, 2015 5:42 am
Blog: View Blog (0)

Next