~ Programming Challenge #4

View previous topic View next topic Go down

~ Programming Challenge #4

Post by Gecko on 7/8/2014, 5:12 pm

This is going to be a series of programming based challenges. So I will be starting easy on the first one. These challenges will be extended to two weeks in the hopes of more activity, and based more around solving potential real world problems, or with real world applications.

It doesn't matter what language you use, in fact, do the problem by hand if you feel like it!

This Weeks Problem:
Code:

Sometimes communications break down, and you cannot just give a person a number, numbers can be confusing to read if they lack commas. Such as 536842357425, I mean clearly this is "Five-Hundred-Thirty-Six-Billion Eight-Hundred-Forty-Two-Million Three-Hundred-Fifty-Seven-Thousand Four-Hundred-Twenty-Five"

Btw, there is your challenge, make a program that takes an arbitrarily large number and coverts it to a formatted text string :3

Last Challenge:

Code:
Greatest Common Factors are fun! So what do I want? I want you to type me up a program that:
~Takes 2, user given, numbers finds the greatest common factor
~Takes another 2, user given, numbers finds the greatest common factor
~Decides the largest one and subtracts the smaller one
~If two of the numbers do not have a common factor use the number One

EXAMPLE: Four numbers(8, 24)(5, 102)
                   GCF: 8, GCF: 1
                          Ans: 7
Rules:
~IN A SPOILER, post your source code using the code tags.
~IN THE SAME OR ANOTHER SPOILER, post your answer that you got.


Last edited by Gecko on 17/9/2014, 2:32 am; edited 8 times in total
avatar
Gecko
Tier 4 (500 posts)
Tier 4 (500 posts)


Back to top Go down

Re: ~ Programming Challenge #4

Post by Gildarts on 11/8/2014, 4:56 am

Well i may as well, but this isent the highest it can go below 100,000

Spoiler:

19, 31, 109, 199, 409, 571, 631, 829, 1489, 1999, 2341, 2971, 3529, 4621, 4789, 7039, 7669, 8779, 9721, 10459, 10711, 13681, 14851, 16069, 16381, 17659, 20011, 20359, 23251, 25939, 27541, 29191, 29611, 31321, 34429, 36739, 40099, 40591, 42589

_____________________

Spoiler:



My Vouches:http://www.smokinelite.com/t81285-kid-gildarts-vouches-19
My merching Log: http://www.smokinelite.com/t81013-kid-rampage5-s-merching-log#1121170
My Art log: http://www.smokinelite.com/t81402-kid-s-art-log
My 1-99 Prayer guide: http://www.smokinelite.com/t81023-runescape-1-99-prayer-guide
[/center]
avatar
Gildarts
Forum Fanatic (1000 posts)
Forum Fanatic (1000 posts)


Back to top Go down

Re: ~ Programming Challenge #4

Post by Broeder on 11/8/2014, 7:30 am

Gotta post the code you used, Kid Smile

I know this assignment, in fact Ive solved it with Python and I believe also with C. Too bad I dont have that code anymore.

Fun idea though!
avatar
Broeder
Grandmaster (2000 posts)
Grandmaster (2000 posts)


Back to top Go down

Re: ~ Programming Challenge #4

Post by Gecko on 12/8/2014, 11:11 pm

Hey Broeder, why don't you type it up again? This isn't quite the same thing, this challenge is based off a Project Euler problem, but not quite. Next challenge is going to be fun :3

As for me, I'll throw down my code on the 14th when the challenge is over.
Also thank you Kid, but I bet you can do better Razz
avatar
Gecko
Tier 4 (500 posts)
Tier 4 (500 posts)


Back to top Go down

Re: ~ Programming Challenge #4

Post by ftpatat on 13/8/2014, 10:23 pm

C#-code:

