Program to save number list in txt file ( 000000 to 606060 )

  • Detlev
  • Graduate
  • Graduate
  • Detlev
  • Posts: 132
  • Loc: In front o' Computer.

Post 3+ Months Ago

Is anyone up to making, for free, a program that would list all possible numbers from 000000 to 606060 and then save it into a .txt file. If so that would be great if not, w/e. Thanks in advanced to everyone. O! and if you know of any place on the net i could get one just do that instead. Thanks.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • aeon
  • Graduate
  • Graduate
  • User avatar
  • Posts: 184
  • Loc: Ireland

Post 3+ Months Ago

you didn't specify a language so... here it in in perl

Code: [ Select ]
$numbers = "numbers.txt";
open(INFO, "+>$numbers") ;

for($i = 0; $i < 606061; $i++) {
    print INFO "$i\n";
}

close(INFO);
  1. $numbers = "numbers.txt";
  2. open(INFO, "+>$numbers") ;
  3. for($i = 0; $i < 606061; $i++) {
  4.     print INFO "$i\n";
  5. }
  6. close(INFO);


go to http://www.perl.org and download the interpreter for your platform, you may also want to download something to convert it to an executable, i can't think of one off hand but googling should turn up something
  • Maedhros
  • Proficient
  • Proficient
  • User avatar
  • Posts: 325
  • Loc: Durham, England

Post 3+ Months Ago

And in PHP:
PHP Code: [ Select ]
$filename = "numbers.txt";
 
$file = fopen($filename, "w+");
 
 
 
for ($i = 0; $i <= 606060; $i++) {
 
    if (!fwrite($file, "$i\n")) {
 
        die ("Could not write to file $filename");
 
    }
 
}
 
 
 
fclose($file);
  1. $filename = "numbers.txt";
  2.  
  3. $file = fopen($filename, "w+");
  4.  
  5.  
  6.  
  7. for ($i = 0; $i <= 606060; $i++) {
  8.  
  9.     if (!fwrite($file, "$i\n")) {
  10.  
  11.         die ("Could not write to file $filename");
  12.  
  13.     }
  14.  
  15. }
  16.  
  17.  
  18.  
  19. fclose($file);


Or in BASH:
Code: [ Select ]
FILENAME="numbers.txt"
touch "numbers.txt"

for i in $(seq 0 606060); do
  echo "$i" >> $FILENAME
done
  1. FILENAME="numbers.txt"
  2. touch "numbers.txt"
  3. for i in $(seq 0 606060); do
  4.   echo "$i" >> $FILENAME
  5. done


Note: The BASH script took 13.6s to run, the PHP script took 2s, and the Perl script took a hair under half a second. They all produce an identical 4Mb file.
  • aeon
  • Graduate
  • Graduate
  • User avatar
  • Posts: 184
  • Loc: Ireland

Post 3+ Months Ago

and here it is in c++

Code: [ Select ]
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    ofstream out;
    out.open ("numbers.txt");
    
    for(int i = 0; i < 606061; i++)
        out << i << endl;
        
    out.close();
}
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5. int main()
  6. {
  7.     ofstream out;
  8.     out.open ("numbers.txt");
  9.     
  10.     for(int i = 0; i < 606061; i++)
  11.         out << i << endl;
  12.         
  13.     out.close();
  14. }


takes about 10 seconds to run. There's also the compile time and the fact that it took longer to code... I love Perl

edit: python

Code: [ Select ]
out = open('numbers.txt', 'w')
a = 10

i = 0

for x in range (606061):
    string = str(i)
    fout.write(string)
    fout.write("\n")
    i+=1
  1. out = open('numbers.txt', 'w')
  2. a = 10
  3. i = 0
  4. for x in range (606061):
  5.     string = str(i)
  6.     fout.write(string)
  7.     fout.write("\n")
  8.     i+=1


this ones a bit quicker than c++, but it took the longest to code (since i had to learn some python first)
  • Mas Sehguh
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1853

Post 3+ Months Ago

Here it is in Reverse Polish Lisp:
Code: [ Select ]
« "" 0 606060 FOR Z Z + "
" + NEXT 'output.txt' STO »
  1. « "" 0 606060 FOR Z Z + "
  2. " + NEXT 'output.txt' STO »


Of course, maybe he wants a file that looks like this:
Code: [ Select ]
000000
000001
000002
000003
...
  1. 000000
  2. 000001
  3. 000002
  4. 000003
  5. ...


So...
Code: [ Select ]
« "" 1000000 1606060 FOR Z Z →STR TAIL + "
" + NEXT 'output.txt' STO »
  1. « "" 1000000 1606060 FOR Z Z →STR TAIL + "
  2. " + NEXT 'output.txt' STO »
  • aeon
  • Graduate
  • Graduate
  • User avatar
  • Posts: 184
  • Loc: Ireland

Post 3+ Months Ago

i made a minor change in my c++ for the leading 0's

