September 12th, 2017, 06: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 shorthand 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 10:54 PM. 
September 12th, 2017, 11:10 PM  #2 
Global Moderator Joined: Dec 2006 Posts: 19,059 Thanks: 1618 
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.
Last edited by skipjack; September 12th, 2017 at 11:47 PM. 
September 12th, 2017, 11:11 PM  #3 
Senior Member Joined: Sep 2015 From: USA Posts: 1,977 Thanks: 1026 
if I were 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 its first digit. $\left \lfloor x \right \rfloor$ is the integer part of $x$ Clipboard01.jpg Last edited by skipjack; January 19th, 2018 at 03:14 PM. 
September 13th, 2017, 04:38 AM  #4 
Senior Member Joined: May 2016 From: USA Posts: 1,038 Thanks: 423 
$\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 nonmath people. Last edited by skipjack; January 19th, 2018 at 03:15 PM. 
September 13th, 2017, 08:43 AM  #5  
Senior Member Joined: Sep 2015 From: USA Posts: 1,977 Thanks: 1026  Quote:
Consider $i=64$ $\left \lfloor \log_{10}(i) \right \rfloor = 1$ $64  10^1 = 54 \neq 4$ Last edited by skipjack; January 19th, 2018 at 03:17 PM.  
September 13th, 2017, 04:35 PM  #6 
Senior Member Joined: May 2016 From: USA Posts: 1,038 Thanks: 423 
Romsek I think you are correct that I screwed up. I'll review further when I am not so busy. 
September 14th, 2017, 10:08 PM  #7 
Senior Member Joined: May 2016 From: USA Posts: 1,038 Thanks: 423 
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 nonnegative 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. 
January 18th, 2018, 07:25 AM  #8 
Newbie Joined: Jan 2018 From: somewhere Posts: 14 Thanks: 2 Math Focus: Algebraic Number Theory / Differential Fork Theory  Actually it doesn't. It gives 10 or something approximately ten depending on whether or not one of those is integer division.

January 19th, 2018, 01:09 PM  #9 
Member Joined: Jan 2016 From: Athens, OH Posts: 89 Thanks: 47 
I just noticed the date of the OP was last September, so this won't help the OP. Maybe it will be of interest to someone. romsek (reply 2) has given you the correct formula. Here is an explanation of that formula and a Java program implementing said formula. Here's a Java program that implements the formula. Notice there are 2 methods that find $10^k$. One method uses floating point calculations, but the other uses all int operations. Generally, for an integer problem, it is best to try and avoid floating point since such numbers are approximations. For this problem, the floating point method is fine, but you should be aware that floating point approximations can introduce errors. Code: package javageneric; import java.util.Scanner; public class Javageneric { public static void main(String[] args) { ModExercise test = new ModExercise(); Scanner sc = new Scanner(System.in); int n = 1; while (n > 0) { System.out.println("Enter a positive integer (0 to quit):"); n = sc.nextInt(); if (n > 0) { System.out.println(test.removeLeadingDigitOf(n)); } } } } package javageneric; public class ModExercise { // returns 10^k where 10^k <= n < 10^(k+1); i.e. 10^floor(log10(n)) int tenToTheK(int n) { // n MUST be positive double lg = Math.log10(n); double flg = Math.floor(lg); double p = Math.pow(10, flg); return ((int) p); } // same return as tenToTheK but uses no floating point arithmetic int tenToTheK0(int n) { int p = 1; while (n >= 10) { // n should be positive p *= 10; n /= 10; } return (p); } int removeLeadingDigitOf(int n) { int tenK = tenToTheK0(n); return (n % tenK); } } 
January 22nd, 2018, 12:07 AM  #10 
Senior Member Joined: Apr 2014 From: UK Posts: 871 Thanks: 316 
Post #2 gets my vote, it's simple to understand and easy for a cpu to do quickly. However, since the input will be a string before it is converted, it may not be such a bad idea to keep the number as a string and chop off the left digit and remove any leading 0's. It won't be as cpu efficient, but it is simple. 

Tags 
challenge problem, equation, java, math, weird 
Thread Tools  
Display Modes  

Similar Threads  
Thread  Thread Starter  Forum  Replies  Last Post 
Weird correlation. Need equation.  OTheB  Math  4  July 19th, 2016 12:16 AM 
Java  gaussrelatz  Computer Science  3  April 6th, 2014 08:33 AM 
weird differential equation  helloprajna  Differential Equations  2  July 16th, 2013 11:25 PM 
java problem  cire09  Computer Science  1  October 9th, 2010 05:54 AM 
Java versus C++  johnny  Computer Science  17  October 21st, 2007 07:12 PM 