Optimizing Integer Comparison without If Statements

gerald
Messages
13
Reaction score
0
i'm reading a programming book and one of the problems it asks me to solve is to write a program that determines the larger and smaller values of two integers that the user inputs. I could not use if statements in the program.

I checked the answer and they had used this expression to come solve it.

long larger = (a*(a/b) + b*(b/a))/(a/b + b/a);
long smaller = (b*(a/b) + a*(b/a))/(a/b + b/a);

i was wondering how they have gone about finding those expressions? I'm not a math whiz so i was wondering if there was something in my mathematics education that was lacking.
 
Physics news on Phys.org
sorry for the ambiguous title
 
max(a,b)+min(a,b) = a+b
max(a,b)-min(a,b)= |a-b|


solve for an easier answer
 
gerald said:
i'm reading a programming book and one of the problems it asks me to solve is to write a program that determines the larger and smaller values of two integers that the user inputs. I could not use if statements in the program.

I checked the answer and they had used this expression to come solve it.

long larger = (a*(a/b) + b*(b/a))/(a/b + b/a);
long smaller = (b*(a/b) + a*(b/a))/(a/b + b/a);

i was wondering how they have gone about finding those expressions? I'm not a math whiz so i was wondering if there was something in my mathematics education that was lacking.

No there's nothing in your education that is lacking, it's just that writers of programming books have to show how clever they are by inventing silly tricks. This has several problems:

1) When I tried it with negative numbers it didn't work - it may do if you define division using negative integers in a different way, but not with the program I was using.

2) Suppose that you changed your integers to floating point, and your program had this code in. There would be no obvious reason why it needs changing - but suddenly people start finding mysterious errors when running the program.
 
hmm, i should have been more specific. it only wanted positive whole numbers and things I've learned in that chapter. this was a very neat trick.
 
Last edited:
An 'if' statement doesn't take as much computationally (and works with all number), so I'd never use that method if I were you.
 
I asked online questions about Proposition 2.1.1: The answer I got is the following: I have some questions about the answer I got. When the person answering says: ##1.## Is the map ##\mathfrak{q}\mapsto \mathfrak{q} A _\mathfrak{p}## from ##A\setminus \mathfrak{p}\to A_\mathfrak{p}##? But I don't understand what the author meant for the rest of the sentence in mathematical notation: ##2.## In the next statement where the author says: How is ##A\to...
The following are taken from the two sources, 1) from this online page and the book An Introduction to Module Theory by: Ibrahim Assem, Flavio U. Coelho. In the Abelian Categories chapter in the module theory text on page 157, right after presenting IV.2.21 Definition, the authors states "Image and coimage may or may not exist, but if they do, then they are unique up to isomorphism (because so are kernels and cokernels). Also in the reference url page above, the authors present two...
When decomposing a representation ##\rho## of a finite group ##G## into irreducible representations, we can find the number of times the representation contains a particular irrep ##\rho_0## through the character inner product $$ \langle \chi, \chi_0\rangle = \frac{1}{|G|} \sum_{g\in G} \chi(g) \chi_0(g)^*$$ where ##\chi## and ##\chi_0## are the characters of ##\rho## and ##\rho_0##, respectively. Since all group elements in the same conjugacy class have the same characters, this may be...
Back
Top