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: >>5 2011-04-13 10:23

So I wrote this instead:

;; instead we use some math and make it O(n)
(defun find-all-sums (x)
  (sort
   (remove-duplicates
    (iter (with 2x = (* 2 x))
          (for i from x downto 1)
          (for (values div mod) = (truncate 2x i))
          (when (zerop mod)           
            (let* ((a (/ (1- (+ div i)) 2))
                   (b (- (max div i) a)))
              (collect (list b a)))))
    :key #'first)
   #'< :key #'first))


Let's test it on the highest number:

CL-USER> (time (find-all-sums 99999999))
Evaluation took:
  1.469 seconds of real time
  1.468750 seconds of total run time (1.468750 user, 0.000000 system)
  100.00% CPU
  3,541,887,882 processor cycles
  0 bytes consed
 
((309 14145) (592 14154) (4999 14999) (5002 15000) (6539 15580)
 (9877 17249) (10224 17450) (11669 18334) (18347 23164) (19859 24379)
 (28337 31669) (31672 34685) (39319 41784) (40307 42715) (43894 46115)
 (54097 55914) (61464 63069) (65604 67110) (75447 76760) (80487 81719)
 (89454 90564) (109557 110465) (121244 122065) (124132 124934)
 (151879 152535) (164714 165319) (228092 228529) (243104 243514)
 (329882 330184) (364827 365100) (456512 456730) (494949 495150)
 (504952 505149) (684859 685004) (729859 729995) (990049 990149)
 (1010052 1010150) (1369827 1369899) (1515119 1515184) (3030287 3030319)
 (4545444 4545465) (5555547 5555564) (9090904 9090914) (11111107 11111115)
 (16666664 16666669) (33333332 33333334) (49999999 50000000)
 (99999999 99999999))


Much better!

Formatting and user input is up to you (I'm not doing your homework) as it's easy enough to implement.

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