Pi Hates Nines
Pi Hates Nines
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…
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…
-
- Posts: 7
- Joined: Thu Mar 05, 2009 2:22 pm
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
-
- Posts: 7
- Joined: Tue May 19, 2009 12:02 am
python
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
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)
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
note: i'm not necessarily a mad hacker, i'ts my screen name
Perl
$pstr contains the first million digits of pi. The rest is pretty self explanatory.
Code: Select all
my @seq = split(/9/, $pstr);
my $idxmax = 0;
$seq[$idxmax] > $seq[$_] or $idxmax =$_ for 1 .. $#seq;
print $seq[$idxmax], "\n";
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]);
}
}
}
}
pi.txt contains one line with pi
-
- Posts: 4
- Joined: Tue Feb 09, 2010 10:42 pm
my ruby code
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
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
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.
smagazor
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;
}
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();
}
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());
}
-
- Posts: 12
- Joined: Sun Oct 26, 2008 4:33 pm
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
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
- livinskull
- Posts: 22
- Joined: Fri Jun 26, 2009 12:01 pm
- Location: /dev/null
- Contact:
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";
}
}
-
- Forum Admin
- Posts: 496
- Joined: Sat May 28, 2011 9:14 am
- Location: Germany
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;
}