UTS Programming Competition 2013 Problem 2
From ProgSoc Wiki
Problem 2: The Palindromic Prime Postulate
It was a lazy Saturday afternoon in Sheldon and Leonard's apartment. Leonard was at his desk, tapping away on his computer. Sheldon was on the couch reading the latest Hulk comic book. They were sitting there together in blissful silence, as old friends and room-mates do, when all of a sudden, Sheldon opened his mouth.
"Tell me, Leonard," began Sheldon. "What is the best number? By the way, there’s only one correct answer!"
Oh dear, thought Leonard. Yet another one of Sheldon's flights of fancy. Knowing full well the repercussions of not playing along, Leonard decided to humour his tedious friend. With a sigh of resignation, Leonard replied, "I don't know...sixty-nine?"
"Wrong," Sheldon quipped. "The best number is 313. You're probably wondering why."
Silence. A beat.
Sheldon continued, unperturbed, "As you know, 313 is a prime number -- divisible only by itself and one. And obviously, 313 is also a palindrome in base ten. But, did you know that 313 is ALSO a palindrome in BASE TWO? You see, 313 in binary is one-zero-zero-one-one-one-zero-zero-one (100111001), which, backwards, is one-zero-zero-one-one-one-zero-zero-one, exactly the same!" "Cool," replied Leonard, feigning enthusiasm.
"There you go," concluded Sheldon, triumphantly. "313 IS the best the number! Did I lie? Huh!? Huh!"
"OK, I get it," said Leonard with a tinge of exasperation. "313 is the Chuck Norris of numbers."
"Chuck Norris wishes!" Sheldon scoffed. "All 'Chuck Norris' backwards gets you is 'Sirron Kcuhc'." Leonard just sat there, bemused, an all too common occurrence with Sheldon.
Write a program that, for a given set of integers as input, will determine whether or not a number is prime and/or palindromic in base-10 (regular counting numbers) and/or palindromic in base-2 (binary numbers).
Each line of input is a single number to be tested, N (1 <= N <= 10000). There may be any number of lines of input.
Each line of output corresponding to the input shall begin with a three character code indicating whether or not the number being tested has one or more of the following properties, in the order shown:
- P - the number is prime
- T - the number is palindromic in base-10
- B - the number is palindromic in base-2 (binary)
If a number does not possess a certain property, the letter representing that property shall be written in lower-case. For example, the code pTb shall be interpreted to mean "This number is: NOT prime, palindromic in base-10, NOT palindromic in base-2".
Following a single SPACE character, the line of output shall end with the test number represented in base-2 format.
Don't forget to terminate each line with a newline character and judging of your program's output is case sensitive.
313 246 787 2047
PTB 100111001 ptb 11110110 PTb 1100010011 ptB 11111111111
Put your solutions here!