Pi Hates Nines

Discussion of challenges you have already solved
wrtlprnft
Posts: 28
Joined: Sun Nov 09, 2008 4:48 pm

Pi Hates Nines

Post by wrtlprnft »

The example is a bit misleading… I thought I was supposed to search for the longest sequence of incrementing numbers (more than one) not containing a nine, ie 234567 or possibly even 83018302 (maybe there's something longer, I haven't checked much further).

Well, searching the forums and seeing someone mentioning a 100 digit sequence (which would never happen with my interpretation of the challenge) unstuck me, but still…
User avatar
yes-man
Posts: 32
Joined: Fri Jan 30, 2009 5:14 pm

Post by yes-man »

After solving the 12345678 in pi challenge, this was just a matter of rephrasing the grep statement.
Bierdeckel
Posts: 7
Joined: Thu Mar 05, 2009 2:22 pm

Post by Bierdeckel »

Code: Select all

array = string.split("9")  // string = Pi
Dim Longest as integer, Pos as integer
Longest = 0
Pos = 0
for i = 0 to ubound(array)
   if longest < array(i).lenght then 
      longest = array(i).length
      pos = i
            //here is the code searching the 
            //longest string of the array.
   end if
next
msgbox(Pos & "   " & array(pos))  //result
//

solved in vb.net
Mad Hacker
Posts: 7
Joined: Tue May 19, 2009 12:02 am

Post by Mad Hacker »

python

Code: Select all

from pi.py import pi

curstr = ""
bigstr = ""

for j in pi:
	if j == "9":
		if len(curstr) > len(bigstr):
			bigstr = curstr
		curstr = ""
	elif j != " " and j != "\t":
		curstr += j
		
print(bigstr)
pi contains the first million digits of pi(found online, thus the checking for spaces and tabs, as i didn't know how long it would take to remove them, and didn't have much reason to do so)
sorry, i don't have pi.py anymore
Last edited by Mad Hacker on Fri Nov 27, 2009 11:23 pm, edited 1 time in total.
~~~~
note: i'm not necessarily a mad hacker, i'ts my screen name
xulfer
Posts: 5
Joined: Mon Nov 02, 2009 11:27 am

Post by xulfer »

Perl

Code: Select all

my @seq = split(/9/, $pstr);
my $idxmax = 0;
$seq[$idxmax] > $seq[$_] or $idxmax =$_ for 1 .. $#seq;
print $seq[$idxmax], "\n";
$pstr contains the first million digits of pi. The rest is pretty self explanatory.
Quurks
Posts: 2
Joined: Tue Nov 04, 2008 4:46 pm

Post by Quurks »

Code: Select all

import java.io.*;

public class PIHatesNines {

	public static void main(String[] args) throws IOException {

		BufferedReader br = new BufferedReader(new FileReader(new File("pi.txt")));
		String s = br.readLine();
		String[] split=s.split("9");		
		int max=0;
		int pos=0;
		for (int i = 0; i < split.length; i++) {
			if(split[i].length()>max){
				pos=i;
				max=split[i].length();
				System.out.println(max+": "+split[pos]);
			}
		}
	}
}
Java
pi.txt contains one line with pi
SecurityCrawler
Posts: 4
Joined: Tue Feb 09, 2010 10:42 pm

Post by SecurityCrawler »

my ruby code

Code: Select all

pi = File.open("pi.txt", "r").readlines.to_s
max = ""
pi[2..1000002].to_s.split('9').each { |val| max = val if val.length > max.length }
puts max
my pi.txt have 10mio digits. i read the whole file but just use the first 1mio digits after comma [2..1000002] for the search
Joeb27
Posts: 4
Joined: Sat Oct 16, 2010 11:28 pm

Post by Joeb27 »

And in one line:

# sed -e 's/9/\n/g' pi-file.txt | sort -n | tail -1


(Replace all '9' by newlines and sort numericaly. The tail just shows the last line.)
smagazor
Posts: 2
Joined: Thu Oct 21, 2010 12:47 pm
Location: Romania

Post by smagazor »

Had fun while solving it by reading from the file using a while loop instead of a for one and by printing the answer also through a while.

Code: Select all

#include <stdio.h>
#include <conio.h>

