0

I have an n-digit number, say X. Now, what-is-the/how-can-I-come-up-with-an equation [or function] that I should use to get the first n/2-digit number and second n/2-digit numbers from the n-digit number?

For example:

X = 1234 ; n = 4
a = 12 (first n/2 digits)
b = 34 (second n/2 digits)

X = 56789 ; n = 5
a = 567 (first n/2 digits - ceiling)
b = 89 (second n/2 digits)
  • You should post it on http://scicomp.stackexchange.com/ (if you are looking for an algorithm that helps in writing a program) – Kirthi Raman Mar 15 '12 at 11:26
  • Well, I can write one in C++ as this was one of my very first programming exercises, before I really gave up. It is not particularly hard to put this into a program if you know what the syntax is. But, yes I think this site is not well-suited for more technical questions about coding and algorithms. –  Mar 15 '12 at 11:28

2 Answers2

4

when $n=1234$, for instance $x = \lceil \log_{10}(n) \rceil = 4$, also what if the number were $4321$ instead of $1234$ ? How would you use this $x$ to get the first portion you are interested in?

$ \displaystyle{\left\lceil \frac{n}{10^{x/2}} \right\rceil} $ gives you the first portion if $x$ here were even, and if $x$ were odd then $ \displaystyle{\left\lceil \frac{n}{10^{(x+1)/2}} \right\rceil} $ gives you the first portion

Use similar logic to explore the second half of what you want.

(Check Wolfram Alpha to get to understand floor and ceil functions better) http://tinyurl.com/6tvhq9s

Kirthi Raman
  • 7,564
  • 2
  • 36
  • 60
2

It would be the quotient and remainder when divided by $k$ where

$$k=\begin{cases} \dfrac n 2, \mbox{$n$ is even} \\\\ \dfrac{n+1}{2}, \mbox{$n$ is odd}\end{cases}$$

If you're writing a computer program, you may also want to determine $n$ by having a loop tell you when the remainder by $10^n$ is no more an integer.