0
$\begingroup$

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) 
  • 0
    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.2012-03-15

2 Answers 2

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

  • 0
    @KVRaman Why do you delete a message that was here? Yes you have the right to, but not when it will outrightly make the next comment pointless!2012-03-30
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.

  • 0
    @Downvoter Care to explain.2012-03-30