Code:
  
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Fibonacci
{
    public partial class Form1 : Form
    {
        int getal_1 = 0;
        int getal_2 = 1;
        int getal;
        int priemgetal;
        int deler;
        int aantal=0;
        int limiet=100000;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            while (getal < limiet)
            {
                getal = getal_1 + getal_2;
                if (getal < limiet)
                {
                    getal_1 = getal_2;
                    getal_2 = getal;
                    priemgetal = getal;
                }
                else
                {
                    priemgetal = getal_2;
                }

                for (deler = 2; deler < priemgetal; deler++)
                {
                    if (priemgetal % deler == 0)
                    {
                        aantal++;
                    }
                }
                if (aantal == 0)
                {
                    textBox1.Text = priemgetal.ToString();
                }
                else
                {
                     aantal = 0;
                }
            }
        }
    }
}
Answer:
28657

I made a quick program in c#
Let me know if the answer is correct Razz
avatar
ftpatat
Grandmaster (2000 posts)
Grandmaster (2000 posts)


Back to top Go down

Re: ~ Programming Challenge #4

Post by Gecko on 13/8/2014, 10:51 pm

So here is my code. The correct answer will be posted tomorrow when I change the challenge. Very Happy
Thanks to all who participated.
C++:

Code:
#include <iostream>
#include <?????.h>
using namespace std;

int main()
{
   int x = 0;
   
   int fib1 = 1;
   int fib2 = 1;
   int fibh = 0;
   while(fibh < 100000)
   {
      fibh = fib1 + fib2;
      if(isPrime(fibh))
      {
         if(fibh > x)
            x = fibh;
      }
      fib1 = fib2;
      fib2 = fibh;
   }
   cout << x;
}

In there is my personal library where I pull the function isPrime()
isPrime:
Code:
bool isPrime(unsigned long long x)
{
   if((x % 2 == 0 && x != 2) || x == 1 || (x % 3 == 0 && x != 3))
   {
      return false;
   }
   for(unsigned long long i = 2; i <= sqrt(x); i++)
   {
      if(x % i  == 0)
      {
         return false;
      }
   }
   return true;
}
avatar
Gecko
Tier 4 (500 posts)
Tier 4 (500 posts)


Back to top Go down

Re: ~ Programming Challenge #4

Post by Gecko on 15/8/2014, 2:43 am

New Challenge / Answer Posted!
avatar
Gecko
Tier 4 (500 posts)
Tier 4 (500 posts)


Back to top Go down

Re: ~ Programming Challenge #4

Post by kalio on 15/8/2014, 11:06 pm

Python 3
Python 3:

Code:
# Challenge #2

import zlib

data = [b'x\x9c\xd3\xc9\xcf\xc9I\xcd\x00\x00\x07_\x02A',
        b'x\x9c\xf3\xf5\x0f\xf1p\rR(\xcf/\xcaIQt\x0bu\xf6v\r\x02\x00<I\x05\xf9',
        b'x\x9c+N\xcdI\xd3\xcbH\xcd\xc9\xc9\x8f/(\xca\xcc+I-\xd2\x03\xd3\x1a\x9a\xd6\xc5 \xa9\xf2\xfc\xa2\x9c\x14t)\x00+\xfd\x14F'
        ]

class Printer:

    def __init__(self, text):
        self.text = text

    def print(self):
        print(self.text, end='')

class HelloPrinter(Printer):

    def __init__(self):
        text = zlib.decompress(data[0]).decode('utf-8')[::-1].capitalize()
        super().__init__(text)

class WorldPrinter(Printer):

    def __init__(self):
        text = zlib.decompress(data[1]).decode('utf-8')[6:13]
        super().__init__(text)

class HelloWorldPrinter:

    def __init__(self):
        self.hello_printer = HelloPrinter()
        self.world_printer = WorldPrinter()

    def print(self):
        exec(zlib.decompress(data[2]).decode('utf-8'))
        print()


HelloWorldPrinter().print()


Last edited by Gecko on 2/9/2014, 6:21 pm; edited 1 time in total (Reason for editing : Spoiler Tags)
avatar
kalio
Tier 4 (500 posts)
Tier 4 (500 posts)


Back to top Go down

Re: ~ Programming Challenge #4

Post by ftpatat on 18/8/2014, 1:30 pm

Your answer for the first task is wrong it isn't 28647, but it is 28657 noob Razz
avatar
ftpatat
Grandmaster (2000 posts)
Grandmaster (2000 posts)


Back to top Go down

Re: ~ Programming Challenge #4

Post by Gecko on 19/8/2014, 12:06 am

