Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Solve using your language of choice

Name: Anonymous 2011-04-13 9:05

http://i56.tinypic.com/9qlh52.png

my solution in Java

import java.io.*;
import java.util.*;

public class ProblemF {
   
    public static void main(String[] args) throws Exception {
        BufferedReader fileIn = new BufferedReader(new FileReader("f.in"));
       
        String line = null;
        while ((line = fileIn.readLine()) != null) {
            int number = Integer.parseInt(line);
            if (number <= 0 || number >= 100000000) {
                break;
            }
            System.out.println(number +" : "+ getOrderedPairs(number));
        }
    }
   
    public static String getOrderedPairs(int number) {
        StringBuffer buff = new StringBuffer();
       
        int pairCount = 0;
        for (int i = 1; i <= number; i++) {
            for (int j = i; j <= number; j++) {
                int sum = getSummation(i, j);
                if (sum == number) {
                    pairCount++;
                    buff.append(" ("+ i +","+ j +")");
                    break;
                } else if (sum > number) {
                    break;
                }
            }
        }
       
        buff.insert(0, pairCount +" :");
       
        return buff.toString();
    }
   
    public static int getSummation(int tempA, int tempB) {
        int a = tempA - 1;
        int b = tempB;
        int sumB = ((b * (b + 1)) / 2);
        int sumA = ((a * (a + 1)) / 2);
        return sumB - sumA;
    }
   
}

Name: Anonymous 2011-04-13 11:08


import Control.Applicative
orderedPair x = [(a,b) | (a,b) <- liftA2 (,) [0..x] [0..x], sum [a..b] == x]


Is that what you mena?

Name: Anonymous 2011-04-13 11:12


import Control.Applicative
orderedPairs x = [(a,b) | (a,b) <- liftA2 (,) [1..x] [1..x], sum [a..b] == x]

Name: Anonymous 2011-04-13 11:17

>>13-14
Looks dead.

Name: Anonymous 2011-04-13 11:20

>>15
I know what u mena.

Name: Anonymous 2011-04-13 11:21

>>15
you mean drop dead gorgeous?

Name: Anonymous 2011-04-13 11:43


import Control.Applicative

main = interact (unlines . map (format . orderedPairs . check . read) . lines)

check x
    | x < 0 || x > 100000000 = error "you mena haskal?"
    | otherwise = x

orderedPairs x = (x, [(a,b) | (a,b) <- liftA2 (,) [1..x] [1..x], sum [a..b] == x])

format (x,ps) = show x ++ " : " ++ show (length ps) ++ " : " ++ (unwords . map (\(a,b) -> "(" ++ show a ++ "," ++ show b ++ ")")) ps


improved it to do the whole task

Name: Anonymous 2011-04-13 11:43

>>13
No. This is what I mena.
orderedPairs n = [(a, b)| a<-[1..n], b<-[a..n], sum [a..b] == n]

Name: Anonymous 2011-04-13 11:45


import Control.Applicative

main = interact (unlines . map (format . orderedPairs . check . read) . lines)

check x
    | x <= 0 || x > 100000000 = error "you mena haskal?"
    | otherwise = x

orderedPairs x = (x, [(a,b) | (a,b) <- liftA2 (,) [1..x] [1..x], sum [a..b] == x])

format (x,ps) = show x ++ " : " ++ show (length ps) ++ " : " ++ (unwords . map (\(a,b) -> "(" ++ show a ++ "," ++ show b ++ ")")) ps

Name: Anonymous 2011-04-13 11:46

>>19
right, stupid me

Name: Anonymous 2011-04-13 11:49


main = interact (unlines . map (format . orderedPairs . check . read) . lines)

check x
    | x <= 0 || x > 100000000 = error "you mena haskal?"
    | otherwise = x

orderedPairs n = (n, [(a,b) | a <- [1..n], b <- [a..n], sum [a..b] == n])

format (x,ps) = show x ++ " : " ++ show (length ps) ++ " : " ++ (unwords . map (\(a,b) -> "(" ++ show a ++ "," ++ show b ++ ")")) ps

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List