Code: [ Select ]
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
  ofstream out;
  out.open ("numbers.txt");

  for(int i = 0; i < 606061; i++)
   out << setw(6) << setfill('0') << i << endl;

  out.close();
}
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <iomanip>
  5. using namespace std;
  6. int main()
  7. {
  8.   ofstream out;
  9.   out.open ("numbers.txt");
  10.   for(int i = 0; i < 606061; i++)
  11.    out << setw(6) << setfill('0') << i << endl;
  12.   out.close();
  13. }


EDIT: perl fix

Code: [ Select ]
$numbers = "numbers.txt";
open(INFO, "+>$numbers") ;

$num = '000000';

for($i = 0; $i < 606061; $i++) {
    print INFO "$num\n"; $num++;
}

close(INFO);
  1. $numbers = "numbers.txt";
  2. open(INFO, "+>$numbers") ;
  3. $num = '000000';
  4. for($i = 0; $i < 606061; $i++) {
  5.     print INFO "$num\n"; $num++;
  6. }
  7. close(INFO);
  • Maedhros
  • Proficient
  • Proficient
  • User avatar
  • Posts: 325
  • Loc: Durham, England

Post 3+ Months Ago

For leading zeroes...

PHP Code: [ Select ]
$filename = "numbers.txt";
 
$file = fopen($filename, "w+");
 
 
 
for ($i = 0; $i <= 606060; $i++) {
 
    $length = strlen($i);
 
    $padding = str_repeat("0", (6 - $length));
 
 
 
    fwrite($file, $padding.$i."\n");
 
}
 
 
 
fclose($file);
  1. $filename = "numbers.txt";
  2.  
  3. $file = fopen($filename, "w+");
  4.  
  5.  
  6.  
  7. for ($i = 0; $i <= 606060; $i++) {
  8.  
  9.     $length = strlen($i);
  10.  
  11.     $padding = str_repeat("0", (6 - $length));
  12.  
  13.  
  14.  
  15.     fwrite($file, $padding.$i."\n");
  16.  
  17. }
  18.  
  19.  
  20.  
  21. fclose($file);


And for bash, replace $(seq 0 606060) with $(seq -w 0 606060). :)

Edit: I'm an idiot... replace all the BASH code with this, and it will run about 5 times faster...
Code: [ Select ]
seq -w 0 606060 > numbers.txt
  • SpooF
  • ٩๏̯͡๏۶
  • Bronze Member
  • User avatar
  • Posts: 3422
  • Loc: Richland, WA

Post 3+ Months Ago

may i ask why might you need something that lists every number from 0 to 606060 ?

p.s VBscript anyone? i was looken at javascript but there is no i/o for it. java?
  • lucassix
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 2350
  • Loc: Indiana

Post 3+ Months Ago

How about.... BASIC!

Code: [ Select ]
DIM TextFile$
PRINT "Enter a path and file name for the text file."
INPUT "(Stick to 8.3 paths, this is BASIC you know):", TextFile$
PRINT " "
PRINT "Writing 000000 to 606060..."
OPEN TextFile$ FOR APPEND AS #1
n = 0
DO
PRINT #1, n
n = n + 1
LOOP UNTIL n = 606061
CLOSE #1
PRINT "Done!"
SHELL "PAUSE"
  1. DIM TextFile$
  2. PRINT "Enter a path and file name for the text file."
  3. INPUT "(Stick to 8.3 paths, this is BASIC you know):", TextFile$
  4. PRINT " "
  5. PRINT "Writing 000000 to 606060..."
  6. OPEN TextFile$ FOR APPEND AS #1
  7. n = 0
  8. DO
  9. PRINT #1, n
  10. n = n + 1
  11. LOOP UNTIL n = 606061
  12. CLOSE #1
  13. PRINT "Done!"
  14. SHELL "PAUSE"


And here is it compiled for you: http://home.satx.rr.com/airforcedave/606060.EXE (40KB)

//EDIT: BASIC doesn't like leading zeros, so still working on that...
  • Detlev
  • Graduate
  • Graduate
  • Detlev
  • Posts: 132
  • Loc: In front o' Computer.

Post 3+ Months Ago

wowsers. thanks for the many replies.
  • Detlev
  • Graduate
  • Graduate
  • Detlev
  • Posts: 132
  • Loc: In front o' Computer.

Post 3+ Months Ago

I tried the basic one, thank you for doing that for me, but I don;t think I explained this correctly. I wanted the numbers:
000001
000002
000003....

Basically; and the reason is because I was wondering what all the possible numbers on a combination lock was. I wasn't sure of the highest possible number but I knew it was not greater than 60. O! and I can see you asking why I would want to know that; the answer is really, im entertained by stupid things such as that and, well im an idiot that is easily amused.
  • Detlev
  • Graduate
  • Graduate
  • Detlev
  • Posts: 132
  • Loc: In front o' Computer.

Post 3+ Months Ago

I think you need something like an algorithem, I have no clue, that's why I came here.
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Okay my advice. Go to google and find out what an algorithm is.

