Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
1.7 (Approximate p) p can be computed using the following formula:
Write a program that displays the result of 4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11)
and 4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13)
Use 1.0 instead of 1 in your 3 5 7 9 11 13
program.
My Code:
/**
* This program will print two different results in
* trying to reach a approximation of pi.
*
* Author: X Date: 11/11/2013
*/
public class one_seven
{
public static void main(String[] args)
{
System.out.print("This will print 1st approximation: ");
System.out.println(4*(1.0-(1.0/3)+(1.0/5)-(1.0/7)-(1.0/11)));
System.out.print("This will print 2nd approximation: ");
System.out.println(4*(1.0-(1.0/3)+(1.0/5)-(1.0/7)-(1.0/11)+(1.0/13)));
}
}
My output for the code is:
This will print 1st approximation: 2.531601731601732
This will print 2nd approximation: 2.83929403929404
You're missing the 1.0/9 term.
System.out.print("This will print 1st approximation: ");
System.out.println(4*(1.0-(1.0/3)+(1.0/5)-(1.0/7)+(1.0/9)-(1.0/11)));
System.out.print("This will print 2nd approximation: ");
System.out.println(4*(1.0-(1.0/3)+(1.0/5)-(1.0/7)+(1.0/9)-(1.0/11)+(1.0/13)));
In both approximations you left out + (1.0/9.0)
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
Given base and n that are both 1 or more, compute recursively (no
loops) the value of base to the n power, so powerN(3, 2) is 9 (3
squared).
The answer is
public int powerN(int base, int n) {
if(n == 1)
return base;
return base * powerN(base, n - 1);
}
I am confused because when I look at this because is this not saying multiply the base against the returned number of powerN()?
powerN(3,3);
= 3
= 3*powerN(base, n-1) = 6
= 3*powerN(base, n-1) = 9
Which would multiply 9*6*3?
I do not see why we would have to multiply the base by the function?
Should the method not just return the answer as the base never changes and once n==1 the base case executes
Let's calculate powerN(3,3) as you did.
powerN(3,3) = 3 * powerN(3,2)
3 * powerN(3,2) = 3 * 3 * powerN(3,1)
3 * 3 * powerN(3,1) = 3 * 3 * 3 = 27
So it was just wrong the way you calculated.
Let's say we have powerN(2,3)
powerN(2,3):
call#1> return 2*powerN(2,3-1)
call#2> return 2*powerN(2,2-1)
call#3> (n==1)return 2*1
So,
call#3 returns 2 to call#2 [call#2 is now returning 2*2=4]
call#2 returns 4 to call#1 [call#1 is now returning 2*4=8]
That's it.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 3 years ago.
Improve this question
This is my attempt to find a well performing algorithm for problem 5 Project Euler - find the smalles possible number that is evenly divisible x and all the numbers below x.
I've tried using one loop make #s and another to test if that number is evenly divisible by x and all #s below x
System.out.println("This program finds the smallest positive "
+ "number that");
System.out.println("is evenly divisible by all of the numbers "
+ "from 1 to N.");
System.out.print("Enter N: ");
int N=kb.nextInt();
long bigN=1;
for(int i=1;i<=N;i++){
bigN=bigN*i;
/*bigN serves as a definite end for our loop
when trying to find all #s divisible from 1 to n
*/
}
long smallestAnswer=bigN;
int count=0;
for(long i=1;i<=bigN;i++){//# being tested
for(int j=1;j<=N;j++){//test
if(i%j==0){
count++;
}
if(count==N && i<smallestAnswer){
smallestAnswer=i;//should catch only the first/smallest answer
break;
}
}
count=0;
}
System.out.printf("\nThe smallest # evenly divisible by all of the "
+ "numbers from 1 to N");
System.out.printf("\nis %,d\n",smallestAnswer);
}
The code works. No run/compileTime errors. It's just far too slow. If the user enters a # bigger than 11, the code just freezes basically
You are using a brute-force algorithm. Challenges, like found on Project Euler, are more often challenges to find the right algorithm, not merely challenges to write the code.
The challenge here is to find the least common multiple (see Wikipedia), of all the numbers from 1 to X.
Example: If X is 10, one way to solve it is to identify the divisors:
1 = 1
2 = 2
3 = 3
4 = 2^2
5 = 5
6 = 2 * 3
7 = 7
8 = 2^3
9 = 3^2
10 = 2 * 5
The divisors for the least common multiple is therefore:
1 * 2^3 * 3^2 * 5 * 7 = 1 * 8 * 9 * 5 * 7 = 2520
Since this is a challenge for you to solve, I'll leave the coding to you.
I'm not quite sure why you're struggling with performance.
$ date && X 20 && date
Tue Jun 25 13:18:13 CDT 2019
N: 20
232792560 is divisible by all numbers 1 to 20
Tue Jun 25 13:18:16 CDT 2019
3 seconds for N == 20.
You are doing extra math for each number you check -- a LOT of extra math. Instead of doing the check for each number 1 to N, first, you could do from 2 to N, as all numbers are divisible by 1. But more importantly, you're doing ALL even if one fails. If you turn that portion around, breaking out of your "does this number work" code as soon as a modulus check fails. On N=20, this will save you 18 checks on all odd numbers.
You could also gain more improvements. The number must be even. So if n>1, you could start at 2 and increment by 2 instead of one. If n>=3, you could actually start at 6 and increment by 6, saving a LOT of math. And if n>=4, you could start at 12 and increment by 12.
For reference, here is my implementation.
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv) {
int n = atoi(argv[1]);
long trying = 1;
bool found = false;
while(!found) {
found = true;
++trying;
for (long checkDivide = 2; checkDivide <= n; +checkDivide) {
if (trying % checkDivide != 0) {
found = false;
break;
}
}
}
printf("%ld is divisible by all numbers 1 to %d\n", trying, n);
return 0;
}
I skipped asking for input and just put the value on the command line.
Note that reversing the check also is probably more efficient. That is, start checking at n and work down to 2. X % 20 is going to fail more often than X % 2. I'm not using sufficient time check resolution to be sure how much more efficient it is.
This question already has answers here:
Int division: Why is the result of 1/3 == 0?
(19 answers)
Closed 3 years ago.
I hav to write a simple program in bluej that computes and prints the sum of the reciprocals of the first 10 positive integers. My code is
public static void main (String[] args){
System.out.println(1/1 + 1/2 + 1/3 + 1/4 + 1/5 + 1/6 + 1/7 + 1/8 + 1/9 +1/10);
}
It outputs the number 1. Can someone please explain why and tell me what the code should be?
My teacher said I shouldn’t use variables or anything.
Try 1.0/1.0 + 1.0/2.0 + etc.
The reason is the compiler interprets 1/2 as an integer divided by an integer which will return an integer (the floor of the true value). 1.0/2.0 is interpreted as a float/float which will return a float.
Google 'C integer arithmetic' for more.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Implementation of fibonacci in Java:
public class Fibonacci {
private int fibonacci(int n){
return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
}
private void test(){
System.out.println(fibonacci(4));
}
public static void main(String[] args) {
Fibonacci test = new Fibonacci();
test.test();
}
}
Using Oracle Java 8 on Ubuntu, it prints out 3 in Eclipse. But I was expecting 5:
fibonacci(4)
fibonacci(3) fibonacci(2)
fibonacci(2) fibonacci(1) fibonacci(1) fibonacci(0)
fibonacci(1) fibonacci(0) 1 1 1
1 1
So then what's wrong with my Java implementation?
Your fibonacci(int n) in Java will return 0 for n=0. You want to always return 1 for n<2. Replace:
return n < 2 ? n : fibonacci(n - 1) + fibonacci(n - 2);
with
return n < 2 ? 1 : fibonacci(n - 1) + fibonacci(n - 2);
As it currently stands, the following is evaluated: (f is short for fibonacci)
f(4)
f(3)+f(2)
(f(1)+f(2))+(f(0)+f(1))
(1+(f(0)+f(1)))+(0+1)
(1+0+1)+(0+1)
2+1
3
The following should occur:
f(4)
f(3)+f(2)
(f(1)+f(2))+(f(0)+f(1))
(1+(f(1)+f(1)))+(1+1)
(1+1+1)+(1+1)
3+2
5
This has nothing to do with the language.
The first returns the sequence 0 1 1 2 3 5 8 13 ...
The second returns the sequence 1 1 2 3 5 8 13 ...
It's up to you to decide which sequence you want.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am really struggling with this recursive question. Can anyone help me solve the recurrence T(n) = 5T (n/5)+5 with the base condition T(1) = 0 via closed-form formula? It is given that n = 5^m with the integer m = log5 n.
It will be sufficient to compute T(5n) for n >= 0. For all other values of x, T(x) will equal T(y) where y is the largest power of 5 smaller than x, since the calculations are the same. (I'm assuming that when you write n/5 you mean integer division, i.e. floor(n/5).)
Then:
T(50) = 0
T(51) = 5 * 0 + 5 = 5
T(52) = 5 * 5 + 5 = 52 + 51
T(53) = 5 * (5 * 5 + 5) + 5 = 53 + 52 + 51
... which leads to:
T(5n) = 5n + 5n-1 + ... + 52 + 51
which, using a high-school algebra formula (sum of a geometric series), is
T(5n) = (5n+1 - 5) / 4
If you're thinking about time complexity, notice that T(x) will always be less than or equal to 5x / 4. And since we don't worry about constant factors when expressing things in O-notation, this essentially means T(x) = O(x).
A non constructive way to solve this: looking a bit at the formula one guesses that T(5m) = (5m+1-5)/4. This can be shown by induction:
it is correct for m=0: T(1) = 0
assuming it is correct for m we show it for m+1: T(5m+1) = T(5*5m) = 5T(5m)+5 = 5*((5m+1-5)/4)+5 = (5m+2-25)/4+5 = (5m+2-5)/4.
Therefore it is correct for all m.