Skip to content

Instantly share code, notes, and snippets.

@M-ZubairAhmed
M-ZubairAhmed / Number with dual Cube Sums
Created February 13, 2017 08:56
Checks if a number can be written as the sum of two cubes in two different ways: n = a³+b³ = c³+d³.
/*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++) {
@M-ZubairAhmed
M-ZubairAhmed / Variable-Narcissistic-Number
Created February 13, 2017 08:59
The sum of each digit when raised to its position value equals the former number
/*
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.
*/
@M-ZubairAhmed
M-ZubairAhmed / Integer Number Check
Created February 13, 2017 10:04
To check if a Number has decimal places
/*
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"}
@M-ZubairAhmed
M-ZubairAhmed / numbers1_100.java
Last active February 20, 2017 12:39
List of Numbers in Languages
//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",
@M-ZubairAhmed
M-ZubairAhmed / Android Studio .gitignore
Created February 23, 2017 07:19 — forked from iainconnor/Android Studio .gitignore
A .gitignore for use in Android Studio
# Built application files
/*/build/
# Crashlytics configuations
com_crashlytics_export_strings.xml
# Local configuration file (sdk path, etc)
local.properties
# Gradle generated files
@M-ZubairAhmed
M-ZubairAhmed / isHex.java
Created March 17, 2017 05:19
Algorithm to check if agiven is a valid hexadecimal number.
public class StringUtils {
public static boolean isHexNumber(String s) {
int cursor;
if (s.length() == 0){
return false;
}
@M-ZubairAhmed
M-ZubairAhmed / stammeringPattern.java
Created March 17, 2017 05:23
Pattern generating algorithm. eg: abcd -> A-Bb-Ccc-Dddd
public class Accumul {
public static String accum(String s){
StringBuilder stBuild = new StringBuilder();
for(int i = 0; i < s.length(); i++){
for(int j = 0; j <= i; j++) {
if (j==0){
stBuild.append(s.toUpperCase().charAt(i));
}
else {
stBuild.append(s.toLowerCase().charAt(i));
@M-ZubairAhmed
M-ZubairAhmed / keypadTaps.java
Created March 17, 2017 05:27
Calculates number of taps required to type a text in old numpad keyboard phones
public class Keypad {
public static int presses(String phrase){
int noPresses = 0;
for(int i = 0 ; i<phrase.length(); i++){
char aToZ = phrase.toUpperCase().charAt(i);
if (aToZ == 'A'||aToZ == 'D'||aToZ == 'G'||aToZ == 'J'||aToZ == 'M'||aToZ == 'P'||aToZ == 'T'||aToZ == 'W'||aToZ == '1'||aToZ == ' '||aToZ == '*'||aToZ == '#'){
noPresses = noPresses + 1;
System.out.println(noPresses+" 1 = "+aToZ);}
else if (aToZ == 'B'||aToZ == 'E'||aToZ == 'H'||aToZ == 'K'||aToZ == 'N'||aToZ == 'Q'||aToZ == 'U'||aToZ == 'X'||aToZ == '0'){
@M-ZubairAhmed
M-ZubairAhmed / Caesar Decryption.java
Created March 22, 2017 04:16
Caesar Decryption Algorithm
public class NotePassing {
public static String decipher (String codedMessage, int key){
char[] charArray = new char[codedMessage.length()];
int[] intArray = new int[codedMessage.length()+1];
for (int r = 0; r < codedMessage.length(); r++){
boolean isUCase = (codedMessage.charAt(r)>=65) && (codedMessage.charAt(r)<=90);
boolean isLCase = (codedMessage.charAt(r)>=97) && (codedMessage.charAt(r)<=122);
int changedAsci = ((int)codedMessage.charAt(r)) + key;
if (isUCase) {
@M-ZubairAhmed
M-ZubairAhmed / New Caesar Decryption.java
Created March 22, 2017 04:33
Algorithm for decrypting improved version of Caesar cypher
public static String demovingShift(String inputCodedString, int shift, int decremental){
char[] inCodedCharArr = new char[inputCodedString.length()];
for (int q = 0; q < inputCodedString.length(); q++) {
int DeFlushV =0 , DeFlushVDec = 0;
DeFlushV = (int)inputCodedString.charAt(q);
if (DeFlushV >= 65 && DeFlushV <=90){
DeFlushVDec = DeFlushV - shift;
if(DeFlushVDec < 65) {
while (DeFlushVDec < 65) {