int main(){
    FILE *op,*oq;
    oq=fopen("10000000.txt","r");
    op=fopen("answer.txt","w");
    int Chr,Chr2;
    
    Chr2=fgetc(oq);
    long int poz=1,poz_ant=1,dist=0,sav=0;
    while(!feof(oq)){
        Chr = fgetc(oq);
        if (Chr=='9'){
           if ((poz-poz_ant>dist) && (Chr2!='9')){
              dist=poz-poz_ant;
              sav=poz_ant;
              poz_ant=poz+1;    }
           else poz_ant=poz+1; 
                       }
        poz++;  
        Chr2=Chr;
                     }
     // printf("%d",dist);
    fseek(oq,0,SEEK_SET);
    long int i=0;
    while(!feof(oq)){
        Chr=fgetc(oq);
        if (i==sav){
            fprintf(op,"%c",Chr);
            for(long int j=i;j<i+dist-1;j++){
                Chr=fgetc(oq);
                fprintf(op,"%c",Chr);
                                             } 
            break;
                   } 
        i++;        }

    fclose(oq);
    fclose(op);
    getch();
    return 0;
          }
smagazor
Guderian
Posts: 18
Joined: Tue Jan 25, 2011 4:31 pm
Location: Greece

Post by Guderian »

i created this function in c++ which outputs the result in a file

Code: Select all

#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void pi_hates_nines(){
	ifstream myfile("pi6.txt");
	ofstream output;
	char x;
	int count=0;
	int max=0;
	int i;
	char temp[10000];
	char result[10000];
	while (!myfile.eof()){
		myfile >> x;
		if (x=='\n')
			continue;
		if (x!='9'){
			temp[count]=x;
			count++;
		} else {
			if(count>max) {
				//copy temp to result
				for (i=0;i<count;i++){
					result[i]=temp[i];
				}
				max=count;
			}
			result[i]='\0';
			count=0;	
		}
	}
	output.open("pi_hates_9.txt");
	output <<"Result:"<<result<<endl;
	output.close();
	cout <<"Done!" <<endl;
}

void main() {
	pi_hates_nines();
}

User avatar
SinistraD
Posts: 89
Joined: Sun Aug 16, 2009 8:39 am
Location: find me
Contact:

Post by SinistraD »

lol, Joeb27 rulz. And I thought SED was a good for nothing language. Thanks for proving me wrong.
DamaTeq
Posts: 8
Joined: Tue Feb 01, 2011 7:40 pm

Post by DamaTeq »

solved in c#

Code: Select all

using (StreamReader sr = new StreamReader(File.OpenRead("pi.txt")))
{
      Console.WriteLine(sr.ReadToEnd().Split('9').OrderByDescending(x => x.Length).First());
}
;)
Schnapphahn
Posts: 12
Joined: Sun Oct 26, 2008 4:33 pm

Post by Schnapphahn »

OMG - I did it, but I don't tell how.







ok, after visiting
http://preview.tinyurl.com/28kwxch

and trimming a bit (Hex-Editor did it)

using LibreOffice (to the max) sort-function.

After all: Voila
User avatar
livinskull
Posts: 22
Joined: Fri Jun 26, 2009 12:01 pm
Location: /dev/null
Contact:

Post by livinskull »

PHP

Code: Select all

$sPi = file_get_contents('pi_1st_million');

for ($i=0, $sLongest = $tmp = '', $max = strlen($sPi); $i<$max; ++$i, $tmp = '') {
	while ($i<$max && $sPi[$i] != '9') 
		$tmp .= $sPi[$i++];

	if (strlen($tmp) > strlen($sLongest)) {
		$sLongest = $tmp;
		echo $sLongest."\n";
	}
}
AMindForeverVoyaging
Forum Admin
Posts: 496
Joined: Sat May 28, 2011 9:14 am
Location: Germany

Post by AMindForeverVoyaging »

Another C solution:

Code: Select all

#include <stdio.h>
#include <string.h>

int main()
{
	FILE *f = fopen("pi_million.txt", "r");
	char a, b[200], c[200];
	unsigned int index = 0, longest = 0, counter = 0;
	
	while(!feof(f))
	{
		fscanf(f, "%c", &a);
		if ( a == '9' )
		{
			if ( counter > longest )
			{
				longest = counter;
				b[index] = '\0';
				strcpy(c, b);
			}
			counter = 0;
			index = 0;
		}
		else
		{
			b[index] = a;
			index++;
			counter++;
		}	
	}
	
	printf("c[]: %s\n", c);
	fclose(f);
	return 0;
}

Post Reply