Logic Puzzle: Finding the Appropriate Suffix for a Number

In summary, you can return a number with a suffix by using this logic: if the number is between 1 and 10, divide it by 10 and then add the suffix "th" if the number is 11, 12, or 13.
  • #1
completenewbie
5
0
Hi, its my first post, so don't be too hard on me. Here's my dilemma:

I'm trying to write a computer program, to return a number with it's apropriate suffix. (ie: 1=1st, 2=2nd, 3=3rd, etc.) Forget the programming aspect for a minute, all I'm interested in is the logic. If X=answer, I can integer divide (mod) X by 10 and I get a number between 0 and 9 (ie: 21mod10=1, 33mod10=3, 40mod10=0, etc.). Then I can assign 1= st, 2=nd, 3=rd, and everything else=th. The logic works except for 11, 12, and 13. Using this logic, I get 11st, 12nd, and 13rd. How can I modify my logic to make it work?
 
Physics news on Phys.org
  • #2
Take the number and divide it by then. If the {answer} mod 10 equals 1, you need to give the number "th". Otherwise, follow the logic you posted above.
 
  • #3
I'm not following the "Take number and divide by then." part of the post. Can you clarify that a little? Thanks.
 
  • #4
Originally posted by completenewbie
I'm not following the "Take number and divide by then." part of the post. Can you clarify that a little? Thanks.
I'm sorry, I meant to say divide it by ten. :smile:
 
  • #5
That theory doesn't seem to make sense to me:
If X=11 Then X/10=1.1
If X=111 Then X/10=11
If X=33 Then X/10=3.3
If X=333 Then X/10=33

I guess I'm not following you, can you explain that agian.
 
  • #6
You need to take the number and divide it by ten. You then need to divide it again by 10, and if the remainder (mod) is 1 it means that the 2nd digit of the number is 1, so you need to use the "th" suffix. For any other rules, the normal rules (in your first post) apply.

In PHP this would like this:
PHP:
function get_suffix($number) {
	if (($number / 10) % 10 == 1) {
		return 'th';
	} else {
		switch ($number % 10) {
			case 1:
				return 'st';
				break;
			case 2:
				return 'nd';
				break;
			case 3:
				return 'rd';
				break;
			default:
				return 'th';
				break;
		}
	}
}
You can make it even more efficient (but less readable):
PHP:
function get_suffix($number) {
	if (($number / 10) % 10 != 1) {
		switch ($number % 10) {
			case 1:
				return 'st';
			case 2:
				return 'nd';
			case 3:
				return 'rd';
		}
	}
	return 'th';
}
But I'm not here to teach PHP. :)
 
  • #7
Here's one way to do it:

- X = X mod 100
- if X > 3 and X < 21 then make it 'th'
- else apply your rule

Here's another:

- X = (X + 90) mod 100
- if X < 4 then X = X + 4
- apply your rule

I like the second one better - it illustrates how computers can do trivial things in very confusing ways...
 
  • #8
And the light goes on... ding.



Thanks guys.
 
  • #9
I could also do this:

A=answer
X=Amod100
Y=Amod10
if X=11 or X=12 or X=13 then add "th"
if Y>3 then add "th"
if Y=0 then add "th"
if Y=1 then add "st"
if Y=2 then add "nd"
if Y=3 then add "rd"

The actual code will look different, but you get the idea. Thanks for steering me in the right direction.:smile:
 

1. What is a logic puzzle?

A logic puzzle is a type of problem that requires critical thinking and deduction to solve. It typically involves a set of clues or conditions that must be used to determine a solution.

2. What is the purpose of the "Logic Puzzle: Finding the Appropriate Suffix for a Number"?

The purpose of this logic puzzle is to practice problem-solving skills and to determine the correct suffix to use when writing numbers in different contexts.

3. How do I solve the "Logic Puzzle: Finding the Appropriate Suffix for a Number"?

To solve this puzzle, you must carefully read and analyze the given clues and use deductive reasoning to determine the correct suffix for each number. It may be helpful to make a chart or list to keep track of your deductions.

4. Can I use any mathematical formulas to solve this puzzle?

No, this puzzle does not require any mathematical formulas. It is purely a logic puzzle that requires critical thinking and deduction.

5. Are there any tips for solving this puzzle?

Yes, some tips for solving this puzzle include carefully reading and re-reading the clues, making a chart or list to track your deductions, and starting with the clues that provide the most information. It may also be helpful to eliminate incorrect suffixes as you go along.

Similar threads

  • General Discussion
Replies
11
Views
921
Replies
2
Views
1K
  • Programming and Computer Science
2
Replies
37
Views
3K
  • Set Theory, Logic, Probability, Statistics
Replies
6
Views
335
  • Set Theory, Logic, Probability, Statistics
2
Replies
40
Views
6K
Replies
4
Views
218
  • Engineering and Comp Sci Homework Help
Replies
32
Views
3K
  • Precalculus Mathematics Homework Help
Replies
4
Views
3K
  • Programming and Computer Science
Replies
1
Views
678
Back
Top