1. Member Join Date
Mar 2011
Posts
27
Rep Power
0

## Calculus & java

yesterday i was study calculus " An Increasing / decreasing seq " ..

in calculus we solve these problems as following :

given (an), and find if sequences is Inc or Dec.
we have to find an+1 first .
then divide an+1/an ..
if the result < 1 so it is decreasing , otherwise it is increasing.
i solved many problems with many cases .. factorial , exponential functions .. etc . I said to myself .. if i have program to make my life easier and give me the answer directly that will save my time .. and my effort :rolleyes: .

an = n .
an+1 = n+1
an+1/an = n+1/n .... n+1 > n .. so that it is increasing .
if i could write this program .. then i believe i can write complex cases
such as factorial , ..etc.

Now ,

input is an .. which user give to the program .
of course ! input maybe n or n+1 , n+2 .. etc.
but what i have focus on is replace each n by( n+1 ) !

this what i did until now ! , because i started programming using java script , and am just little bit confused about java .. please tell me does until
now am on the line .. give me yours advice to improve myself ..
am stack little bit .. but i believe i can complete this program.

Java Code:
```    public static void main(String[] args) {
// TODO code application logic here
Scanner input = new Scanner(System.in);
String an = input.nextLine();
int a = an.length();
System.out.println("the an length is"+"["+a+"]");
char []arr=an.toCharArray();
String aa = "n+1";

for(int index=0;index<arr.length;index++)
{
System.out.println("the CharArray is"+"["+arr[index]+"]");
}
for(int i =0;i<arr.length;i++)

if(an.charAt(i)=='n')
{
System.out.println("the new is"+"["+aa+"]");
}
else
{
System.out.println("the new is"+"FAILED ");
}
}```
Last edited by SHE; 03-28-2011 at 03:29 PM.  Reply With Quote

2. ## Can you please tell us in ordinary words what you're trying to do because I (we) understand zilch from your code ...

kind regards,

Jos  Reply With Quote

3. Member Join Date
Mar 2011
Posts
27
Rep Power
0

## Program determine whether the sequence Increasing or Decreasing.

The input of program is : an.
[an] should contain (n) !

steps to determine whether this input is Increasing or Decreasing.
1. count an+1 .. by replace each n by n+1 ..
for example if an = n+1 , an+1 = (n+1)+1= n+2 and so on.
.
.

step two :
divide an+1/an ..
case one if the result < 1 // it is decreasing , other wise .. it is increasing.  Reply With Quote

4. ##  Originally Posted by SHE Program determine whether the sequence Increasing or Decreasing.

The input of program is : an.
[an] should contain (n) !

steps to determine whether this input is Increasing or Decreasing.
1. count an+1 .. by replace each n by n+1 ..
for example if an = n+1 , an+1 = (n+1)+1= n+2 and so on.
.
.

step two :
divide an+1/an ..
case one if the result < 1 // it is decreasing , other wise .. it is increasing.
Do you want to do it all symbolically? If you want to do it numerically all you have to check is whether or nor the next entry is larger than the current entry ... I still think I don´t understand what you want to do ...

kind regards,

Jos  Reply With Quote

5. Member Join Date
Mar 2011
Posts
27
Rep Power
0

## let's take an = n + 1
an+1 will be = n+2
an+1/an = n+2/n+1 .. you can notice now ( an+1 )is greater than (an ) .. so it is increasing , this simplest case for these problems.
.
.
so now i faced two things :
1. the input will be String .. , if i replace each n by n+1 , after the operation how i can sum the numbers when the input is String , so that i thought about the char Array !
but i still can't sum the numbers and keep the n . ( this the first thing which i want to know .. and i think after that i can do the step two)

I hope you understand my point ^^"  Reply With Quote

6. ##  Originally Posted by SHE let's take an = n + 1
an+1 will be = n+2
an+1/an = n+2/n+1 .. you can notice now ( an+1 )is greater than (an ) .. so it is increasing , this simplest case for these problems.
.
.
so now i faced two things :
1. the input will be String .. , if i replace each n by n+1 , after the operation how i can sum the numbers when the input is String , so that i thought about the char Array !
but i still can't sum the numbers and keep the n . ( this the first thing which i want to know .. and i think after that i can do the step two)

