Why it is not working for 4 digit numbers? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
23rd Apr 2021, 11:41 AM
Atul [Inactive]
13 Answers
+ 2
public class Program { static long function(long a){ long one=a*2; long two=a*3; int f=0; String concat =""+a+one; long i=(long)Long.parseLong(concat+two); while(i>0){ long d=i%10; for(int j=1;j<=9;j++){ if(j==(d)){ ++f; } } i/=10; } return f;} public static void main(String[] args) { long a=2019; System.out.println ((long)function(a)); if(function(a)==9){ System.out.println ("yes"); } else{ System.out.println ("no"); } } } //JaScript getting correct output through this
23rd Apr 2021, 1:54 PM
Atul [Inactive]
+ 2
// the parse methode do not work with addition of string and integer String concat =""+a+one; int ico=Integer.parseInt(concat); int i = ico + two;
23rd Apr 2021, 12:05 PM
JaScript
JaScript - avatar
+ 2
@JaScript "// the parse methode do not work with addition of string and integer " It works. Just do and see int two = 3; int i = Integer.parseInt(concat + two);
23rd Apr 2021, 1:02 PM
A͢J
A͢J - avatar
+ 2
🅰🅹 (Challenge Accepted) ok, interesting found. I tried this code now with long instead int and got the same error. Why?
23rd Apr 2021, 1:43 PM
JaScript
JaScript - avatar
+ 1
Because your string converted in more than 10 character for example "201940386057" which is beyond the size of Integer.
23rd Apr 2021, 12:58 PM
A͢J
A͢J - avatar
+ 1
🅰🅹 (Challenge Accepted) what can be done to avoid the error?
23rd Apr 2021, 1:23 PM
Atul [Inactive]
+ 1
JaScript I too used long instead of int . I typecasted the integers too but it's showing different errors too
23rd Apr 2021, 1:51 PM
Atul [Inactive]
+ 1
Atul In your above edited code, you are printing function (a) directly as well as comparing function (a) with 9 so here you are calling same method two time which will take two time processing to get same value. So instead of doing this, just do long num = function (a); System.out.println(num); if (num == 9) { System.out.println("Yes"); }
23rd Apr 2021, 3:44 PM
A͢J
A͢J - avatar
+ 1
Oh you mean the time complexity will be O(log n)², or greater than that for the time complexity?
23rd Apr 2021, 3:49 PM
Atul [Inactive]
0
It is showing no for that
23rd Apr 2021, 12:21 PM
Atul [Inactive]
0
Atul No need to convert all values in Long, just parse string value in long. And also don't print method, just store in a variable and use that variable (to avoid two time processing) https://code.sololearn.com/c6Uyn01XFe77/?ref=app
23rd Apr 2021, 2:24 PM
A͢J
A͢J - avatar
0
🅰🅹 (Challenge Accepted) What is two time processing?
23rd Apr 2021, 2:59 PM
Atul [Inactive]
0
Atul Yes
23rd Apr 2021, 4:12 PM
A͢J
A͢J - avatar