My Math Forum A weird equation? Java

 Computer Science Computer Science Forum

 September 12th, 2017, 07:19 PM #1 Newbie   Joined: Sep 2017 From: Somewhere Posts: 1 Thanks: 0 A weird equation? Java So, I was given a 'challenge' by my professor to write a code that does something along these lines: - Take user input (int) (Any positive int) - return/print that value minus the first digit. - ex. input = 145, return/print = 45. input = 100, return/print = 0. I accomplished it originally by converting from int to string, then using a .substring to declare the new starting position: ex. int 145 = "145", .substring (1(//instead of 0)); Once I had declared the new starting position, I Integer.parseInt'd it to turn it back into an int. This solved the issue of entering 100, 1000, 10000, etc and returning more than one zero. I used short-hand coding terms, but the full code is here: https://code.sololearn.com/cFHa430To2Ik/#java Now that I've accomplished the task through translations (int to str and str to int), he has asked me to do it the 'mathy' way. Which leads me to believe there's an equation or equations that can help me accomplish the same thing. Any guru's out there than can shed some light on this? Math is definitely not my strong suit. To clarify: this is not a graded assignment, just something he's throwing at us to 'challenge' us. Seems to be working! Thanks! TJ Last edited by skipjack; September 12th, 2017 at 11:54 PM.
 September 13th, 2017, 12:10 AM #2 Global Moderator   Joined: Dec 2006 Posts: 18,142 Thanks: 1417 If the number is 685757, 685757/(685757/10) gives 6, the first digit. There are various ways to determine the number of digits in 685757, so that you can subtract the place value, 600000, rather than just 6, to produce 85757. Thanks from topsquark Last edited by skipjack; September 13th, 2017 at 12:47 AM.
September 13th, 2017, 12:11 AM   #3
Senior Member

Joined: Sep 2015
From: Southern California, USA

Posts: 1,601
Thanks: 816

if i was given a random positive number $d$ and had to remove the first digit I'd do

$\large \tilde{d}=d \mod{10^{\left \lfloor \log_{10}(d) \right \rfloor}}$

where $\large d$ is your input number and $\large \tilde{d}$ is that number without it's first digit.

$\left \lfloor x \right \rfloor$ is the integer part of $x$

Attached Images
 Clipboard01.jpg (43.7 KB, 23 views)

Last edited by romsek; September 13th, 2017 at 12:14 AM.

 September 13th, 2017, 05:38 AM #4 Senior Member   Joined: May 2016 From: USA Posts: 823 Thanks: 335 $\text {Let input integer } = i.$ $\text {Let desired output } = x.$ $\text {Let } u = \left \lfloor log_{10}(i) \right \rfloor.$ $x = i - 10^u.$ That is a very simple "mathy" way. Example $i = 145.$ $log_{10}(145) \approx 2.16.$ $2 = \lfloor 2.45 \rfloor.$ $10^2 = 100.$ $145 - 100 = 45.$ EDIT: This method is logically identical to romsek's, but differs slightly in notation and perhaps in ease of comprehension for non-math people. Last edited by JeffM1; September 13th, 2017 at 05:50 AM.
September 13th, 2017, 09:43 AM   #5
Senior Member

Joined: Sep 2015
From: Southern California, USA

Posts: 1,601
Thanks: 816

Quote:
 Originally Posted by JeffM1 $\text {Let input integer } = i.$ $\text {Let desired output } = x.$ $\text {Let } u = \left \lfloor log_{10}(i) \right \rfloor.$ $x = i - 10^u.$ That is a very simple "mathy" way. Example $i = 145.$ $log_{10}(145) \approx 2.16.$ $2 = \lfloor 2.45 \rfloor.$ $10^2 = 100.$ $145 - 100 = 45.$ EDIT: This method is logically identical to romsek's, but differs slightly in notation and perhaps in ease of comprehension for non-math people.
I don't think this works unless the leading digit is $1$.

Consider $i=64$

$\left \lfloor \log_{10}(i) \right \rfloor = 1$

