Pi Hates Nines

Discussion of challenges you have already solved
karategeek6
Posts: 1
Joined: Sat Jun 18, 2011 6:50 am

My python solution

Post 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.
User avatar
PainKeeper
Posts: 4
Joined: Tue Jul 19, 2011 7:30 pm
Location: Germany

Post 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];
Taurath
Posts: 5
Joined: Mon Feb 06, 2012 7:15 pm

Post 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]
avrrobot
Posts: 51
Joined: Fri Mar 04, 2011 2:54 pm
Location: Germany

Post 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]
Shirosan
Posts: 10
Joined: Sat May 28, 2011 3:40 am

Post by Shirosan »

http://www.piday.org/million.php

then a simple PHP loop as the same as livinskull posted above, easy peasy
xenonr
Posts: 1
Joined: Wed May 11, 2011 9:59 am

Post by xenonr »

another PHP
$array = explode("9", $pi);

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

usort($array,'sortByLength');

echo $array[0];
net-fabrikken
Posts: 1
Joined: Thu Jan 30, 2014 9:44 pm

Post 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;
?>
CptObvious
Posts: 3
Joined: Wed Apr 16, 2014 2:53 pm

Post 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);
}
User avatar
xiexun162534
Posts: 4
Joined: Fri Jul 26, 2013 1:26 pm
Contact:

Post 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;
}
I'm boring.
Mad Mike
Posts: 11
Joined: Tue Jun 21, 2011 4:38 pm

Post 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
chown -R us ./base
eulerscheZahl
Posts: 58
Joined: Thu Nov 29, 2012 7:45 pm
Location: Germany

Post 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.
th4wri
Posts: 8
Joined: Tue Mar 29, 2016 10:27 am
Location: TuNiSiA

Post 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.
Post Reply