Programming Challenges - Page 3

# Thread: Programming Challenges

1. Did any of you ever read "The Code Book" by Simon Singh? He did what Sepero's just done, and had 10 levels of difficulty. If I remember rightly, there was a cash prize, but that was several years ago. Great read.

2. Bad Speller ^^
Join Date
May 2001
Location
Uh, I'm somewhere where I don't know where I am.
Posts
1,228
Originally posted by o0zi
Did any of you ever read "The Code Book" by Simon Singh? He did what Sepero's just done, and had 10 levels of difficulty. If I remember rightly, there was a cash prize, but that was several years ago. Great read.
I'm actually in the process of reading that. Singh writes some damn good math books. I also read his Fermat's Theorem book(can't remember exactly what it was)...maybe Fermat's Enigma, or was that by someone else, ramble, ramble...

3. Registered User
Join Date
Mar 2001
Posts
729
Originally posted by grady
Compute the complex product ( a + ib ) * ( c + id ) using only three real multiplications
You have no idea how much pain this problem gave me.

x = (a+b)(c+d)
y = ac
z = bd

Answer = y-z + (x-y-z)i

4. (claps Strogian) Ingenious, absolutely ingenious...

5. Registered User
Join Date
Jul 2003
Posts
28
Hey strogian g/j, you got all my challenges.

Here's another challenge:
rand( ) in C generates a flat distribution of numbers; you are equally likely to get any given number the generator is capable of returning. If you histogram these numbers the histogram is relatively flat. Looking at the histogram ( sideways ) it might look like this:

x =0
xxxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxx
xxxxxxxxxxxx
xxxxxxxxxxx
xxxxxxxxxx
xxxxxxxxxxx
x = 2^32 - 1

Write a function such that the numbers are distributed as x^2. In other words if you histogrammed many numbers from your function it would look like this ( sideways ):

x = -1
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxx
xxxxxxxxxx
xxxxx
xx
x
xx
xxxx
xxxxxxxxxx
xxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
x = 1

only produce numbers on the interval [-1,1] instead of [0,2^32-1]. Put your function in a small program that outputs to the console a 20 bin ( line ) histogram similar to the one's I've shown above to prove ( by eyeball method ) that your function works correctly. If you really want to show off the function's legitimacy compute the chi-squared. You are allowed to use your language's native flat-distribution random number generator in your program.
Last edited by grady; 08-22-2003 at 04:08 PM.

6. i have a challenge. write a program that reads a text file and prints out how many characters, numbers, letters, words, and lines there are.

7. Registered User
Join Date
Apr 2001
Location
SF Bay Area, CA
Posts
14,947
Originally posted by trashthing
i have a challenge. write a program that reads a text file and prints out how many characters, numbers, letters, words, and lines there are.
Code:
```#!/bin/bash

/usr/bin/wc "\$@"```
You just have to give it the right options...

8. Another challenge: write a program that converts any decimal number to any base up to base 36, where numbers after 0-9 are represented by A-Z.

9. Originally posted by o0zi
Convert decimal numbers to a base up to base 36, where numbers after 0-9 are represented by A-Z.
Cool. I only thought I had to do 36base, but after re-reading it, I see he wanted "up to base 36". After a few modifications, it does binary, octal, hex, up to base36 and everything inbetween!!!

10. I'm attaching the binary for any non-C++ programmers. I might actually use this program sometime, so I thought you guys might too.

11. Cool solution. You put it under the LGPL? There's a man with principles

12. Thanks for the compliment.(Notice my signature)
I was gonna use GPL, but I figured it's a little extreme for something small as this.
Last edited by Sepero; 08-24-2003 at 03:13 AM.

13. Registered User
Join Date
Jul 2003
Posts
28
Challenge: Write a function that reverses the bits of an integer, i.e. 100110 -> 011001. The shorter the function the better. This is an essential procedure when coding a fast fourier transform.
Last edited by grady; 08-25-2003 at 05:45 PM.

14. Bad Speller ^^
Join Date
May 2001
Location
Uh, I'm somewhere where I don't know where I am.
Posts
1,228
do you mean like this:

Code:
```unsigned int x(1000);
return ~x;```
or
Code:
```unsigned int reverseBits(unsigned int x) {
unsigned int res(0), cntr(0xFFFFFFFF);
while (cntr) {
cntr >>= 1;
res <<= 1;
res |= x & 1;
x >>= 1;
}
return res;
}```

15. Registered User
Join Date
Jul 2003
Posts
28
The latter, I didn't realize my example was an xor too.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•