$64 - 10^1 = 54 \neq 4$

 September 13th, 2017, 05:35 PM #6 Senior Member   Joined: May 2016 From: USA Posts: 823 Thanks: 335 Romsek I think you are correct that I screwed up. I'll review further when I am not so busy.
 September 14th, 2017, 11:08 PM #7 Senior Member   Joined: May 2016 From: USA Posts: 823 Thanks: 335 Romsek was quite right that my first post was incorrect because too simple. I apologize. The problem was: given a positive integer i expressed in decimal notation, find the non-negative integer represented by the number equal to i without its leftmost digit. One way to proceed is to define: $i = j * 10^k + n \text {, where } j,\ k,\ n \in \mathbb Z,\ 1 \le j \le 9,\ 0 \le k \text {, and } 0 \le n < 10^k.$ Note that j, k, and n are uniquely defined given i. $\text {Define: } u = log_{10}(i),\ v = \lfloor u \rfloor ,\ w = u - v \text {, and } x = \left \lfloor 10^w \right \rfloor.$ Note that u, v, w, and x are functions of i, not of j, k, or n. $n = i - x * 10^v.$ So n is a function of i. All that is left is to prove that formula. $\text {Line 1: } 1 \le j \implies 10^k \le j * 10^k.$ $\text {Line 2: } j \le 9 \implies j + 1 \le 10 \implies (j + 1) * 10^k \le 10^{(k+1)}.$ $\text {Line 3: } 0 \le n < 10^k \implies j * 10^k + 0 \le j * 10^k + n < j * 10^k + 10^k = (j + 1) * 10^k \implies$ $\text {Line 4: } j * 10^k \le i < (j + 1) * 10^k\ \because \ i = j * 10^k + n.$ $\text {Line 5: } 10^k \le i < 10^{(k+1)} \text { by lines 1, 2, and 4.}$ $\text {Line 6: } \therefore k \le log_{10}(i) < k + 1 \implies k \le u < k + 1.$ $\text {Line 7: }\therefore k = \lfloor u \rfloor \ \because \ 0 \le k \in \mathbb Z.$ $\text {Line 8: } \therefore k = v\ \because \ v = \lfloor u \rfloor \text { by definition.}$ $\text {Line 9: By line 8 and definitions of u and w, } w = u - v = u - k = log_{10}(i) - k \implies$ $\text {Line 10: } w = log_{10}(i) - log_{10} \left (10^k \right ) = log_{10} \left ( \dfrac{i}{10^k} \right ).$ $\text {Line 11: } \therefore \text { by line 4, } j * 10^k \le i < (j + 1) * 10^k \implies j \le \dfrac{i}{10^k} < j + 1.$ $\text {Line 12: By lines 10 and 11, } log_{10}(j) \le w < log_{10}(j + 1).$ $\text {Line 13: } \therefore 10^{log_{10}(j)} \le 10^w < 10^{log_{10}(j + 1)} \implies j \le 10^w < j + 1.$ $\text {Line 14: }\therefore j = \left \lfloor 10^w \right \rfloor \ \because \ 1 \le j \in \mathbb Z.$ $\text {Line 15: But } x = \left \lfloor 10^w \right \rfloor \implies j = x.$ $\text {Line 16: } i = j * 10^k + n \implies n = i - j * 10^k = i - x * 10^v$ $\text {because } k = v \text { by line 7, and } j = x \text { by line 15.}$ Let's see how it works with romsek's example of i = 64. By inspection n = 4. $u = log_{10}(64) \approx 1.806$ $v = \lfloor u \rfloor = 1.$ $w \approx 0.806 \implies 10^w \approx 6.397 \implies x = 6.$ $i - x * 10^v = 64 - 6 * 10^1 = 64 - 60 = 4.$ From a coding perspective, all that would remain is to see whether the limits on the accuracy of floating point numbers and of the system's log function required some tweaking to the process.

 Tags challenge problem, equation, java, math, weird

 Thread Tools Display Modes Linear Mode

 Similar Threads Thread Thread Starter Forum Replies Last Post OTheB Math 4 July 19th, 2016 01:16 AM gaussrelatz Computer Science 3 April 6th, 2014 09:33 AM helloprajna Differential Equations 2 July 17th, 2013 12:25 AM cire09 Computer Science 1 October 9th, 2010 06:54 AM johnny Computer Science 17 October 21st, 2007 08:12 PM

 Contact - Home - Forums - Cryptocurrency Forum - Top