My Math Forum  

Go Back   My Math Forum > Science Forums > Computer Science

Computer Science Computer Science Forum

Thanks Tree1Thanks
LinkBack Thread Tools Display Modes
January 23rd, 2018, 03:56 PM   #11
Joined: Jan 2018
From: somewhere

Posts: 14
Thanks: 2

Math Focus: Algebraic Number Theory / Differential Fork Theory
Originally Posted by weirddave View Post
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.
2 doesnt even work. It needs to be rethought.
Typhon is offline  
January 24th, 2018, 12:42 AM   #12
Senior Member
Joined: Apr 2014
From: UK

Posts: 800
Thanks: 297

Yeah, you're right, I don't think I'm the only one who somehow missed that! You'd have to keep dividing by 10 until you get the last (first) digit then multiply that digit by 10^n where n is the number of divide by 10 you did to get the digit.
Clearly, the original x/(x/10) always gives 10.
So, in the original example 685757:
int(685757/10/10/10/10/10) = 6
6*100000 = 600000
685757 - 600000 = 85757

a simple while loop would do this quickly.
Alternatively, it may be quicker to pull out the leftmost character and convert it to an integer then mutiply by 10^("length of string"-1)

The original code posted looks like it might work fine, I just wonder how slow it is.
weirddave is offline  
January 24th, 2018, 09:04 AM   #13
Joined: Jan 2016
From: Athens, OH

Posts: 79
Thanks: 39

The Java code as posted by the OP is not quite correct. If the input is a single digit positive integer, his code generates a NumberFormatError. This can be easily fixed by adding a check to see if the input is such a single digit.

The problem is so small that any correct solution will execute very quickly. The only way efficiency could become an issue is if the algorithm is executed numerous times. I can' t think of any possible problem that would need this.
johng40 is offline  
January 26th, 2018, 02:44 AM   #14
Senior Member
Joined: Apr 2014
From: UK

Posts: 800
Thanks: 297

It might slow down (relatively!) if a very large number is used. I agree that as the problem stands, it will be fast enough for human interaction. If the input was replaced by a huge data set then it might prove to be a bit sluggish, but then, I wouldn't use Java for that
weirddave is offline  

  My Math Forum > Science Forums > Computer Science

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 01:16 AM
Java gaussrelatz Computer Science 3 April 6th, 2014 09:33 AM
weird differential equation helloprajna Differential Equations 2 July 17th, 2013 12:25 AM
java problem cire09 Computer Science 1 October 9th, 2010 06:54 AM
Java versus C++ johnny Computer Science 17 October 21st, 2007 08:12 PM

Copyright © 2018 My Math Forum. All rights reserved.