i want to count the number of zeros in a N-range of numbers starting from 0.
If the N is 100 means,then the range should starts from 0 to 100 then and the count should be 12.
Below code works till some 10^6
private static int calcZeros(String input){
int count = 0;
int n = Integer.parseInt(input);
for(int i = 0;i<=n;i++){
count += String.valueOf(i).replaceAll("[a-zA-Z_1-9]","").trim().length();
}
return count;
}
If the N exceeds 10^6, it hangs,Obiviously, for loop runs for long duration.
My Question here is ,this is right approach to find the zeros count?
Suggest me some fastest way to do the count.
This is (semi)combinatoric problem.
For example imagine 4-digit number :
We start to find all numbers with one zero.
At first position (from left to right), you can use numbers from 1-9, then 1-9, then 1-9 and then there will be 0, so there is no other option.
its 9*9*9 possibilities.
And that zero can be in three positions.
Then we have to find all 4-digit numbers with two zeroes.
We can use Binomial coefficient where (n k) in our case will be : n=number of digits - 1, k = number of zeroes and then you multiply it by 9*9, because thats all possibilities of other numbers.
I have to go now, so I cant finish it for any possible number, but for given number of digits, this shold be working :
public static void main(String[] args) {
System.out.println(numberZeroes(2));
}
public static int numberZeroes(int digits) {
int result = 0;
for (int i = 1; i <= digits - 1; i++) {
result += binomial(digits - 1, i) * Math.pow(9, digits - i);
}
return result+1;
}
public static int binomial(final int N, final int K) {
BigInteger ret = BigInteger.ONE;
for (int k = 0; k < K; k++) {
ret = ret.multiply(BigInteger.valueOf(N - k))
.divide(BigInteger.valueOf(k + 1));
}
return ret.intValue();
}
Instead of using strings and regex, it would be a lot faster to keep dividing by 10 - for example:
int i = 96;
int firstDigit = i / 10;
int secondDigit = i - firstDigit * 10;
For arbitrarily large numbers, you just need to replace that by a while loop.
This algorithm calculates the number of zeros until 1000000000 in 20 seconds. I hope it helps.
int count = 0;
int n = 1000000000;
for (int i = 0; i <= n; i++) {
int m= i;
do {
if (m % 10 == 0)
++count;
m = m / 10;
} while (m > 0);
}
System.out.println("count = " + count);
try this
int n = 1000000;
int count = 0 ;
Pattern pattern = Pattern.compile("0+");
for(int i = 0; i <= n ; i++ ) {
Matcher m = pattern.matcher(String.valueOf(i));
while (m.find()) {
count += m.group().length();
}
}
System.out.println(count);
Related
I am still somewhat of a beginner to Java, but I need help with my code. I wanted to write an Armstrong Number checker.
An Armstrong number is one whose sum of digits raised to the power three equals the number itself. 371, for example, is an Armstrong number because 3^3 + 7^3 + 1^3 = 371.
If I understand this concept correctly, then my code should work fine, but I don't know where I made mistakes. I would appreciate if you could help correct my mistakes, but still kind of stick with my solution to the problem, unless my try is completely wrong or most of it needs to change.
Here is the code:
public class ArmstrongChecker {
boolean confirm = false;
Integer input;
String converter;
int indices;
int result = 1;
void ArmstrongCheck(Integer input) {
this.input = input;
converter = input.toString();
char[] array = converter.toCharArray();
indices = array.length;
result = (int) Math.pow(array[0], indices);
for (int i = 1; i < array.length; i++) {
result = result + (int) Math.pow(array[i], indices);
}
if (result == input) {
confirm = true;
System.out.println(confirm);
} else {
System.out.println(confirm);
}
}
}
For my tries I used '153' as an input. Thank you for your help!
You aren't summing the digits, but the numeric values of the characters representing them. You can convert such a character to its numeric value by subtracting the character '0':
int result = 0;
for(int i = 0; i < array.length; i++) {
result = result + (int) Math.pow(array[i] - '0', indices);
}
Having said that, it's arguably (probably?) more elegant to read the input as an actual int number and iterate its digits by taking the reminder of 10 on each iteration. The number of digits itself can be calculated using a base-10 log.
int temp = input;
int result = 0;
int indices = (int) Math.log10(input) + 1;
while (temp != 0) {
int digit = temp % 10;
result += (int) Math.pow(digit, indices);
temp /= 10;
}
There is a small logical mistake in your code, You're not converting the character to an integer instead you're doing something like
Math.pow('1', 3) -> Math.pow(49, 3) // what you're doing
Math.pow(1, 3) // what should be done
You should first convert the character to the string using any method below
result = (int) Math.pow(array[0],indices);
for(int i = 1;i<array.length;i++) {
result = result + (int) Math.pow(array[i],indices);
}
For converting char to integer
int x = Character.getNumericValue(array[i]);
or
int x = Integer.parseInt(String.valueOf(array[i]));
or
int x = array[i] - '0';
Alternatively
You can also check for Armstrong's number without any conversion, using the logic below
public class Armstrong {
public static void main(String[] args) {
int number = 153, num, rem, res = 0;
num = number;
while (num != 0)
{
rem = num % 10;
res += Math.pow(rem, 3);
num /= 10;
}
if(res == num)
System.out.println("YES");
else
System.out.println("NO");
}
}
For any int >= 0 you can do it like this.
Print all the Armstrong numbers less than 10_000.
for (int i = 0; i < 10_000; i++) {
if (isArmstrong(i)) {
System.out.println(i);
}
}
prints
0
1
2
3
4
5
6
7
8
9
153
370
371
407
1634
8208
9474
The key is to use Math.log10 to compute the number of digits in the candidate number. This must be amended by adding 1. So Math.log10(923) returns 2.965201701025912. Casting to an int and adding 1 would be 3 digits.
The number of digits is then the power used for computation.
Then it's just a matter of summing up the digits raised to that power. The method short circuits and returns false if the sum exceeds the number before all the digits are processed.
public static boolean isArmstrong(int v) {
if (v < 0) {
throw new IllegalArgumentException("Argument must >= 0");
}
int temp = v;
int power = (int)Math.log10(temp)+1;
int sum = 0;
while (temp > 0) {
sum += Math.pow(temp % 10, power);
if (sum > v) {
return false;
}
temp/= 10;
}
return v == sum;
}
I am trying to do this exercise:
Write a program that asks the user for N and M and adds up the
integers between N and M using the formula
SUM(N to M) = SUM( 1 to M ) - SUM( 1 to N-1 )
I can get this to work for positive numbers but not negative numbers.
static int method2(int n, int m) {
int sum = 0;
int sum2 = 0;
for (int i = 1; i <= m; i++) {
sum = sum + i;
}
for (int i = 1; i <= n - 1; i++) {
sum2 = sum2 + i;
}
System.out.println("sum: " + sum + ", sum2: " + sum2);
return sum = sum - sum2;
}
e.g.
using n = -1, m = 1 returns sum = 1.
Using n = -5, m = 5 returns sum = 15.
Using n = 5, m = -5 returns sum = -10.
These should all return 0.
e.g.
Using n = -2, m = 3, returns sum = 6.
Using n = -2, m = 4, returns sum = 10.
The problem is with for (int i = 1; i <= n - 1; i++), specifically i <= n - 1 because when n-1 <= 0 this will not run. I just can't think of a way around it.
Your formula
SUM(N to M) = SUM( 1 to M ) - SUM( 1 to N-1 )
Doesn't really make sense for negative values. If you give that up you can make your program simpler. We very often start for loops at 0 or 1 but that doesn't have to be the case. You could instead start your loop at a n which might be negative:
static int method2(int n, int m) {
int sum = 0;
for (int i = n; i <= m; i++) {
sum = sum + i;
}
System.out.println("sum: " + sum);
return sum;
}
You could always check before if n < 0.
And then do another reverse loop for negative numbers.
e.g.
int sum = 0;
if(m < 0){
for(int i = 0; i >= m; i--) {
sum += i;
}
} else {
for (int i = 1; i <= m; i++) {
sum += i;
}
}
If you really have to use that formula you could use instead of:
for (int i = 1; i <= m; i++) {
the following code which changes the index either by 1 or by -1
for (int i = 1; i <= m; i+=(int)Math.signum(m-1+0.1)) {
(added 0.1 such that in case m is 1 the result is positive and not 0)
Ofc you should do the same for n.
The below code calculates a value which is obtained as sum of square of each digits in str. This sum is again calculate m times.
Example: m = 2, str="123"
At m=1 (1^2)+(2^2)+(3^2) = 1+4+9 = 14
At m=2 (1^2)+(4^2) = 1+16 = 17
So 17 should be the final answer for this input. For large inputs or when run against 1000 of test case like above, this code is giving time limit exceeded errors. Can this code further optimized?
Test case will be less than 1001
1 <= str.length(), m <= 10^9
public static void puzzle(int m,String str) {
int ans = 0;
while (m > 0) {
ans=0;
m--;
int j = 0;
while (j < str.length()) {
int val = Integer.parseInt(str.charAt(j) + "");
ans += val* val;
j++;
}
str = String.valueOf(ans);
}
System.out.println(ans);
}
I have tried my level best and could come up with above iterative solution. Could not improve even with a recursive solution.
Recursive Code:
public static int solve(int m, String n){
if(m < 1)
return Integer.parseInt(n);
int idx = 0;
int res = 0;
while(idx< n.length()){
int val = Integer.parseInt(n.charAt(idx) + "");
res += val*val;
idx++;
}
return solve(m-1,String.valueOf(res));
}
Don't do the operation using strings. Stay with integers.
Since you're just adding the square of digits, you can do it starting with the last digit, so a loop doing modulus and division by 10, until zero, will do the trick.
public static int puzzle(int m, String str) {
int value = Integer.parseInt(str);
for (int i = 0; i < m; i++) {
int sum = 0;
for (; value != 0; value /= 10) {
int digit = value % 10;
sum += digit * digit;
}
value = sum;
}
return value;
}
I have to find number of even number of divisor of given number. For this i tried.I am getting correct output but i am getting time complexity more than required.Question :- First line contains the number of testcases T, followed by T lines each containing an integer N output should be - For each testcase, print the required answer in a single line.How can i reduce complexity for this given code.Or can anyone please suggest more efficient way...
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class TestClass {
public static void main(String args[]) throws Exception {
// Read input from stdin and provide input before running
String frt = "";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
int T = Integer.parseInt(line);
int[] inp = new int[T];
for (int i = 0; i < T; i++) {
int x = Integer.parseInt(br.readLine());
inp[i] = x;
}
int[] ans = new int[T];
int count = 1;
for (int i = 0; i < T; i++) {
int x = inp[i];
if (x % 2 == 0) {
for (int j = 2; j <= x / 2; j = j + 2) {
if (x % j == 0)
count++;
}
} else
count = 0;
ans[i] = count;
}
for (int i = 0; i < T; i++)
System.out.println(ans[i]);
}
}
import java.io.*;
class Ideone
{
public static void main (String[] args) throws Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine());
int i,j,k,n;
int[] inp = new int[T];
for (i = 0; i < T; i++) {
inp[i] = Integer.parseInt(br.readLine());
}
//Find all the primes numbers till the square-root of 10^9.
int MAX, root, arrLen;
MAX=1000000000;
arrLen=(int)Math.sqrt(MAX); // arrLen=31622
boolean[] primes=new boolean[arrLen+2]; // No need to find all the primes numbers till MAX
primes[0]=primes[1]=false;
for(i=2;i<arrLen;i++)
primes[i]=true;
// Using Sieve of Eratosthenes
// Square root of 31622 is 177.8
root=(int)Math.sqrt(arrLen); // root=177
for(i=2;i<=root;i++)
{
if(primes[i])
{
n=i*i;
k=0;
//arrLen is the length of primes array.
for(j=n; j<arrLen; k+=1, j=n+(i*k))
primes[j]=false;
}
}
int[] ans = new int[T];
for( i = 0; i < T; i++) {
n = inp[i];
if(n%2==1)
{
ans[i]=0; // Odd numbers will have 0 even divisors
}
else
{
int[] facts=new int[50];
for(k=0;k<50;k++)
facts[k]=1;
facts[0]=0; // fact[0] will contain the highest power of 2 that divides n.
while(n%2==0)
{
facts[0]+=1;
n=n/2;
}
// Prime factorizing n
j=1;
for( k=3; k<arrLen; k+=2)
{
if(primes[k] && n%k==0)
{
while(n%k==0)
{
facts[j]+=1;
n=n/k;
}
j+=1;
}
if(n==1) // To check if n has been completely divided or not.
break;
}
if(n!=1) // To check if there is any prime factor greater than the square root of MAX.
{
facts[j]+=1;
j+=1;
}
int count=1;
for(k=0;k<j;k++)
count=count*facts[k];
ans[i]=count;
}
}
for ( i = 0; i < T; i++)
System.out.println(ans[i]);
}
}
I am of the feeling that this question might have been posted on any competitive coding platform, probably like HackerEarth. If so, then please don't post direct questions on StackOverFlow(in my opinion).
Anyways, I have tested my code and it runs correctly.
In questions where you are not able to reduce time complexity, first make sure that unnecessary objects are not created. Object creation in the memory is a time consuming operation. Avoid irrelevant creation of object and variables. The code above can still be optimized, but that will reduce it's readability. :)
Also before approaching a problem, try to figure out various test cases. Like odd numbers will have 0 even divisors. So by checking whether a number is odd, can reduce several operations.
A bit more explanation to the above code:
Number Of Divisor of A Number are: (N1+1)(N2+1)(N3+1).... Where N1,N2,N3 etc are Powers Of Prime Multiples of the number.
Now if N1 is for 2(the only even prime number),
then Number of Even Divisors of the Number are: N1*(N2+1)*(N3+1)...
In the facts[] array, facts[0] corresponds to N1, while N2, N3 etc are stored in facts[1],facts[2], etc.
facts[0] is initialized with 0, while others are initialized with 1.
The count stores the final product: N1*(N2+1)*(N3+1)... which is equal to the number of Even divisors of the original number.
There is a very simple trick for this,first compute the prime factorization of 720,which is 2^4×3^2×5,the total number of factors here is 3x2×5=30, and number of odd factors (number of factors of the odd primes)=3×2=6,subtracting gives number of even factors = 24.This method works for any number.
NOTE: If the number has no odd factors i.e,the prime factorization is of the form 2a,then the number of number of even factors is a and number of odd factors is 1.
My solution, shorter and simpler:
public class Solution {
public static int numberOfEvenDivisors(int n) {
if (n % 2 == 1)
return 0;
int evenDivisors = 0;
for (int i = 1; i < Math.sqrt(n) + 1; i++) {
if ((i % 2 == 0) && (n % i == 0))
evenDivisors++;
if ((n % (n / i) == 0) && (n / i) % 2 == 0)
evenDivisors++;
if ((i * i == n) && (i % 2 == 0))
evenDivisors--;
}
return evenDivisors;
}
public static void main(String[] args) {
//test here
}
}
Here is short explanation:
When n is odd we return 0, there are no even divisors.
In the first if we check whether i is a divisor and whether i is even. When yes, wo do increment the divisors counter. Note that we start with 1. This is necessary because here we know that n is even and n is its own divisor.
In second if we increment divisors counter if n / i is even and i is a divisor. If i is even, we counted it already in first if. And we know that (n/i) >= i, because we count i only up to square root of n.
And in the last, third if we check the case whether i is a even divisor that is square root of n (for example i == 4, n == 16). Here we decrement number of divisors because we do not want to count same divisor twice. That is.
P.S. We assume n >= 1.
This for loop
for (int i = 0; i < T; i++) {
int x = inp[i];
if (x % 2 == 0) {
for (int j = 2; j <= x / 2; j = j + 2) {
if (x % j == 0)
count++;
} else
count = 0;
ans[i] = count;
}
could be changed to
for (int i = 0; i < T; i+=2) {
int x = inp[i];
for (int j = 2; j <= x / 2; j = j + 2) {
if (x % j == 0)
count++;
if(count != 1)
count = 0;
ans[i] = count;
}
so that it will make the for loop run half as many times.
public class Test {
static int nthDigit(int number, int n)
{
int digit = 0;
for (int i = 0; i <= n; i++) { // Assumes n is >= 0
digit = number % 10;
number /= 10;
}
return digit;
}
public static void main(String args[]) {
System.out.println(nthDigit(2314, 4));
}
}
Now output of this program is 0
When I perform unit testing it first returns 4 then 1,3,2 respectively.....
I don't know where I have done the mistake is there any other way to get the output 4,1,3,2
respectively....
You can also do:
return new Integer(String.valueOf(number).substring(n-1,n));
The error in your code is in the return statement/ or your use of it.
digit = number % 10;
If what you want is for the string 4 3 2 1 to be written to the output stream, then you probably want to do this
static String nthDigit(int number, int n)
{
String digit = 0;
for (int i = 0; i <= n; i++) { // Assumes n is >= 0
digit += (number % 10).ToString();
number /= 10;
}
return digit;
}
Notice I changed the return type of the method, and I changed the operator used to populate digit from '=' to '+=' to ensure all values are captured.
If it is the case that you instead want the sum of the values returned, then you would change your code like so
static int nthDigit(int number, int n)
{
int digit = 0;
for (int i = 0; i <= n; i++) { // Assumes n is >= 0
**digit += number % 10;**
number /= 10;
}
return digit;
}
I revert the int return type back to what you originally had, but I keep the operator change I had made earlier.
static int nthDigit(int number, int n)
{
n = String.valueOf(number).length() - n;
int digit = 0;
for (int i = 0; i <= n; i++) {
digit = number % 10;
number /= 10;
}
return digit;
}
public static void main(String args[]) {
System.out.println(nthDigit(2314, 1)); // 2
System.out.println(nthDigit(2314, 2)); // 3
System.out.println(nthDigit(2314, 3)); // 1
System.out.println(nthDigit(2314, 4)); // 4
}
What I did was instead of starting with n as the nth digit, I had it start at the opposite side of the number to it. So instead of the 4th digit, I would have used the 1st digit. To do that, I subtracted n from the length of the number (that's the complement of it, if you've ever taken a probability course).