Just a quicky though, all the above are basic algorithms.

Quote:
Reverse Polish Lisp

Only you Sam ;)

Interesting read
http://www.engr.uvic.ca/~aschoorl/faq/48faq-8.html
  • aeon
  • Graduate
  • Graduate
  • User avatar
  • Posts: 184
  • Loc: Ireland

Post 3+ Months Ago

Quote:
I tried the basic one, thank you for doing that for me, but I don;t think I explained this correctly. I wanted the numbers:
000001
000002
000003....


both the perl one and the c++ one do that..

I've uploaded a c++ executable here
  • Detlev
  • Graduate
  • Graduate
  • Detlev
  • Posts: 132
  • Loc: In front o' Computer.

Post 3+ Months Ago

super cool you guys are all awsome, i must study how this works.
  • o[tter]
  • Student
  • Student
  • User avatar
  • Posts: 80
  • Loc: Seattle, WA, USA

Post 3+ Months Ago

Nobody did it in either Euphoria or ML. I need to re-learn both of those... ML is fun to wrap your mind around.
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Well just as a matter of interest, these algorithms won't give you all the possible combinations for a three way combinationlock with a highest value of 60 for each lock ( well they will but it will have extra information in it).

For an optimized algorithm that does this look at it from the point of view that you have one lock with a value of 60 repeated three times.

so you are going to need one main loop till sixty and two nested loops till sixty.

the code to do soemthinglike this would look like
PHP Code: [ Select ]
 
$fh = fopen("606060.txt","w");
 
$total = 0;
 
for ($i = 0; $i < 61; $i++){ // first set of 60
 
   
 
   if ($i < 10 ) {
 
       $first_set = "0" . $i;
 
   }else{
 
      $first_set = $i;
 
   }
 
   for ($j = 0; $j < 61; $j++){ //second set of 60
 
      if ($j < 10) {
 
          $second_set = "0" . $j;
 
      }else{
 
         $second_set = $j;
 
      }
 
      for ($k = 0; $k < 61; $k++){ // third set of 60
 
         $total++; // edit thanks to Maedhros
 
         if ($k < 10) {
 
             $third_set = "0" . $k;
 
         }else{
 
            $third_set = $k;
 
         }
 
         $string  = $first_set . $second_set . $third_set . "\n";
 
         fwrite($fh,$string);
 
      }
 
   
 
   }
 
}
 
fwrite($fh,"Total Combinations: " . $total);
 
fclose($fh);
 
echo "Total Combinations: " . $total
 
 
  1.  
  2. $fh = fopen("606060.txt","w");
  3.  
  4. $total = 0;
  5.  
  6. for ($i = 0; $i < 61; $i++){ // first set of 60
  7.  
  8.    
  9.  
  10.    if ($i < 10 ) {
  11.  
  12.        $first_set = "0" . $i;
  13.  
  14.    }else{
  15.  
  16.       $first_set = $i;
  17.  
  18.    }
  19.  
  20.    for ($j = 0; $j < 61; $j++){ //second set of 60
  21.  
  22.       if ($j < 10) {
  23.  
  24.           $second_set = "0" . $j;
  25.  
  26.       }else{
  27.  
  28.          $second_set = $j;
  29.  
  30.       }
  31.  
  32.       for ($k = 0; $k < 61; $k++){ // third set of 60
  33.  
  34.          $total++; // edit thanks to Maedhros
  35.  
  36.          if ($k < 10) {
  37.  
  38.              $third_set = "0" . $k;
  39.  
  40.          }else{
  41.  
  42.             $third_set = $k;
  43.  
  44.          }
  45.  
  46.          $string  = $first_set . $second_set . $third_set . "\n";
  47.  
  48.          fwrite($fh,$string);
  49.  
  50.       }
  51.  
  52.    
  53.  
  54.    }
  55.  
  56. }
  57.  
  58. fwrite($fh,"Total Combinations: " . $total);
  59.  
  60. fclose($fh);
  61.  
  62. echo "Total Combinations: " . $total
  63.  
  64.  


This should give you all possible combinations

Sam anything wrong with my maths here?
  • Maedhros
  • Proficient
  • Proficient
  • User avatar
  • Posts: 325
  • Loc: Durham, England

Post 3+ Months Ago

Everything seems to work, except your total - you add $k to it, which at some points would add 60 lines to it, despite there only being one. If you replace $total = $total + $k with $total++, it gives the right number of combinations (as in, the number of lines in the file): 226981.
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Maedhros wrote:
Everything seems to work, except your total - you add $k to it, which at some points would add 60 lines to it, despite there only being one. If you replace $total = $total + $k with $total++, it gives the right number of combinations (as in, the number of lines in the file): 226981.


OOPS. Have updated above and given credit. Thanks for pointing that out, I have a bad habit of getting caught up in the complex stuff and forgetting the simple stuff.

Thanks mate
  • Maedhros
  • Proficient
  • Proficient
  • User avatar
  • Posts: 325
  • Loc: Durham, England

Post 3+ Months Ago

Rabid Dog wrote:
OOPS. Have updated above and given credit. Thanks for pointing that out, I have a bad habit of getting caught up in the complex stuff and forgetting the simple stuff.

Thanks mate

No problem! Now that you've written it, I understand it all perfectly, but I think I would have been stumped for a while before I could come up with it on my own... :?
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Busy trying to reduce the code and write a reusable class for combinations of any size sets.

Proving to be a bit tricky!
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

for interest sake here is the same code ported to ASP

Code: [ Select ]
<%

Dim intTotal
Dim strOutput
Dim i,j,k
Dim first_set, second_set, third_set
Dim objFS, objFile

filename   = server.mappath("606060asp.txt")
Set objFS  = server.CreateObject("Scripting.FileSystemObject")
Set objFile = objFS.CreateTextFile(filename, true)

intTotal = 0

for i = 0 to 60
 if i < 10 then
  first_set = "0" & i
 else
  first_set = i
 end if
 for j = 0 to 60
  if j < 10 then
   second_set = "0" & j
  else
   second_set = j
  end if
  for k = 0 to 60
   if k < 10 then
    third_set = "0" & k
   else
    third_set = k
   end if
   strOutput = first_set & second_set & third_set
   objFile.writeline strOutput
   intTotal = intTotal + 1 
  next ' third set for
 next' second set for
next ' first set for
objFile.writeline ""
objFile.writeline intTotal
objFile.close

Response.Write "Done"
%>
  1. <%
  2. Dim intTotal
  3. Dim strOutput
  4. Dim i,j,k
  5. Dim first_set, second_set, third_set
  6. Dim objFS, objFile
  7. filename   = server.mappath("606060asp.txt")
  8. Set objFS  = server.CreateObject("Scripting.FileSystemObject")
  9. Set objFile = objFS.CreateTextFile(filename, true)
  10. intTotal = 0
  11. for i = 0 to 60
  12.  if i < 10 then
  13.   first_set = "0" & i
  14.  else
  15.   first_set = i
  16.  end if
  17.  for j = 0 to 60
  18.   if j < 10 then
  19.    second_set = "0" & j
  20.   else
  21.    second_set = j
  22.   end if
  23.   for k = 0 to 60
  24.    if k < 10 then
  25.     third_set = "0" & k
  26.    else
  27.     third_set = k
  28.    end if
  29.    strOutput = first_set & second_set & third_set
  30.    objFile.writeline strOutput
  31.    intTotal = intTotal + 1 
  32.   next ' third set for
  33.  next' second set for
  34. next ' first set for
  35. objFile.writeline ""
  36. objFile.writeline intTotal
  37. objFile.close
  38. Response.Write "Done"
  39. %>
  • Maedhros
  • Proficient
  • Proficient
  • User avatar
  • Posts: 325
  • Loc: Durham, England

Post 3+ Months Ago

Rabid Dog wrote:
Busy trying to reduce the code and write a reusable class for combinations of any size sets.

Proving to be a bit tricky!

I like a challenge! :D

This is what I came up with:
PHP Code: [ Select ]
<?php
 
 
 
class combination_lock {
 
 
 
        var $file_handle;
 
        var $possible_values    = array();
 
        var $number_wheels      = 0;
 
        var $total_combinations = 0;
 
 
 
        function combination_lock($values, $file) {
 
 
 
                $this->file_handle = fopen($file, "w+");
 
 
 
                $this->set_possible_values($values);
 
 
 
        }
 
 
 
        function set_possible_values($values) {
 
 
 
                if (!is_array($values)) {
 
 
 
                        die("set_highest_values(): This must be an array containing the elements 'beginning', 'end', and 'increment' for each wheel.");
 
 
 
                } else {
 
 
 
                        $this->possible_values = $values;
 
                        $this->number_wheels   = count($values);
 
 
 
                }
 
 
 
        }
 
 
 
        function pad($value, $max_length) {
 
 
 
                $length = strlen($value);
 
 
 
                return $value = str_repeat("0", ($max_length - $length)).$value;
 
 
 
        }
 
 
 
        function loop($current_wheel) {
 
 
 
                global $a;
 
 
 
                $beginning      = $this->possible_values[$current_wheel]["beginning"];
 
                $end            = $this->possible_values[$current_wheel]["end"];
 
                $increment      = $this->possible_values[$current_wheel]["increment"];
 
 
 
                for ($i = $beginning, $j = $end, $k = $increment; $i <= $j; $i += $k) {
 
 
 
                        $a[$current_wheel] = $i;
 
 
 
                        if ($current_wheel < $this->number_wheels) {
 
                                $this->loop($current_wheel + 1);
 
                        } else {
 
                                $combination = "";
 
                                foreach ($a as $number) {
 
                                        $number = $this->pad($number, strlen($j));
 
                                        $combination .= "$number";
 
                                }
 
                                fwrite($this->file_handle, $combination."\n");
 
 
 
                                $this->total_combinations += 1;
 
                        }
 
 
 
                }
 
 
 
        }
 
 
 
