Project Euler #17: Number to Words

Sort by

recency

|

86 Discussions

|

  • + 0 comments

    Here's what I came up with using Javascript:

    let numbers = {
      1: 'One', 2: 'Two', 3: 'Three', 4: 'Four', 5: 'Five', 6: 'Six', 7: 'Seven', 8: 'Eight', 9: 'Nine', 10: 'Ten', 11: 'Eleven', 12: 'Twelve', 13: 'Thirteen', 14: 'Fourteen', 15: 'Fifteen', 16: 'Sixteen', 17: 'Seventeen', 18: 'Eighteen', 19: 'Nineteen', 20: 'Twenty', 30: 'Thirty', 40: 'Forty', 50: 'Fifty', 60: 'Sixty', 70: 'Seventy', 80: 'Eighty', 90: 'Ninety', 100: 'Hundred', 1000: 'Thousand', 1000000: 'Million', 1000000000: 'Billion', 1000000000000: 'Trillion'
    }
    
    let convertTriplet = str => {
      let rtn = [];
      let num = parseInt(str);
      if (isNaN(num) || num === 0) return [];
      if (num > 100) {
        rtn.push(numbers[Math.floor(num/100)], 'Hundred');
        num %= 100;
      }
      if (num <= 20) {
        rtn.push(numbers[num]);
      } else {
        if (Math.floor(num/10) > 0) {
          rtn.push(numbers[Math.floor(num/10) * 10]);
          num %= 10
        }
        if (num > 0) {
          rtn.push(numbers[num]);
        }
      }
      return rtn;
    }
    
    function processData(input) {
      let data = input.split('\n');
      for (let i = 1; i < data.length; i++) {
        let number = [];
        for (let j = 0; j < Math.ceil(data[i].length / 3); j++) {
          let triplet = convertTriplet(data[i].substring(data[i].length - 3 * (j+1), data[i].length - 3 * (j+1) + 3));
          if (triplet.length > 0 && j > 0) triplet.push(numbers[Math.pow(10, j*3)]);
          number.unshift(...triplet);
        }
        console.log(number.join(' '));
      }
    } 
    
  • + 0 comments

    Here is my code : Basic logic.

    numbers={0:"", 1:" One", 2:" Two",3:" Three", 4:" Four",5:" Five",6:" Six",7:" Seven", 8:" Eight",9:" Nine"}
    
    teens={10:" Ten",11:" Eleven",12:" Twelve",13:" Thirteen",14:" Fourteen",15:" Fifteen",16:" Sixteen",17:" Seventeen",18:" Eighteen",19:" Nineteen"}
    
    tens={20:" Twenty",30:" Thirty",40:" Forty",50:" Fifty",60:" Sixty",70:" Seventy",80:" Eighty",90:" Ninety"}
    
    def convert(n):
        if n<10:
            return numbers[n]
        elif n<20:
            return teens[n]
        elif n<100 and n%10==0:
            return tens[n]
        elif n<100:
            return tens[10*(n//10)] + numbers[n%10]
        elif n<1000:
            return numbers[n//100]+" Hundred"+convert(n%100)
        elif n<1000000:
            return convert(n//1000)+ " Thousand"+convert(n%1000)
        elif n<1000000000:
            return convert(n//1000000)+ " Million" + convert(n%1000000)
        else:
            return convert(n//1000000000)+" Billion" + convert(n%1000000000)
    
    t=int(input().strip())
    for _ in range(t):
        n=int(input().strip())
        print(convert(n).strip())
    
  • + 0 comments

    Java code

    import java.io.*;
    import java.util.*;
    
    public class Solution {
        
          static String[] digits = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
                "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
    
        static String[] tens = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
    
        // public static void main(String[] args) {
        //     /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            
           
    
        public static void word(int num) {
            if (num / 100 > 0) {
                System.out.print(digits[num / 100] + " Hundred ");
            }
            if ((num % 100) < 20 && (num % 100) > 0) {
                System.out.print(digits[num % 100] + " ");
            } else if ((num / 10) % 10 > 0) {
                System.out.print(tens[(num / 10) % 10] + " ");
                if (num % 10 > 0) {
                    System.out.print(digits[num % 10] + " ");
                }
            }
        }
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int t = scanner.nextInt();
            while (t-- > 0) {
                long num = scanner.nextLong();
                int tn = (int) (num / 1000000000000L);
                int bn = (int) ((num / 1000000000L) % 1000);
                int mn = (int) ((num / 1000000L) % 1000);
                int th = (int) ((num / 1000L) % 1000);
                int hd = (int) (num % 1000);
                if ((tn + bn + mn + th + hd) == 0) {
                    System.out.print("Zero");
                }
                if (tn > 0) {
                    word(tn);
                    System.out.print("Trillion ");
                }
                if (bn > 0) {
                    word(bn);
                    System.out.print("Billion ");
                }
                if (mn > 0) {
                    word(mn);
                    System.out.print("Million ");
                }
                if (th > 0) {
                    word(th);
                    System.out.print("Thousand ");
                }
                if (hd > 0) {
                    word(hd);
                }
                System.out.println();
            }
        }
        
        
    }
    
  • + 0 comments
    import java.io.*;
    import java.util.*;
    
    public class Solution {
        
          static String[] digits = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten",
                "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
    
        static String[] tens = {"", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
    
        // public static void main(String[] args) {
        //     /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            
           
    
        public static void word(int num) {
            if (num / 100 > 0) {
                System.out.print(digits[num / 100] + " Hundred ");
            }
            if ((num % 100) < 20 && (num % 100) > 0) {
                System.out.print(digits[num % 100] + " ");
            } else if ((num / 10) % 10 > 0) {
                System.out.print(tens[(num / 10) % 10] + " ");
                if (num % 10 > 0) {
                    System.out.print(digits[num % 10] + " ");
                }
            }
        }
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int t = scanner.nextInt();
            while (t-- > 0) {
                long num = scanner.nextLong();
                int tn = (int) (num / 1000000000000L);
                int bn = (int) ((num / 1000000000L) % 1000);
                int mn = (int) ((num / 1000000L) % 1000);
                int th = (int) ((num / 1000L) % 1000);
                int hd = (int) (num % 1000);
                if ((tn + bn + mn + th + hd) == 0) {
                    System.out.print("Zero");
                }
                if (tn > 0) {
                    word(tn);
                    System.out.print("Trillion ");
                }
                if (bn > 0) {
                    word(bn);
                    System.out.print("Billion ");
                }
                if (mn > 0) {
                    word(mn);
                    System.out.print("Million ");
                }
                if (th > 0) {
                    word(th);
                    System.out.print("Thousand ");
                }
                if (hd > 0) {
                    word(hd);
                }
                System.out.println();
            }
        }
        
        
    }
    
  • + 0 comments
    d1 = {1: "One", 2: "Two", 3: "Three", 4: "Four", 5: "Five", 6: "Six", 7: "Seven", 8: "Eight", 9: "Nine"}
    
    d2 = {10: "Ten", 11: "Eleven", 12: "Twelve", 13: "Thirteen", 14: "Fourteen", 15: "Fifteen", 16: "Sixteen", 17: "Seventeen", 18: "Eighteen", 19: "Nineteen"}
    
    d3 = {2: "Twenty", 3: "Thirty", 4: "Forty", 5: "Fifty", 6: "Sixty", 7: "Seventy", 8: "Eighty", 9: "Ninety"}
    
    def number1(n):   # this fonction give the numbre with word with number between 0 nd 100
        if 1 <= n < 10:
            return d1[n]
        elif 10 <= n < 20:
            return d2[n]
        else:
            a = n % 10
            b = n // 10
            if a == 0:
                return d3[b]
            else:
                ch = d3[b] + " " + d1[a]
                return ch
    
    def number2(n): # this fonction give the numbre with word with number between 0 nd 999
        if n < 100:
            return number1(n)
        else:
            a = n % 100
            b = n // 100
            if a == 0:
                return d1[b] + " Hundred"
            else:
                ch = d1[b] + " Hundred " + number1(a)
                return ch
    
    d4 = {1: "Thousand", 2: "Million", 3: "Billion", 4: "Trillion"}
    
    def number3(n): # this fonction give the numbre with word with number between 0 nd 9999..
        if n < 1000:
            return number2(n)
        else:
            ch1 = str(n)
            l = []
            for i in range(len(ch1), 0, -3):
                l.append(int(ch1[max(i - 3, 0):i]))
            ch2 = number2(l[0])
            for i in range(1, len(l)):
                if l[i] != 0:  
                    ch2 = number2(l[i]) + " " + d4[i] + " " + ch2
            return ch2
    
    for _ in range(int(input())):
        num = int(input())
        print(number3(num))