Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Read file text and find fibonacci series

by darkblackblue (Novice)
on Dec 28, 2017 at 14:37 UTC ( #1206326=perlmeditation: print w/replies, xml ) Need Help??

Hi My goal is read only line from file , and find fibonacci series that has minimun 3 element.I want to control numbers from 1 to 6 digit. Fibonacci series starts 1,1,2,3,5,8,13,21,34,55,89, ...... .For example;File text document

49693463452312343388645

I division string 1 to 6 digit. Check 4 is fibonacci number , no , go ahaed 49 is fibonacci number , no, next 496 is fibonacci ,no, 4969 after 44693 ,after 496934 no.There isnt any fibonacci , go to next digit and do it again 9 , 96,969,9693,96934.

use 5.010; use strict; use warnings; open(FILE, "<:encoding(UTF-8)", "aa.txt") or die "Could not open file: + $!"; my $numbers; while (<FILE>) { $numbers="$_" ; print "$_"; } chomp $numbers; print "\n$numbers"; my $len=length($numbers); print "\n$len\n"; my $i; my $abc; foreach my $i (0..$len){ foreach my $j (1..6) { print "$j----->"; $abc = substr($numbers,$i,$j); print "$abc\n"; } print "***********************************\n"; } close FILE;

I read to find fibonacci series with PERFECT SQUARE. I didn't use it. C code example

// C++ program to check if x is a perfect square #include <iostream> #include <math.h> using namespace std; // A utility function that returns true if x is perfect square bool isPerfectSquare(int x) { int s = sqrt(x); return (s*s == x); } // Returns true if n is a Fibinacci Number, else false bool isFibonacci(int n) { // n is Fibinacci if one of 5*n*n + 4 or 5*n*n - 4 or both // is a perferct square return isPerfectSquare(5*n*n + 4) || isPerfectSquare(5*n*n - 4); } // A utility function to test above functions int main() { for (int i = 1; i <= 10; i++) isFibonacci(i)? cout << i << " is a Fibonacci Number \n": cout << i << " is a not Fibonacci Number \n" ; + return 0; }

2017-12-29 Athanasius restored original content

Replies are listed 'Best First'.
Re: Read file text and find fibonacci series
by haukex (Abbot) on Dec 28, 2017 at 19:12 UTC

    This appears to have been posted to Meditations, but I'm not sure if you meant it to be a question? Anyway, you're describing scanning a string looking for a pattern, which is exactly what regular expressions are good at. Ignoring the duplicate 1's, there are only 29 Fibonacci numbers with 1 to 6 digits, a regex can handle that just fine (and it would have no problem handling much more). See Building Regex Alternations Dynamically.

    Update: You appear to have edited your node to remove its content, please see How do I change/delete my post? for why you shouldn't do that. Content restored, thanks Athanasius. Also made very minor fixups.

      Hi, haukex.

      while (/(?=($re))./g) { ... }

      "Look for a fib anywhere ahead, then if found, consume its leftmost digit, then repeat" probably makes no sense, even for this assignment/homework. The %fib was built but not actually used. The "3 element" part of assignment (not addressed in your code) could be either consecutive, or just ascending fibs. But, the way %fib was constructed, it may be to help looking for a fib whose initial digits are a fib too, whose initial digits, ..., etc. While leftmost digit of any 2-digit fib is a fib, -- I wonder if any value of %fib could be an array with more than 3 elements? :)

      P.S. Please, I'm not appealing to rewrite anything, don't waste your time, the task is too much unclear.

      P.P.S. And, I suspect the "PERFECT SQUARE" in OP, in C, won't work for 6-digit numbers and int type, because of overflow?

        The %fib was built but not actually used.

        Well not quite, it is used for what you said:

        to help looking for a fib whose initial digits are a fib too

        This is what I was using it for, since I didn't feel like getting too fancy with the regex. The code I wrote was mostly just an implementation of my interpretation of the description the OP gave:

        "Check 4 is fibonacci number , no , go ahaed 49 is fibonacci number , no, next 496 is fibonacci ,no, 4969 after 44693 [sic] ,after 496934 no.There isnt any fibonacci , go to next digit and do it again 9 , 96,969,9693,96934."
        I wonder if any value of %fib could be an array with more than 3 elements?

        Good question, I didn't try cranking up $DIGITS past 60 yet...

        the task is too much unclear

        Agreed, which is why I just implemented the part I (thought I) understood ;-)

Re: Read file text and find fibonacci series
by thanos1983 (Vicar) on Jan 02, 2018 at 18:00 UTC

    Hello darkblackblue,

    As the fellow Monks say this looks like a school assignment. Well I could suggest another approach to your problem but it only contains half of the solution, I have left the rest for you to solve.

    Sample of code:

    I used different hard coded input instead of reading your data from your file but you get the point that the input is data from your file. What I am doing on this sample of code is comparing the string part by part as you wanted with a list of fibonacci numbers. The module that I am using is Math::Fibonacci. What the script does not do is to compare the numbers by subtracting the elements one by one until the end as you described. I have left this part out.

    Update: I just observed that the darkblackblue has asked the question since the 28th December. I assume that he/she was not able to resolve it. Just to add complete answer to the question for future reference see sample of code bellow:

    Hope this helps, BR.

    Seeking for Perl wisdom...on the process of learning...not there...yet!
Re: Read file text and find fibonacci series
by aartist (Monk) on Jan 02, 2018 at 15:14 UTC
    Is it a programming exercise or you are solving a mathematical problem?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://1206326]
Approved by Corion
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2018-07-17 22:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (379 votes). Check out past polls.

    Notices?