        function create_possibilities() {
 
 
 
                if (empty($this->possible_values)) {
 
 
 
                        die ("You must set the possible values for the combination first");
 
 
 
                } else {
 
 
 
                        $this->loop(1);
 
 
 
                        return $this->total_combinations;
 
 
 
                }
 
 
 
        }
 
 
 
}
 
 
 
?>
  1. <?php
  2.  
  3.  
  4.  
  5. class combination_lock {
  6.  
  7.  
  8.  
  9.         var $file_handle;
  10.  
  11.         var $possible_values    = array();
  12.  
  13.         var $number_wheels      = 0;
  14.  
  15.         var $total_combinations = 0;
  16.  
  17.  
  18.  
  19.         function combination_lock($values, $file) {
  20.  
  21.  
  22.  
  23.                 $this->file_handle = fopen($file, "w+");
  24.  
  25.  
  26.  
  27.                 $this->set_possible_values($values);
  28.  
  29.  
  30.  
  31.         }
  32.  
  33.  
  34.  
  35.         function set_possible_values($values) {
  36.  
  37.  
  38.  
  39.                 if (!is_array($values)) {
  40.  
  41.  
  42.  
  43.                         die("set_highest_values(): This must be an array containing the elements 'beginning', 'end', and 'increment' for each wheel.");
  44.  
  45.  
  46.  
  47.                 } else {
  48.  
  49.  
  50.  
  51.                         $this->possible_values = $values;
  52.  
  53.                         $this->number_wheels   = count($values);
  54.  
  55.  
  56.  
  57.                 }
  58.  
  59.  
  60.  
  61.         }
  62.  
  63.  
  64.  
  65.         function pad($value, $max_length) {
  66.  
  67.  
  68.  
  69.                 $length = strlen($value);
  70.  
  71.  
  72.  
  73.                 return $value = str_repeat("0", ($max_length - $length)).$value;
  74.  
  75.  
  76.  
  77.         }
  78.  
  79.  
  80.  
  81.         function loop($current_wheel) {
  82.  
  83.  
  84.  
  85.                 global $a;
  86.  
  87.  
  88.  
  89.                 $beginning      = $this->possible_values[$current_wheel]["beginning"];
  90.  
  91.                 $end            = $this->possible_values[$current_wheel]["end"];
  92.  
  93.                 $increment      = $this->possible_values[$current_wheel]["increment"];
  94.  
  95.  
  96.  
  97.                 for ($i = $beginning, $j = $end, $k = $increment; $i <= $j; $i += $k) {
  98.  
  99.  
  100.  
  101.                         $a[$current_wheel] = $i;
  102.  
  103.  
  104.  
  105.                         if ($current_wheel < $this->number_wheels) {
  106.  
  107.                                 $this->loop($current_wheel + 1);
  108.  
  109.                         } else {
  110.  
  111.                                 $combination = "";
  112.  
  113.                                 foreach ($a as $number) {
  114.  
  115.                                         $number = $this->pad($number, strlen($j));
  116.  
  117.                                         $combination .= "$number";
  118.  
  119.                                 }
  120.  
  121.                                 fwrite($this->file_handle, $combination."\n");
  122.  
  123.  
  124.  
  125.                                 $this->total_combinations += 1;
  126.  
  127.                         }
  128.  
  129.  
  130.  
  131.                 }
  132.  
  133.  
  134.  
  135.         }
  136.  
  137.  
  138.  
  139.         function create_possibilities() {
  140.  
  141.  
  142.  
  143.                 if (empty($this->possible_values)) {
  144.  
  145.  
  146.  
  147.                         die ("You must set the possible values for the combination first");
  148.  
  149.  
  150.  
  151.                 } else {
  152.  
  153.  
  154.  
  155.                         $this->loop(1);
  156.  
  157.  
  158.  
  159.                         return $this->total_combinations;
  160.  
  161.  
  162.  
  163.                 }
  164.  
  165.  
  166.  
  167.         }
  168.  
  169.  
  170.  
  171. }
  172.  
  173.  
  174.  
  175. ?>


It's usage goes like this:
PHP Code: [ Select ]
<?php
 
 
 
$possible_values = array( "1" => array( "beginning" => "0",
 
                                        "end"       => "99",
 
                                        "increment" => "1" ),
 
                          "2" => array( "beginning" => "0",
 
                                        "end"       => "60",
 
                                        "increment" => "1" ),
 
                          "3" => array( "beginning" => "0",
 
                                        "end"       => "29",
 
                                        "increment" => "1" ) );
 
 
 
$combination = new combination_lock($possible_values, "numbers.txt");
 
$total = $combination->create_possibilities();
 
 
 
echo "\nTotal combinations: ".$total."\n";
 
 
 
?>
  1. <?php
  2.  
  3.  
  4.  
  5. $possible_values = array( "1" => array( "beginning" => "0",
  6.  
  7.                                         "end"       => "99",
  8.  
  9.                                         "increment" => "1" ),
  10.  
  11.                           "2" => array( "beginning" => "0",
  12.  
  13.                                         "end"       => "60",
  14.  
  15.                                         "increment" => "1" ),
  16.  
  17.                           "3" => array( "beginning" => "0",
  18.  
  19.                                         "end"       => "29",
  20.  
  21.                                         "increment" => "1" ) );
  22.  
  23.  
  24.  
  25. $combination = new combination_lock($possible_values, "numbers.txt");
  26.  
  27. $total = $combination->create_possibilities();
  28.  
  29.  
  30.  
  31. echo "\nTotal combinations: ".$total."\n";
  32.  
  33.  
  34.  
  35. ?>

If you have another wheel, just add it to the array.

Whew, that was deceptively hard! It didn't help that that's the first time I've ever tried to use recursion!
  • o[tter]
  • Student
  • Student
  • User avatar
  • Posts: 80
  • Loc: Seattle, WA, USA

Post 3+ Months Ago

Good show old chap.
  • Mas Sehguh
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1853

Post 3+ Months Ago

Rabid Dog wrote:
Sam anything wrong with my maths here?


Now, I'm not a math nazi, I'm just an encryption nazi... You know I won't tolerate anything that can be broken by a team of supercomputer-weilding cryptanalysts working for ten years...

Though I wonder what kind of lock would have three numbers run from 00 to 60. You'd think it'd be 00 to 59... ;-)

An interesting problem is generating all the possible combinations of everyday Master Locks (well, maybe not the latest model, but the earlier ones). In the locks I'm talking about, the numbers run from 0 to 39, but there are some interesting facets. When you divide the first and third number by four, you get the same remainder. When you divide the second number by four, your remainder is two off the first and third's. Plus, there are some special rules -- the third number can't be two less or two more than the second number, and the second number can't be two less or six less than the first number. (I am not so sure about the second statement. It might be two/six more instead of less. Or it might be two more and two less. I can't remember.)

But anyway, outputting the possible Master lock combinations would be an interesting task. There are only 40 * 64 = 2560 combinations to output.
  • Maedhros
  • Proficient
  • Proficient
  • User avatar
  • Posts: 325
  • Loc: Durham, England

Post 3+ Months Ago

I was bored, so I had a go. :)

PHP Code: [ Select ]
<?php
 
 
 
class master_lock {
 
 
 
        var $file_handle;
 
        var $total_combinations = "0";
 
 
 
        function master_lock($file) {
 
 
 
                $this->file_handle = fopen($file, "w+");
 
 
 
        }
 
 
 
        function loop($n) {
 
 
 
                global $a;
 
 
 
                for ($i = 0; $i < 40; $i++) {
 
 
 
                        $a[$n] = $i;
 
 
 
                        if ($n < 3) {
 
                                $this->loop($n+1);
 
                        } else if ($this->sanity_check($a)) {
 
 
 
                                $combination = "";
 
 
 
                                foreach ($a as $number) {
 
                                        $number = $this->pad($number);
 
 
 
                                        if (!empty($combination)) $combination .= "-";
 
                                        $combination .= "$number";
 
 
 
                                }
 
 
 
                                fwrite($this->file_handle, $combination."\n");
 
 
 
                                $this->total_combinations += 1;
 
 
 
                        }
 
 
 
                }
 
 
 
        }
 
 
 
        function pad($value) {
 
 
 
                $length = strlen($value);
 
                return $value = str_repeat("0", (2 - $length)) . $value;
 
 
 
        }
 
 
 
        function sanity_check($array) {
 
 
 
                // Remainder rule:
 
                if (($a[0] % 4) != ($a[2] % 4)) {
 
 
 
                        return FALSE;
 
 
 
                }
 
 
 
                // If the remainder from the secondis two less or two more than that of
 
                // the first / third number:            
 
                $difference = ($a[0] % 4) - ($a[1]);
 
                if (($difference == "2") || ($difference == "-2")) {
 
 
 
                        return FALSE;
 
 
 
                }
 
 
 
 
 
                // Third minus second can't equal plus or minus 2:
 
                // Bah, let's actually do it... thanks o[tter]!
 
                $difference = ($a[2] - $a[1]);
 
                if (($difference == "2") || ($difference == "-2")) {
 
 
 
                        return FALSE;
 
 
 
                }
 
 
 
                $difference = ($a[0] - $a[1]);
 
                if (($difference == "2") || ($difference == "6")) {
 
 
 
                        return FALSE;
 
 
 
                }
 
 
 
                return TRUE;
 
 
 
        }
 
 
 
}
 
 
 
$lock = new master_lock("master_lock.txt");
 
 
 
$lock->loop(0);
 
 
 
echo "\nTotal combinations: ".$lock->total_combinations."\n";
 
 
 
?>
  1. <?php
  2.  
  3.  
  4.  
  5. class master_lock {
  6.  
  7.  
  8.  
  9.         var $file_handle;
  10.  
  11.         var $total_combinations = "0";
  12.  
  13.  
  14.  
  15.         function master_lock($file) {
  16.  
  17.  
  18.  
  19.                 $this->file_handle = fopen($file, "w+");
  20.  
  21.  
  22.  
  23.         }
  24.  
  25.  
  26.  
  27.         function loop($n) {
  28.  
  29.  
  30.  
  31.                 global $a;
  32.  
  33.  
  34.  
  35.                 for ($i = 0; $i < 40; $i++) {
  36.  
  37.  
  38.  
  39.                         $a[$n] = $i;
  40.  
  41.  
  42.  
  43.                         if ($n < 3) {
  44.  
  45.                                 $this->loop($n+1);
  46.  
  47.                         } else if ($this->sanity_check($a)) {
  48.  
  49.  
  50.  
  51.                                 $combination = "";
  52.  
  53.  
  54.  
  55.                                 foreach ($a as $number) {
  56.  
  57.                                         $number = $this->pad($number);
  58.  
  59.  
  60.  
  61.                                         if (!empty($combination)) $combination .= "-";
  62.  
  63.                                         $combination .= "$number";
  64.  
  65.  
  66.  
  67.                                 }
  68.  
  69.  
  70.  
  71.                                 fwrite($this->file_handle, $combination."\n");
  72.  
  73.  
  74.  
  75.                                 $this->total_combinations += 1;
  76.  
  77.  
  78.  
  79.                         }
  80.  
  81.  
  82.  
  83.                 }
  84.  
  85.  
  86.  
  87.         }
  88.  
  89.  
  90.  
  91.         function pad($value) {
  92.  
  93.  
  94.  
  95.                 $length = strlen($value);
  96.  
  97.                 return $value = str_repeat("0", (2 - $length)) . $value;
  98.  
  99.  
  100.  
  101.         }
  102.  
  103.  
  104.  
  105.         function sanity_check($array) {
  106.  
  107.  
  108.  
  109.                 // Remainder rule:
  110.  
  111.                 if (($a[0] % 4) != ($a[2] % 4)) {
  112.  
  113.  
  114.  
  115.                         return FALSE;
  116.  
  117.  
  118.  
  119.                 }
  120.  
  121.  
  122.  
  123.                 // If the remainder from the secondis two less or two more than that of
  124.  
  125.                 // the first / third number:            
  126.  
  127.                 $difference = ($a[0] % 4) - ($a[1]);
  128.  
  129.                 if (($difference == "2") || ($difference == "-2")) {
  130.  
  131.  
  132.  
  133.                         return FALSE;
  134.  
  135.  
  136.  
  137.                 }
  138.  
  139.  
  140.  
  141.  
  142.  
  143.                 // Third minus second can't equal plus or minus 2:
  144.  
  145.                 // Bah, let's actually do it... thanks o[tter]!
  146.  
  147.                 $difference = ($a[2] - $a[1]);
  148.  
  149.                 if (($difference == "2") || ($difference == "-2")) {
  150.  
  151.  
  152.  
  153.                         return FALSE;
  154.  
  155.  
  156.  
  157.                 }
  158.  
  159.  
  160.  
  161.                 $difference = ($a[0] - $a[1]);
  162.  
  163.                 if (($difference == "2") || ($difference == "6")) {
  164.  
  165.  
  166.  
  167.                         return FALSE;
  168.  
  169.  
  170.  
  171.                 }
  172.  
  173.  
  174.  
  175.                 return TRUE;
  176.  
  177.  
  178.  
  179.         }
  180.  
  181.  
  182.  
  183. }
  184.  
  185.  
  186.  
  187. $lock = new master_lock("master_lock.txt");
  188.  
  189.  
  190.  
  191. $lock->loop(0);
  192.  
  193.  
  194.  
  195. echo "\nTotal combinations: ".$lock->total_combinations."\n";
  196.  
  197.  
  198.  
  199. ?>


I suspect I may have missed something vital, though, as this gives me 2560000 possibilities...

Edit: Woah! That gave me a 30mb file! :shock:
  • o[tter]
  • Student
  • Student
  • User avatar
  • Posts: 80
  • Loc: Seattle, WA, USA

Post 3+ Months Ago

Maedhros wrote:
PHP Code: [ Select ]
                // Third minus second can't equal plus or minus 2:
                $difference = ($a[3] - $a[2]);
  1.                 // Third minus second can't equal plus or minus 2:
  2.                 $difference = ($a[3] - $a[2]);

Think you've got a problem there...
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Maedhros wrote:
Everything seems to work, except your total - you add $k to it, which at some points would add 60 lines to it, despite there only being one. If you replace $total = $total + $k with $total++, it gives the right number of combinations (as in, the number of lines in the file): 226981.


The total could have been calculated as
Code: [ Select ]
61 * 61 * 61 = 226981


I still haven't got this thing right. I am trying to find a mathematical formula to reduce my code size.

Nice one on the class there M
  • Mas Sehguh
  • Mastermind
  • Mastermind
  • User avatar
  • Posts: 1853

Post 3+ Months Ago

Well here's it in C++, no two adjacent numbers being two spots apart...

Code: [ Select ]
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ofstream ostr("output.txt");

    int count(0);

    for (int firstnum(0); firstnum < 40; ++ firstnum)
    {
        for (int secondnum((firstnum & 3) ^ 2);
            secondnum < 40; secondnum += 4)
        {
            while ((firstnum - secondnum + 40) % 40 == 2 || (firstnum - secondnum + 40) % 40 == 38)
                secondnum += 4;
            if (secondnum >= 40) break;
            for (int thirdnum(firstnum & 3);
                thirdnum < 40; thirdnum += 4)
            {
                while ((thirdnum - secondnum + 40) % 40 == 2 || (thirdnum - secondnum + 40) % 40 == 38)
                    thirdnum += 4;
                if (thirdnum >= 40) break;

                ostr << (firstnum < 10 ? "0" : "") << firstnum << '-'
                    << (secondnum < 10 ? "0" : "") << secondnum << '-'
                    << (thirdnum < 10 ? "0" : "") << thirdnum << endl;
                count++;
            }
        }
    }
    ostr << "Count: " << count << endl;

    return 0;
}
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4. int main()
  5. {
  6.     ofstream ostr("output.txt");
  7.     int count(0);
  8.     for (int firstnum(0); firstnum < 40; ++ firstnum)
  9.     {
  10.         for (int secondnum((firstnum & 3) ^ 2);
  11.             secondnum < 40; secondnum += 4)
  12.         {
  13.             while ((firstnum - secondnum + 40) % 40 == 2 || (firstnum - secondnum + 40) % 40 == 38)
  14.                 secondnum += 4;
  15.             if (secondnum >= 40) break;
  16.             for (int thirdnum(firstnum & 3);
  17.                 thirdnum < 40; thirdnum += 4)
  18.             {
  19.                 while ((thirdnum - secondnum + 40) % 40 == 2 || (thirdnum - secondnum + 40) % 40 == 38)
  20.                     thirdnum += 4;
  21.                 if (thirdnum >= 40) break;
  22.                 ostr << (firstnum < 10 ? "0" : "") << firstnum << '-'
  23.                     << (secondnum < 10 ? "0" : "") << secondnum << '-'
  24.                     << (thirdnum < 10 ? "0" : "") << thirdnum << endl;
  25.                 count++;
  26.             }
  27.         }
  28.     }
  29.     ostr << "Count: " << count << endl;
  30.     return 0;
  31. }


Edit: And here is the Reverse Polish LISP version:

Code: [ Select ]
«
"" 'output.txt' STO
0
0
39
FOR F
 F 2 + 4 MOD
 DUP 36 +
 FOR S
  WHILE
  S F - 2 + 40 MOD 2 - ABS 2 ==
  REPEAT
   S 4 + 'S' STO
  END
  IF
  S 40 <
  THEN
   ""
   F 4 MOD
   DUP 36 +
   FOR T
    WHILE
    T S - 2 + 40 MOD 2 - ABS 2 ==
    REPEAT
     T 4 + 'T' STO
    END
    IF
    T 40 <
    THEN
     F 10 < "0" "" IFTE + F +
     S 10 < "-0" "-" IFTE + S +
     T 10 < "-0" "-" IFTE + T +
     "
" +
     SWAP 1 + SWAP
    END
   4 STEP
   output.txt SWAP +
   'output.txt' STO
  END
 4 STEP
NEXT
»
  1. «
  2. "" 'output.txt' STO
  3. 0
  4. 0
  5. 39
  6. FOR F
  7.  F 2 + 4 MOD
  8.  DUP 36 +
  9.  FOR S
  10.   WHILE
  11.   S F - 2 + 40 MOD 2 - ABS 2 ==
  12.   REPEAT
  13.    S 4 + 'S' STO
  14.   END
  15.   IF
  16.   S 40 <
  17.   THEN
  18.    ""
  19.    F 4 MOD
  20.    DUP 36 +
  21.    FOR T
  22.     WHILE
  23.     T S - 2 + 40 MOD 2 - ABS 2 ==
  24.     REPEAT
  25.      T 4 + 'T' STO
  26.     END
  27.     IF
  28.     T 40 <
  29.     THEN
  30.      F 10 < "0" "" IFTE + F +
  31.      S 10 < "-0" "-" IFTE + S +
  32.      T 10 < "-0" "-" IFTE + T +
  33.      "
  34. " +
  35.      SWAP 1 + SWAP
  36.     END
  37.    4 STEP
  38.    output.txt SWAP +
  39.    'output.txt' STO
  40.   END
  41.  4 STEP
  42. NEXT
  43. »
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Ha ha got the formula! Will post it once I have created the class
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

Post Information

  • Total Posts in this topic: 32 posts
  • Users browsing this forum: Liamw411 and 40 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.