ftpatat wrote:Your answer for the first task is wrong it isn't 28647, but it is 28657 noob Razz

Psh, typos don't make me a noob. Noobiness is inherited. So That is what makes me a noob. Also Kalio, nice code XD
avatar
Gecko
Tier 4 (500 posts)
Tier 4 (500 posts)


Back to top Go down

Re: ~ Programming Challenge #4

Post by Gecko on 29/8/2014, 3:48 pm

Will post my code tomorrow, any takers?
avatar
Gecko
Tier 4 (500 posts)
Tier 4 (500 posts)


Back to top Go down

Re: ~ Programming Challenge #4

Post by Swadq on 2/9/2014, 3:23 pm

C++
Spoiler:

Code:
# include <cmath>
# include <iostream>

using namespace std;

int gcf(int a, int b) {
   // Euclidean algorithm
      
   int temp;
   while (b != 0) {
      temp = b;
      b = a % b;
      a = temp;
   }
   
   return a;
}

int main() {
   int oneA, twoA, oneB, twoB;
   
   cout << "First pair of numbers, seperated by a space: ";
   cin >> oneA;
   cin >> twoA;
   
   cout << "Second pair of numbers, seperated by a space: ";
   cin >> oneB;
   cin >> twoB;

   int gcfA, gcfB;
   gcfA = gcf(oneA, twoA);
   gcfB = gcf(oneB, twoB);

   cout << "First GCF: " << gcfA << endl;
   cout << "Second GCF: " << gcfB << endl;
   cout << "Ans: " << abs(gcfA - gcfB) << endl;

   return 0;
}
Sample IO
Spoiler:
First pair of numbers, seperated by a space: 8 24
Second pair of numbers, seperated by a space: 5 102
First GCF: 8
Second GCF: 1
Ans: 7
avatar
Swadq
Grandmaster (2000 posts)
Grandmaster (2000 posts)


http://swadq.com

Back to top Go down

Re: ~ Programming Challenge #4

Post by Gecko on 3/9/2014, 3:38 pm

Have no idea why you were down repped. +rep to even out, Swadq
avatar
Gecko
Tier 4 (500 posts)
Tier 4 (500 posts)


Back to top Go down

Re: ~ Programming Challenge #4

Post by Gecko on 9/9/2014, 1:54 am

My original used pass by reference and was returning false values and all sorts of undef behavior so... here is a smaller neater one. I was originally trying to employ a lot of concepts to make it more teaching oriented.

C++:

Code:
#include <iostream>
using namespace std;

int gf(int x, int y)
{
   for(int i = x; i > 0; i--)
   {
      if(!(x % i) && !(y % i))
         return i;
   }
}
            

int main()
{
   int x = 0;
   int y = 0;
   int g1;
   int g2;
   
   cout << "First Num: ";
   cin >> x;
   cout << "\nSecond Num: ";
   cin >> y;
   
   g1 = gf(x, y);
   
   cout << "First Num: ";
   cin >> x;
   cout << "\nSecond Num: ";
   cin >> y;
   
   g2 = gf(x, y);
   
   cout << "\nAnswer: " << ((g1 > g2) ? g1 - g2: g2 - g1);
      
   return 0;
}
avatar
Gecko
Tier 4 (500 posts)
Tier 4 (500 posts)


Back to top Go down

Re: ~ Programming Challenge #4

Post by Gecko on 17/9/2014, 2:33 am

Updated, with one difficult challenge, please try. I will
avatar
Gecko
Tier 4 (500 posts)
Tier 4 (500 posts)


Back to top Go down

Re: ~ Programming Challenge #4

Post by Minato on 30/9/2014, 9:53 pm

In my defence I will say that before this random feeling of wanting to program I hadn't opened eclipse for at least 6 months.
Please don't hate too much, because I know this code can be improved a lot.
Spoiler:

Code:



import java.util.Scanner;


