Skip to content

Instantly share code, notes, and snippets.

@rahman99
Last active June 3, 2016 07:49
Show Gist options
  • Select an option

  • Save rahman99/883be01e0f028e381b8203eb20a23157 to your computer and use it in GitHub Desktop.

Select an option

Save rahman99/883be01e0f028e381b8203eb20a23157 to your computer and use it in GitHub Desktop.
convertingNumber
private static String RomanNumerals(int value){
LinkedHashMap<String, Integer> roman_numerals = new LinkedHashMap<String,Integer>();
roman_numerals.put("M", 1000);
roman_numerals.put("CM", 900);
roman_numerals.put("D", 500);
roman_numerals.put("CD", 400);
roman_numerals.put("C", 100);
roman_numerals.put("XC", 90);
roman_numerals.put("L", 50);
roman_numerals.put("XL", 40);
roman_numerals.put("X", 10);
roman_numerals.put("IX", 9);
roman_numerals.put("V", 5);
roman_numerals.put("IV", 4);
roman_numerals.put("I", 1);
String res = "";
for (Map.Entry<String, Integer> entry : roman_numerals.entrySet()){
int matches = value/entry.getValue();
res += repeat(entry.getKey(), matches);
value = value % entry.getValue();
}
return res;
}
public static String repeat(String s, int n){
if(s == null) {
return null;
}
final StringBuilder sb = new StringBuilder();
for(int i = 0; i < n; i++) {
sb.append(s);
}
return sb.toString();
}
private static final String[] lowNames = {
"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
private static final String[] tensNames = {
"twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
private static final String[] bigNames = {
"thousand", "million", "billion"};
/**
* Converts an integer number into words (american english).
* @author Christian d'Heureuse, Inventec Informatik AG, Switzerland, www.source-code.biz
**/
public String convertNumberToWord (int n) {
if (n < 0) {
return "minus " + convertNumberToWord(-n); }
if (n <= 999) {
return convert999(n); }
String s = null;
int t = 0;
while (n > 0) {
if (n % 1000 != 0) {
String s2 = convert999(n % 1000);
if (t > 0) {
s2 = s2 + " " + bigNames[t-1]; }
if (s == null) {
s = s2; }
else {
s = s2 + ", " + s; }}
n /= 1000;
t++; }
return s; }
// Range 0 to 999.
private static String convert999 (int n) {
String s1 = lowNames[n / 100] + " hundred";
String s2 = convert99(n % 100);
if (n <= 99) {
return s2; }
else if (n % 100 == 0) {
return s1; }
else {
return s1 + " " + s2; }
}
// Range 0 to 99.
private static String convert99 (int n) {
if (n < 20) {
return lowNames[n]; }
String s = tensNames[n / 10 - 2];
if (n % 10 == 0) {
return s; }
return s + "-" + lowNames[n % 10];
}
public static void main(String[] args) {
for(int i=1;i<=10;i++){
//cara 1:
// System.err.print(getNumberFibonacci(i)+" ");
//cara 2:
System.out.print(fibonacciRecursion(i)+" ");
}
}
private static int fibonacciRecursion(int n){
if(n == 1 || n==2){
return 1;
}
return fibonacciRecursion(n-1)+fibonacciRecursion(n-2);
}
private static int getNumberFibonacci(int number){
if(number == 1 || number==2){
return 1;
}
int numberOf = 1, lastNumber = 1, beforeNumber = 1;
for(int i=3;i<=number;i++){
numberOf = beforeNumber + lastNumber;
beforeNumber = lastNumber;
lastNumber = numberOf;
}
return numberOf;
}
//if fibonacci begin in first number is bil1 and second number bil2.
private static int fibo(int bil1, int bil2, int x){
if(x==1)
return bil1;
if(x==2)
return bil2;
return fibo(bil1, bil2, x-1)+fibo(bil1, bil2, x-2);
}
private static boolean polindrome(String in){
if(in == null)
return false;
StringBuilder strBuild = new StringBuilder(in);
strBuild.reverse();
System.out.println(in +" <> "+strBuild);
System.out.println(in.compareTo("popa"));
return strBuild.toString().equals(in);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment