473,396 Members | 1,770 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

need help, please

How would i go about implementing a program in java that would do the
following,

f(1) = 1; f(2) = 1; f(3) = 1; f(4) = 1; f(5) = 1;

f(n) = f( n - 1 ) + 3 * f( n - 5 ) for all n > 5

and display the results for n = 6, 7, 12, and 15

if someone can start me off, i would really appreciate it cause i'm
lost.

I know how to calulate the values on paper, but implementing a program
to do it is something else
*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
Jul 17 '05 #1
5 1705
"slickn_sly" <bn***@hotmail-dot-com.no-spam.invalid> wrote in message
news:41********@Usenet.com...
How would i go about implementing a program in java that would do the
following,

f(1) = 1; f(2) = 1; f(3) = 1; f(4) = 1; f(5) = 1;

f(n) = f( n - 1 ) + 3 * f( n - 5 ) for all n > 5

and display the results for n = 6, 7, 12, and 15

if someone can start me off, i would really appreciate it cause i'm
lost.

I know how to calulate the values on paper, but implementing a program
to do it is something else

*sniff sniff* Smells like homework. We don't do homework here. If you bring
some code to us, we'll be happy to help you straighten it out and figure out
whatever problems you run into. That said, if you can figure it out on
paper, you should be able to code it. Just break it down into the simplest
steps you can. For example, the first thing you might notice is that you
have one set of conditions for n <= 5 and another for n > 5. That's an if
statement if I've ever seen one:
if (n <= 5) {
...
} else {
...
}

Or if you prefer:
if (n > 5) {
...
} else {
...
}

That's your first "rule" governing the problem. Just break it down more from
there and fill in what you need. See http://www.physci.org/codes/sscce.jsp
for tips on posting your code to the ng for help.

Finally, please post to comp.lang.java.help in the future. This group is
deprecated.
Jul 17 '05 #2
In Python something that may be close:
def fibonacciSlow(n): if n==0:
return 0
elif n==1:
return 1
else:
return fibonacciSlow(n-1) + fibonacciSlow(n-2)

def fibonacciFastHelper(n, currFib, prevFib): if n==0:
return prevFib
elif n==1:
return currFib
else:
return fibonacciFastHelper(n-1, currFib + prevFib, currFib)

def fibonacciFast(n):

return fibonacciFastHelper(n, 1, 0)

--
Regards,
Casey
Jul 17 '05 #3
I don't know if im in the right track

public static int f( int n )
{
if( n == 1 && n == 5 )
{
return 1;
}
else
{
return f( n - 1 ) + ( 3 * f( n - 5 ) );
}
}

public static void main( String[] args )
{
System.out.println( "f( 6 ) is equal to " + f( 6 ) );
}
*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
Jul 17 '05 #4
I think you got the idea.
There is something wrong with your basic condition.

if(n == 1 && n == 5) //What if n = 2? It will cause an infinite loop.

So it should be

if(n == 0) return 0;
if(n < 6) return 1;

-jit

slickn_sly wrote:
I don't know if im in the right track

public static int f( int n )
{
if( n == 1 && n == 5 )
{
return 1;
}
else
{
return f( n - 1 ) + ( 3 * f( n - 5 ) );
}
}

public static void main( String[] args )
{
System.out.println( "f( 6 ) is equal to " + f( 6 ) );
}
*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*

Jul 17 '05 #5
thanks everyone for all the help.

I think I have enough info to figure it out.
*-----------------------*
Posted at:
www.GroupSrv.com
*-----------------------*
Jul 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: mike | last post by:
Hello, After trying to validate this page for a couple of days now I was wondering if someone might be able to help me out. Below is a list of snippets where I am having the errors. 1. Line 334,...
5
by: John Flynn | last post by:
hi all i'm going to be quick i have an assignment due which i have no idea how to do. i work full time so i dont have the time to learn it and its due date has crept up on me .. As follows:...
5
by: TrvlOrm | last post by:
HI There, I have been struggling with JavaScript code for days now, and this is my last resort! Please help... I am trying to create a JavaScript slide show with links for Next Slide,...
7
by: Timothy Shih | last post by:
Hi, I am trying to figure out how to use unmanaged code using P/Invoke. I wrote a simple function which takes in 2 buffers (one a byte buffer, one a char buffer) and copies the contents of the byte...
15
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
8
by: skumar434 | last post by:
i need to store the data from a data base in to structure .............the problem is like this ....suppose there is a data base which stores the sequence no and item type etc ...but i need only...
8
by: CM | last post by:
Hi, Could anyone please help me? I am completing my Master's Degree and need to reproduce a Webpage in Word. Aspects of the page are lost and some of the text goes. I would really appreciate it....
0
by: U S Contractors Offering Service A Non-profit | last post by:
Brilliant technology helping those most in need Inbox Reply U S Contractors Offering Service A Non-profit show details 10:37 pm (1 hour ago) Brilliant technology helping those most in need ...
21
by: asif929 | last post by:
I need immediate help in writing a function program. I have to write a program in functions and use array to store them. I am not familiar with functions and i tried to create it but i fails to...
5
by: Justin | last post by:
Here's my XML: <?xml version="1.0" ?> <AppMode Type="Network"> <CurrentFolder Path="c:\tabs"> <Tabs> <FilePath>tabs\Justin.tab</FilePath> <FilePath>tabs\Julie.tab</FilePath> *****There could...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.