This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
One of the easiest ways to check if a number after it comes back from some function is an Integer or not is to check its remainder with 1. eg. | |
if(Number % 1 == 0){ | |
print "this is a perfect integer"} | |
else{ | |
print "this is a real number with decimal places"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
89 is a unique number and searching similar others | |
The number 89 is the first integer with more than one digit that fulfills the property partially introduced below: | |
example | |
89 = 8^1 + 9^2 | |
135 = 1^1 + 3^2 + 5^3 | |
The sum of each digit when raised to its position value equals the former number | |
A range is given and all those numbers with above property is collected. | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Number with dual Cube Sums | |
TwoCubeSums | |
which checks if a given number can be written as the sum of two cubes in two different ways: n = a³+b³ = c³+d³. All the numbers a, b, c and d should be different and greater than 0. | |
e.g. 1729 = 9³+10³ = 1³+12³. | |
*/ | |
public class CubeSumsClass { | |
public static boolean hasTwoCubeSums(int n){ | |
int checker = 0; | |
for (int i = 0; i <=(int)Math.pow(n,1.0/3.0); i++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* When the given problem is to find number number of trailing zeros of the factorial, one is not required to | |
find out the factorial. | |
Statement : factorial(5) = 120 = 1*2*3*4*5 | |
factorial(4) = 24 = 1*2*3*4 | |
if it is observed the occurance of 5 or multiples of five is in direct proportion to number of trailing zeros | |
If however, expression contains powers of 5, then the trailing zeros increasing with power of 5. | |
factorial(25) = 1*...5*..10..*15*..20*..25 | |
As expected number of trailing zero in above is expected to be 5, but since 25 is 5 power 2, it adds another zero | |
hence final count is 6. | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//bNum = Initializing big interger to value. | |
BigInteger bNum = BigInteger.ONE; | |
// n = The input number whose factorial is to be calculated. | |
for (long i = 1; i <= n; i++) { | |
bNum = bNum.multiply(bNum.valueOf(i)); | |
} | |
//Storing the value of factorial in a string. | |
String factorialValue = String.valueOf(bNum); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Many a times we do need the list of numbers in languages. i did a lot of times but couldnt find it, so i got the unfiltered list | |
//applied some regex and got the final list as follow. | |
//List of numbers from 1-100 in English | |
private String[] numbersArray_En = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", | |
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", "twenty", | |
"twenty-one", "twenty-two", "twenty-three", "twenty-four", "twenty-five", "twenty-six", "twenty-seven", "twenty-eight", "twenty-nine", "thirty", | |
"thirty-one", "thirty-two", "thirty-three", "thirty-four", "thirty-five", "thirty-six", "thirty-seven", "thirty-eight", "thirty-nine", "forty", | |
"forty-one", "forty-two", "forty-three", "forty-four", "forty-five", "forty-six", "forty-seven", "forty-eight", "forty-nine", "fifty", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String[] common = {"the","of","and","a","to","in","is","you","that","it","he","was","for","on","are","as","with","his","they","I","at","be","this","have","from","or","one","had","by","word","but","not","what","all","were","we","when","your","can","said","there","use","an","each","which","she","do","how","their","if","will","up","other","about","out","many","then","them","these","so","some","her","would","make","like","him","into","time","has","look","two","more","write","go","see","number","no","way","could","people","my","than","first","water","been","call","who","oil","its","now","find","long","down","day","did","get","come","made","may","part"}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static int convertBits(int initialNumber, int newNumber) { | |
return Integer.bitCount(initialNumber ^ newNumber); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
public static void main(String[] args) { | |
String originalString = "azzbyyyx"; | |
StringBuilder str = new StringBuilder(originalString); | |
int loopCounter = 0; | |
int repetitionCounter = 0; | |
while (true) { | |
if (loopCounter < str.length() - 1) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int decimalNum; | |
//Converts decimal number to binary, where regex is 2 | |
Integer.toString(decimalNum,2); | |
//Converts decimal number to hexadecimal, where regex is 16 | |
Integer.toString(decimalNum,16); |