public class main {
   public static void main(String[] args) {
      
         int number;
         int i = 0;
         
         //Getting the integer from the user
         Scanner in = new Scanner(System.in);
           System.out.println("Enter your integer, please.");
           number = in.nextInt();
           
           //Getting the number of digits from the integer
           int IntLength = String.valueOf(number).length();
           
           int IntDigits[] = new int[IntLength];
           
           //Getting each integer and putting it BACKWARDS into an array
           while (number > 0) {
               System.out.println( number % 10);
               IntDigits[i] = number % 10;
               number = number / 10;
               i++;
                  if(i == IntLength){
                     i--;
                  }
               
           }
           
           //A very crude compare mechanism
           while(i >= 0){
               if(IntDigits[i] == 9){
                  System.out.print("-Nine-");
               }
               else if(IntDigits[i] == 8){
                  System.out.print("-Eight-");
               }
               else if(IntDigits[i] == 7){
                  System.out.print("-Seven-");
               }
               else if(IntDigits[i] == 6){
                  System.out.print("-Six-");
               }
               else if(IntDigits[i] == 5){
                  System.out.print("-Five-");
               }
               else if(IntDigits[i] == 4){
                  System.out.print("-Four-");
               }
               else if(IntDigits[i] == 3){
                  System.out.print("-Three-");
               }
               else if(IntDigits[i] == 2){
                  System.out.print("-Two-");
               }
               else if(IntDigits[i] == 1){
                  System.out.print("-One-");
               }
               else if(IntDigits[i] == 0){
                  System.out.print("-Zero-");
               }
               else{
                  System.out.println("I am broken.");
               }
               i--;
              
           }

   }   
        
}   
   




_____________________



Minato
Grandmaster (2000 posts)
Grandmaster (2000 posts)


Back to top Go down

Re: ~ Programming Challenge #4

Post by Gaz on 10/4/2015, 3:18 pm

Bit late to the party with this but I love the idea of this thread so I did the most recent one.
Knocked it up quickly while working on something else in Unity which is why i am using a unity library lol. Only tested it with a few numbers so I can get back to work, don't judge me too harshly Smile

Code:

using UnityEngine;

public class test : MonoBehaviour
{
    string[] nums = { "", "one ", "two ", "three ", "four ", "five ", "six ", "seven ", "eight ", "nine " };
    string[] posVals = { "", "thousand ", "million ", "billion ", "trillion ", "quadrillion " };
    string[] tys = { "", "", "twenty ", "thirty ", "fourty ", "fifty ", "sixty ", "seventy ", "eighty ", "ninety " };
    string[] teens = { "ten ", "eleven ", "twelve ", "thirteen ", "fourteen ", "fifteen ", "sixteen ", "seventeen ", "eighteen ", "nineteen " };

    int num = 14573856;
    int numLength;
    int htuSets;

    int[] digits;
    string output;

    void Start()
    {
        numLength = Mathf.FloorToInt(Mathf.Log10(num) + 1);
        htuSets = Mathf.CeilToInt((float)numLength / 3);

        digits = new int[numLength];
        for (int i = 0; i < digits.Length; i++)
            digits[i] = num.ToString().ToCharArray()[i] - 48;

        for (int i = 0; i < digits.Length; i++)
        {
            if ((digits.Length - i) % 3 == 0)
                output += nums[digits[i]] + ((digits[i] == 0) ? "" : "hundred ") + ((digits[i+1] == 0 && digits[i+2] == 0) ? "" : "and ");

            if ((digits.Length - i) % 3 == 2)
                output += ((digits[i] == 1) ? teens[digits[i+1]] : tys[digits[i]]);

            if ((digits.Length - i) % 3 == 1)
                output += ((digits[i - ((i == 0) ? 0:1)] == 1 && i != 0) ? "" : nums[digits[i]]) + (checkPrev(i) ? "" : posVals[htuSets - Mathf.FloorToInt(i / 3 + 1)]);
        }
        Debug.Log(output);
    }

    bool checkPrev(int i)
    {
        if (i >= 2)
            if (digits[i] == 0 && digits[i - 1] == 0 && digits[i - 2] == 0)
                return true;
        return false;
    }
}

Some example values of num and their outputs:

14573856:


1000000


15:


619405201:


and finally, int.MaxValue:  Cool
avatar
Gaz
Forum Fanatic (1000 posts)
Forum Fanatic (1000 posts)


Back to top Go down

Re: ~ Programming Challenge #4

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum