Page 2 of 2

My python solution

Posted: Sat Jul 02, 2011 6:14 am
by karategeek6

Code: Select all

pi = open('pi.txt', 'r').read().split('9')
y = map(lambda x: len(x), pi)
print(pi[y.index(max(y))])
Pi.txt is a text file containing pi to a million decimal places. Found the number at 3.141592653589793238462643383279502884197169399375105820974944592.com, copy pasted into a text file and used vim to clean it up.

Posted: Tue Jul 26, 2011 10:42 am
by PainKeeper

Code: Select all

#perl
open (HD, "<pi1000000.txt") || die "file not found";
$pi = <HD>;
close (HD);

@bla = split (/9/, $pi);
@foo = reverse sort {length $a <=> length $b} @bla;
print @foo[0];

Posted: Tue Mar 20, 2012 2:22 am
by Taurath
Mine in Ruby:

Code: Select all

file = File.open("millionpi", "r")
line = file.gets
bigdist = 0
n = 0
a = (0 .. line.length - 1).find_all {|i| line[i,1] == '9'} ##gives the location of each 9 in line
for i in 1..a.length - 1 do
  dist = a[i] - a[i-1]
  if bigdist < dist
    bigdist = dist
    n = a[i-1]
  end
end
puts line[n+1..n+bigdist-1]

Posted: Mon May 28, 2012 10:01 am
by avrrobot
[code]
int main()
{
uint64_t counts[10];
char a[200],s[200];
for(uint8_t o=0;o<200;o++)
{
a[o]=0;
s[o]= 0;
}
for(uint8_t i=0;i<10;i++)counts[i] = 0;
FILE *f;
f = fopen("pi_data.txt","r");
char c;
int anz = 0;
int nine_seq = 0,long_seq = 0;
do
{
c = fgetc(f);
if(c >= 48 && c <= 57)
{
counts[c-48]++;
if(c == 57)
{
if(nine_seq > long_seq)
{
long_seq = nine_seq;
for(uint8_t i=0;i<200;i++)s[i] = a[i];
}
for(uint8_t o=0;o<200;o++)
{
a[o]=0;
}
nine_seq = 0;
}
else
{
a[nine_seq] = c;
nine_seq++;
}
}
anz = 0;
for(uint8_t i=0;i<10;i++) anz += counts[i];
}while(c != EOF);
fclose(f);
FILE *sf;
sf = fopen("erg.txt","w");
for(uint8_t i=0;i<124;i++)
{
fputc(s[i],sf);
}
fclose(sf);
return 0;
}
[/code]

Posted: Sun Sep 23, 2012 7:58 pm
by Shirosan
http://www.piday.org/million.php

then a simple PHP loop as the same as livinskull posted above, easy peasy

Posted: Tue Mar 19, 2013 2:15 pm
by xenonr
another PHP
$array = explode("9", $pi);

function sortByLength($a,$b){
return strlen($b)-strlen($a);
}

usort($array,'sortByLength');

echo $array[0];

Posted: Tue Apr 08, 2014 1:42 pm
by net-fabrikken
Yet another php code ^^

Code: Select all

<?php
$tmp = "";
$long = "";
$file = file_get_contents('./pi.txt', true);
for($i = 0; $i  <= 1000000; $i++){
	
	$substr = substr($file, $i, 1);

	if($substr == 9){
		if(strlen($tmp) > strlen($long)){
			$long = $tmp;
		}
		$tmp = ""; 
		
	} else {
			$tmp .= $substr; 
		}
}
echo $long;
?>

Posted: Fri Apr 25, 2014 10:47 am
by CptObvious
Another Java approach

Code: Select all

public static void main(String[] args) {
	
	String pi = "put pi here";
	String[] splitpi = pi.split("9");
	String longest = "";
	
	for(String s : splitpi){
		if (s.length() > longest.length()){
			longest = s;
		}
	}
	
	System.out.println(longest);
}

Posted: Wed Jul 13, 2016 9:49 am
by xiexun162534
C

Code: Select all

#include <stdio.h>

int main ()
{
  char string_longest[10000];
  char string[10000];
  int number;
  int length = 0;
  int longest_length = 0;
  while ((number = getchar ()) != EOF)
    {
      if (number == '9')
        {
          if (length > longest_length)
            {
              int i;
              for (i = 0; i < length; i ++)
                string_longest[i] = string[i];
              longest_length = length;
            }
          length = 0;
        }
      else
        {
          string[length] = number;
          length ++;
        }
    }
  string_longest[longest_length] = '\0';

  printf ("%s\n", string_longest);
  return 0;
}

Posted: Fri Dec 02, 2016 8:32 pm
by Mad Mike
Search & replace 9 to \n in Notepad++
Look for a line with over 100 characters
Adapt and search on.

Not very hack-y but my Python script sorta failed me. I have no idea how so few lines can fail so badly. For the record:

(stripped out all whitespaces from pi file after first failure)

Code: Select all

seq = []
tseq = []
# usual file blah omitted here 
for c in pi:
    if c != '9':
        tseq.append(c)  # yeah. += would have been smarter

    else:
        if len(tseq) > len(seq):
            seq = tseq
            tseq = []

print tseq

Posted: Sat Dec 03, 2016 6:46 am
by eulerscheZahl
@Mad Mike
did you want to print seq instead of tseq?
and tseq = [] sould not be inside if len(tseq) > len(seq), as it applies for all '9' characters.

Posted: Tue Aug 14, 2018 12:10 pm
by th4wri
emm java code also
but i hav another method not mentionned here
we can use one of the regex tester tools to find the chain
https://www.freeformatter.com/regex-tester.html
regex : 9[^9]{120,}9
why 120 ?
i tested with 50,70,100,110,120 until the msg
Found 1 match. Match is highlighted below.
986065706615844070167350646845510834483041034071433068861350648161231335008442336241417442522038471620685815778003440744224089
that's it.