I hope you understand my point ^^"
Ok, give us a few examples of what you want to type as the input for your program; I suspect that you want to do this symbolically.

kind regards,

Jos  Reply With Quote

7. Member Join Date
Mar 2011
Posts
27
Rep Power
0

## i already putted this example :

let's take an = n + 1
an+1 will be = n+2
an+1/an = n+2/n+1
anyway this another example :
the input (n+3)/(n+2)
because these operations about sequences so we need to know what is after this input.
so that program will replace each n by (n+1).. the output which display to the user
should be as following n+4/n+3 .. this first step.

let's see my code :

Java Code:
```        Scanner input = new Scanner(System.in);
String an = input.nextLine();
[COLOR="Red"]// HERE THE PROGRAM  WILL TAKE THE INPUT FROM USER. [/COLOR]
char []arr=an.toCharArray();
[COLOR="Red"]// Now i putted the input to char array to be able to me change each n . [/COLOR]
String aa = "n+1"; // an+1 is constant.```
But Then ...
I MADE THESE CHANGES WHICH ARE WRONG , THAT IN RED ....
Java Code:
```        Scanner input = new Scanner(System.in);
String an = input.nextLine();
char []arr=an.toCharArray();
[COLOR="Red"]char A ='n'+'1';[/COLOR]
System.out.println(A);
for(int index=0;index<arr.length;index++)
{
System.out.println("the CharArray is"+"["+arr[index]+"]");
}
for(int i =0;i<arr.length;i++)

if(an.charAt(i)=='n')
{
char a = an.charAt(i);
[COLOR="Red"] an.replace(a,A);[/COLOR]
}
else
{
System.out.println("the new is"+"?????");
}
}```  Reply With Quote

8. ##  Originally Posted by SHE i already putted this example :

anyway this another example :
the input (n+3)/(n+2)
because these operations about sequences so we need to know what is after this input.
so that program will replace each n by (n+1).. the output which display to the user
should be as following n+4/n+3 .. this first step.
Hold it for a minute; if your input is "(n+3)/(n+2)" and you replace each "n" by "n+1", you get "(n+1+3)/(n+1+2)"; it takes a lot more logic to find out that you want "(n+4)/(n+3)". And what if the input was "n/2"; if you replace each "n" by "n+1" you get "n+1/2" which is totally wrong ...

kind regards,

Jos  Reply With Quote

9. Member Join Date
Mar 2011
Posts
27
Rep Power
0

## yeah this logic is what am asking about .. which i have no idea about how i can implement it in java.

if input n/2 .. output will n+1/2 .. !

step two .. i have to divide n+1/2 / n/2 .. and then i have do the multiplication .. will get
n+1/2 * 2/n & the final answer is .. n+1/n .. after if statement will find that
input is increasing.

so that i said what i want to try it now is simple case if it is work then i have to think
in other cases ... there is sin , cos , factorial .. etc.  Reply With Quote

10. ##  Originally Posted by SHE yeah this logic is what am asking about .. which i have no idea about how i can implement it in java.

if input n/2 .. output will n+1/2 .. !

step two .. i have to divide n+1/2 / n/2 .. and then i have do the multiplication .. will get
n+1/2 * 2/n & the final answer is .. n+1/n .. after if statement will find that
input is increasing.

so that i said what i want to try it now is simple case if it is work then i have to think
in other cases ... there is sin , cos , factorial .. etc.
So after 10 replies or so we can conclude that you want to solve this problem symbolically and you have no idea how to do it, right? I can tell you that a textual substitution, as your tried, is never going to work. You need to know the 'depth structure' of the expression, so you need to parse the textual representation and generate an AST (Abstract Syntax Tree) from it. Only then you can substitute the expression n+1 for the sub-expression n.

kind regards,

Jos  Reply With Quote

11. Member Join Date
Mar 2011
Posts
27
Rep Power
0

## Ops ! so do you know good references (books, websites .. etc) which i can learn more about textual representation and generate an AST  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•