mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-25 13:34:54 +08:00
Merge remote-tracking branch 'upstream/master'
This commit is contained in:
55
Others/BrianKernighanAlgorithm.java
Normal file
55
Others/BrianKernighanAlgorithm.java
Normal file
@ -0,0 +1,55 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author Nishita Aggarwal
|
||||
*
|
||||
* Brian Kernighan’s Algorithm
|
||||
*
|
||||
* algorithm to count the number of set bits in a given number
|
||||
*
|
||||
* Subtraction of 1 from a number toggles all the bits (from right to left) till the rightmost set bit(including the
|
||||
* rightmost set bit).
|
||||
* So if we subtract a number by 1 and do bitwise & with itself i.e. (n & (n-1)), we unset the rightmost set bit.
|
||||
*
|
||||
* If we do n & (n-1) in a loop and count the no of times loop executes we get the set bit count.
|
||||
*
|
||||
*
|
||||
* Time Complexity: O(logn)
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
public class BrianKernighanAlgorithm {
|
||||
|
||||
/**
|
||||
* @param num: number in which we count the set bits
|
||||
*
|
||||
* @return int: Number of set bits
|
||||
* */
|
||||
static int countSetBits(int num)
|
||||
{
|
||||
int cnt = 0;
|
||||
while(num != 0)
|
||||
{
|
||||
num = num & (num-1);
|
||||
cnt++;
|
||||
}
|
||||
return cnt;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param args : command line arguments
|
||||
*
|
||||
*/
|
||||
public static void main(String args[])
|
||||
{
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int num = sc.nextInt();
|
||||
int setBitCount = countSetBits(num);
|
||||
System.out.println(setBitCount);
|
||||
sc.close();
|
||||
}
|
||||
}
|
@ -9,7 +9,7 @@ import java.util.Arrays;
|
||||
import java.util.Scanner;
|
||||
import java.util.Stack;
|
||||
|
||||
public class Solution {
|
||||
public class Dijkshtra {
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
Scanner in =new Scanner(System.in);
|
||||
|
@ -9,7 +9,7 @@ public class FibToN {
|
||||
// print fibonacci sequence less than N
|
||||
int first = 0, second = 1;
|
||||
//first fibo and second fibonacci are 0 and 1 respectively
|
||||
|
||||
scn.close();
|
||||
while(first <= N){
|
||||
//print first fibo 0 then add second fibo into it while updating second as well
|
||||
|
||||
|
@ -1,45 +0,0 @@
|
||||
/**
|
||||
* The Sieve of Eratosthenes is an algorithm use to find prime numbers,
|
||||
* up to a given value.
|
||||
* Illustration: https://upload.wikimedia.org/wikipedia/commons/b/b9/Sieve_of_Eratosthenes_animation.gif
|
||||
* (This illustration is also in the github repository)
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
public class FindingPrimes{
|
||||
/**
|
||||
* The Main method
|
||||
*
|
||||
* @param args Command line arguments
|
||||
*/
|
||||
public static void main(String args[]){
|
||||
SOE(20); //Example: Finds all the primes up to 20
|
||||
}
|
||||
|
||||
/**
|
||||
* The method implementing the Sieve of Eratosthenes
|
||||
*
|
||||
* @param n Number to perform SOE on
|
||||
*/
|
||||
public static void SOE(int n){
|
||||
boolean sieve[] = new boolean[n];
|
||||
|
||||
int check = (int)Math.round(Math.sqrt(n)); //No need to check for multiples past the square root of n
|
||||
|
||||
sieve[0] = false;
|
||||
sieve[1] = false;
|
||||
for(int i = 2; i < n; i++)
|
||||
sieve[i] = true; //Set every index to true except index 0 and 1
|
||||
|
||||
for(int i = 2; i< check; i++){
|
||||
if(sieve[i]==true) //If i is a prime
|
||||
for(int j = i+i; j < n; j+=i) //Step through the array in increments of i(the multiples of the prime)
|
||||
sieve[j] = false; //Set every multiple of i to false
|
||||
}
|
||||
for(int i = 0; i< n; i++){
|
||||
if(sieve[i]==true)
|
||||
System.out.print(i+" "); //In this example it will print 2 3 5 7 11 13 17 19
|
||||
}
|
||||
}
|
||||
}
|
@ -6,7 +6,7 @@ class FloydTriangle {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
System.out.println("Enter the number of rows which you want in your Floyd Triangle: ");
|
||||
int r = sc.nextInt(), n = 0;
|
||||
|
||||
sc.close();
|
||||
for(int i=0; i < r; i++) {
|
||||
for(int j=0; j <= i; j++) {
|
||||
System.out.print(++n + " ");
|
||||
|
@ -1,5 +1,5 @@
|
||||
import java.util.*;
|
||||
public class Array {
|
||||
public class InsertDeleteInArray {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner s = new Scanner(System.in); // Input statement
|
||||
|
@ -4,7 +4,7 @@ import java.text.*;
|
||||
import java.math.*;
|
||||
import java.util.regex.*;
|
||||
|
||||
public class Solution {
|
||||
public class RootPrecision {
|
||||
|
||||
public static void main(String[] args) {
|
||||
//take input
|
||||
|
@ -1,6 +1,6 @@
|
||||
import java.util.*;
|
||||
|
||||
public class Postfix {
|
||||
public class StackPostfixNotation {
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
String post = scanner.nextLine(); // Takes input with spaces in between eg. "1 21 +"
|
||||
|
@ -7,7 +7,7 @@ import java.util.Scanner;
|
||||
* @author Marcus
|
||||
*
|
||||
*/
|
||||
class CountTheWords{
|
||||
public class countwords{
|
||||
|
||||
public static void main(String[] args){
|
||||
Scanner input = new Scanner(System.in);
|
||||
|
@ -1,30 +1,28 @@
|
||||
import java.util.Scanner;
|
||||
|
||||
class krishnamurthy
|
||||
{
|
||||
int fact(int n)
|
||||
{
|
||||
int i,p=1;
|
||||
for(i=n;i>=1;i--)
|
||||
p=p*i;
|
||||
return p;
|
||||
}
|
||||
public static void main(String args[])
|
||||
{
|
||||
Scanner sc=new Scanner(System.in);
|
||||
int a,b,s=0;
|
||||
System.out.print("Enter the number : ");
|
||||
a=sc.nextInt();
|
||||
int n=a;
|
||||
while(a>0)
|
||||
{
|
||||
b=a%10;
|
||||
s=s+fact(b);
|
||||
a=a/10;
|
||||
}
|
||||
if(s==n)
|
||||
System.out.print(n+" is a krishnamurthy number");
|
||||
else
|
||||
System.out.print(n+" is not a krishnamurthy number");
|
||||
}
|
||||
class krishnamurthy {
|
||||
static int fact(int n) {
|
||||
int i, p = 1;
|
||||
for (i = n; i >= 1; i--)
|
||||
p = p * i;
|
||||
return p;
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
Scanner sc = new Scanner(System.in);
|
||||
int a, b, s = 0;
|
||||
System.out.print("Enter the number : ");
|
||||
a = sc.nextInt();
|
||||
int n = a;
|
||||
while (a > 0) {
|
||||
b = a % 10;
|
||||
s = s + fact(b);
|
||||
a = a / 10;
|
||||
}
|
||||
if (s == n)
|
||||
System.out.print(n + " is a krishnamurthy number");
|
||||
else
|
||||
System.out.print(n + " is not a krishnamurthy number");
|
||||
sc.close();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user