Forum Topic

Programming Puzzle

  • Programming Puzzle #1

    output \"2014\" in the screen

    no numerical chars in the source code. Do not use \"0,1,2,3,4,5,6,7,8,9\" in your source code.
    no external variables like DateTime.Year
    no file.read



    eto sample:

    string twenty = \"aaaaaaaaaaaaaaaaaaaa\".Length.ToString()
    string fourteen = \"aaaaaaaaaaaaaaa\".Length.ToString()

    Console.WriteLine(string.Concat(twenty,fourteen);


    isip pa kayo ibang way!
  • Using Javascript :)

    document.write(\"ABCDEFGHIJKLMNOPQRSTUVWXYZ\".indexOf(\'U\').toString()+\"ABCDEFGHIJKLMNOPQRSTUVWXYZ\".indexOf(\'O\').toString());
  • python:

    method 1:
    for x in \'cabe\': print ord(x)-ord(\'a\'),

    method 2:
    print \'\'.join( [ str(ord(x) - ord(\'a\')) for x in \'cabe\' ])

    -- edited by sevenstring on Mar 24 2015, 12:53 PM
  • for x in \'cabe\': print ord(x)-ord(\'a\'),


    galling!
  • C#:

    Console.WriteLine(Convert.ToChar(\',\')*Convert.ToChar(\',\')+Convert.ToChar(\'N\'));
  • I am not sure if this is within the criteria, another from python.

    country = [\'japan\', \'malaysia\', \'philippines\', \'china\', \'thailand\']
    value = str(country.index(\'philippines\')) + str(country.index(\'japan\')) + str(country.index(\'malaysia\')) + str(country.index(\'thailand\'))
    print(value)
  • i also would like to share a simple programming puzzle. :)

    Cube of a number can also be solved using addition instead of multiplying the number three times, and this is the pattern:
    1 = 1
    2 = 3 + 5 = 8
    3 = 7 + 9 + 11 = 27
    4 = 13 + 15 + 17 + 19 = 64
    5 = 21 + 23 + 25 + 27 + 29 = 125
    so on so forth...

    Write a program in which you enter an integer variable (maybe from 1 to 20) and it will display all the addends.
    As a check, you might also try to add all the addends to see if you displayed all the correct addends.

    example:
    input : 5
    output of the program: 21 23 25 27 29
    addition of all addends: 125
  • number x (number - 1) + 1

    i think that is formula for the first addend
  • Using python class, with dragonslash\'s first term.
    Code:
    class cube_of_number():
    def __init__(self, num):
    self.num = num

    n = self.num
    value = []
    inc = 0

    for i in range(n):
    if i == 0:
    first_term = n*(n-1)+1
    inc += first_term
    value.append(first_term)
    else:
    inc += 2
    value.append(inc)
    self.value = value

    def show_addends(self):
    print(\'Addends for %d:\' %(self.num)),
    for item in self.value:
    print(\'%s \' %(item)),
    print

    def show_total(self):
    print(\'sum: %d\' %(sum(self.value)))

    def show_cube(self):
    n = self.num
    print(\'%dx%dx%d: %d\' %(n, n, n, n*n*n))

    def show_result_validity(self):
    if self.num*self.num*self.num == sum(self.value):
    print(\'sum check: Valid!!\\n\')
    else:
    print(\'sum check: Invalid??\\n\')



    def main():

    n = int(raw_input(\'input a number: \'))

    for i in range(1, n+1):
    z = cube_of_number(i)

    z.show_addends()
    z.show_total()
    z.show_cube()
    z.show_result_validity()


    if __name__ == \'__main__\':
    main()


    Sample run:
    input a number: 5
    Addends for 1: 1
    sum: 1
    1x1x1: 1
    sum check: Valid!!

    Addends for 2: 3 5
    sum: 8
    2x2x2: 8
    sum check: Valid!!

    Addends for 3: 7 9 11
    sum: 27
    3x3x3: 27
    sum check: Valid!!

    Addends for 4: 13 15 17 19
    sum: 64
    4x4x4: 64
    sum check: Valid!!

    Addends for 5: 21 23 25 27 29
    sum: 125
    5x5x5: 125
    sum check: Valid!!
  • Nice!
  • using Gauss Theorem and powershell

    $input = Read-Host
    [int] $number = [int]::Parse($input)
    [int] $firstTerm = $number * ($number - 1 ) + 1
    [int] $lastTerm = (($number -1 ) * 2) + $firstTerm
    $output = 0.5 * $number * ($firstTerm + $lastTerm)
    for($i=$firstTerm;$i -le $lastTerm;$i+=2) {write-host \"$i \" -NoNewline} write-host
    write-host \"total = $output\"

    -- edited by arthurjr07 on Mar 26 2015, 10:14 AM
  • Puzzle # 3

    Create a program that will output it\'s source code in the screen

    No File.Read
    No external variable like Assembly.Resource, File.Description, Environment.variable etc.

    -- edited by arthurjr07 on Mar 30 2015, 01:36 PM
  • Puzzle#4

    input is 2 integers separated with comma
    output is the quotient is you divide the first number by the second number, disregard the remainder

    example:
    Input 6,2
    output 3

    do not use +,-,*and / operations in your source code.
  • Puzzle $# 3 - create a string and assign the entire contents of the source code into this string.

    Puzzle #4:
    1. Use the integer division logic found here: http://en.wikipedia.org/wiki/Division_algorithm#Integer_division_.28unsigned.29_with_remainder
    2. There is a \'-\' operator used in the logic. Two\'s complement can be used instead to subtract 2 integers.
  • Puzzle $# 3 - create a string and assign the entire contents of the source code into this string.


    this will not work. this is wrong.
  • lol. of course it\'s not gonna work by just assigning a string variable the contents of the source without the rest of the code, if that\'s what you mean. but it will work.

    see the examples here: http://en.wikipedia.org/wiki/Quine_(computing)

    or the code in the last post here: http://www.cplusplus.com/forum/unices/14588/
  • galling :)
  • nakaka dugo ng utak dito haha.. nice thread
  • maganda din itong site na to:

    https://projecteuler.net/archives

    :)