Logic Puzzle: Finding the Appropriate Suffix for a Number

  • Context: High School 
  • Thread starter Thread starter completenewbie
  • Start date Start date
  • Tags Tags
    Logic Puzzle
Click For Summary

Discussion Overview

The discussion revolves around the logic for determining the appropriate suffix for numbers in a programming context, specifically focusing on how to handle exceptions for numbers ending in 11, 12, and 13. Participants explore various logical approaches and programming strategies to implement this functionality.

Discussion Character

  • Exploratory
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant proposes using integer division and modulus to determine suffixes, noting that the logic fails for 11, 12, and 13.
  • Another suggests dividing the number by ten and checking the second digit to apply the "th" suffix for numbers ending in 11, 12, or 13.
  • A participant expresses confusion about the division method and requests clarification.
  • Further clarification is provided, emphasizing the need to check the second digit after dividing by ten.
  • One participant critiques the division method, providing examples to illustrate their misunderstanding of the logic.
  • Another participant offers a refined approach that involves checking the last two digits and applying rules based on their values.
  • A different method is proposed that involves adjusting the number before applying the suffix rules, which one participant finds preferable for its complexity.
  • A participant summarizes their understanding of the logic and expresses gratitude for the guidance received.
  • Another participant shares their own version of the logic, detailing a step-by-step approach to determine the suffix based on modular arithmetic.

Areas of Agreement / Disagreement

Participants present multiple competing views and methods for determining the appropriate suffix, with no consensus reached on a single approach. The discussion remains unresolved regarding the best logic to apply.

Contextual Notes

Some methods rely on specific interpretations of modular arithmetic and may not account for all edge cases. Participants express varying levels of understanding of the proposed logic, indicating potential gaps in clarity.

completenewbie
Messages
5
Reaction score
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?
 
Mathematics news on Phys.org
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.
 
I'm not following the "Take number and divide by then." part of the post. Can you clarify that a little? Thanks.
 
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:
 
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.
 
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. :)
 
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...
 
And the light goes on... ding.



Thanks guys.
 
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:
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 37 ·
2
Replies
37
Views
5K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 24 ·
Replies
24
Views
7K
  • · Replies 40 ·
2
Replies
40
Views
9K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 13 ·
Replies
13
Views
3K