I have an array of 100 numbers and I only gave the array even values. How can I print out how many elements of the array I have to add to obtain a sum < than 1768 using a WHILE LOOP? The following is what I have so far and I am stuck... thanks in advance for the help
void setup() {
int[] x = new int[100];
int i=0;
int sum=0;
for(i=0; i<100; i++) {
if (i%2==0) {
x[i]=i;
sum+=x[i];
}
}
}
void setup() {
int i = 0;
int sum = 0;
int counter = 0;
while (sum < 1768) {
sum += i;
i += 2;
counter++;
}
System.out.println(counter);
}
You start with the even index of 0. Then just skip odd numbers by using i += 2.
If the number of elements is limited with 100, add i < 200 to the while condition:
while (sum < 1768 && i < 200)
The array of 100 even numbers will contain numbers from 0 to 200.
The variable counter will contain the number of additions performed. Its value will be equal to i / 2, so you can remove that additional variable.
You can use this loop and element number would be i+1.
for(int i=0,k=0; k<1768; i++,k+=x[i]) {
System.out.println(x[i]+" - "+k);
}
While loop -
int i=0,k=0;
while(k<1768; ) {
System.out.println(x[i]+" - "+k);
i++,k+=x[i];
}
You are skipping indexes in your array.You are only filling every other 'slot'
Also, it would probably be easier to use a while loop to check against your max value (1728)
int[] x = new int[100];
int i = 0;
int sum = 0;
int max = 1728;
while (sum < max && i < 100)
{
x[i] = i*2;
if ((x[i] + sum) < max)
{
sum += x[i];
}
i++;
}
void setup() {
int[] x = new int[100];
int maxValue = 1768;
int i;
int sum=0;
while(sum<maxValue) {
if (i%2==0) {
x[i]=i;
sum+=x[i];
i++;
}
}
System.out.println(i+" Elements needed")
}
following is code:
void setup() {
int[] x = new int[100];
int i=0;
int sum=0;
for(i=0; i<100; i++) {
if (i%2==0) {
sum += i;
if(sum<1768){
num +=1;
}
}
}
}
Related
Hi Im new to Java Im trying to make a counter. What Id like to do is calculate the numbers from 1 to x, for example if x is 5, calculate 1 + 2 + 3 + 4 + 5.
This is what I wrote, but I get 6 as output instead of 15.
public class Counter {
public int count (int x){
int contatore = 0;
int sum = 0;
for (int i = 0; i <= x; i++) {
sum = i;
System.out.println(i);
}
System.out.println(sum);
return sum;
}
}
You need to add i to the sum in each iteration of the loop:
public int count (int x) {
int contatore = 0;
int sum = 0;
for (int i = 0; i <= x; i++) {
sum += i;
System.out.println(i);
}
System.out.println(sum);
return sum;
}
You are assigning the loop variable to the sum - you need to add it to the sum
public int count (int x){
int sum = 0;
for (int i = 0; i <= x; i++) {
sum = sum + i;
}
return sum;
}
As others have noted, you can use the following shorthand notation for addition with assignment.
sum += i; // equivalent to sum = sum + i;
I need to print all prime numbers from 1 to 1,000,000 and print all even numbers from 4 to 10,000 and two prime numbers that sum to it.
I have a sieve method that changes all non-prime numbers in an array to a 0 (the problem specifically asks for this to be done), and I need to use a goldbach method that passes this array and displays all even numbers from 4 to 10,000 and two primes that sum up to that number.
The point of the goldbach portion of the problem is to print the numbers efficiently, and I am pretty sure my solution uses a polynomial time search when the correct solution is to be done with a linear time search. Any clue on how I might optimize this?
import java.lang.Math;
public class sieveAndGoldbach {
public static void sieve(int[] a) {
int n = a.length;
a[0] = 0;
for (int i = 1; i <= Math.sqrt(n); i++) {
if (a[i] != 0) {
for (int j = a[i]*a[i]; j <= n; j+=a[i]) {
a[j-1] = 0;
}
}
}
}
public static void goldbach(int[] a) {
int max = 10000;
for (int i = 4; i <= max; i += 2) {
int count = 0;
for (int j = 0; j < i/2; j++) {
if (a[j] != 0) {
int difference = i-a[j];
for (int k = 0; k < max; k++) {
if (a[k] == difference && count == 0) {
System.out.println(i + " = " + a[j] + " + " + (difference));
count++;
}
}
}
}
}
}
public static void main(String[] args) {
//initialize and fill array from 1 to n
int n = 1000000; //initially one million GOLDBACH METHOD WILL NOT WORK FOR n < 10,000
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = i + 1;
}
//Call sieve method on array a, then print all primes, not the zeros
sieve(a);
for (int i = 0; i < n; i++) {
if (a[i] != 0) {
System.out.print(a[i]);
System.out.print(" ");
}
}
System.out.print("\n");
//Call goldbach method on array a
goldbach(a);
}
}
You currently seem to be iterating through the array of primes for each prime looking for one that sums to your target. That's not necessary; you just need to check whether the difference is a prime:
int[] primes;
int target;
for (int i = 2; i < target / 2; i++) {
if (primes[i] != 0 && primes[target - i] != 0)
...
}
Beyond that I can't see a lot of obvious optimisation but there may well be some numerical analysis that allows you to target likely primes first.
Can someone please explain the thought process behind this code? I am kind of confused on how it works. This is the question that the code is addressing:
Write code (using one or more loops) to fill an array "a" with 10 different random numbers between 1 and 10.
Thank you so much for any help!
public static void main(String[] args){
//R8.8
int a[] = new int[10];
Random randomGenerator = new Random();
for (int i = 0; i < a.length; i++){
a[i] = 1 + randomGenerator.nextInt(100);
}
for (int i = 0; i < a.length; i++) {
int number = 1 + randomGenerator.nextInt(100);
int count = 0;
for (int j = 0; j < i; j++) {
if (a[j] == number) {
count += 1;
}
}
if (count > 0) i -= 1;
else a[i] = number;
}
}
}
See my comments in the code itself:
public static void main(String[] args){
//make a new array of 10 integers
int a[] = new int[10];
//declare an object which we can use to generate random numbers
//this object probably uses the system time to generate numbers that appear random
//but at the end of the day, java does it for us so
//we don't really need to know or care how it generates random numbers
Random randomGenerator = new Random();
//loop over each element in our array
for (int i = 0; i < a.length; i++){
//for each element, set that element to a random between 1 and 100 inclusive
//nextInt(x) gets a number between 0 (inclusive) and x (not inclusive)
//so to translate that to 1 to x inclusive, we need to add 1 to the result
a[i] = 1 + randomGenerator.nextInt(100);
}
//everything below here does literally nothing to solve the problem
//everything you need to fill the array with random numbers is above
for (int i = 0; i < a.length; i++) {
int number = 1 + randomGenerator.nextInt(100);
int count = 0;
for (int j = 0; j < i; j++) {
if (a[j] == number) {
count += 1;
}
}
if (count > 0) i -= 1;
else a[i] = number;
}
}
}
Please note that you should use 1 + randomGenerator.nextInt(10); to fill the array with numbers between 1 and 10, not 1 + randomGenerator.nextInt(100);.
I have this function for selection sort:
public static void selectionSort(int[] n)
{
for(int start = 0; start < n.length; start++)
{
int smallest = start;
for(int i = start+1; i<n.length; i++)
{if(n[i]<n[start])
smallest = i;}
int tmp = n[start];
n[start] = n[smallest];
n[smallest] = tmp;
}
}
and i call it like this.
Random ran = new Random();
int n = 10;
int[] a = new int[n];
for(int i = 0; i<n; i++)
a[i] = ran.nextInt(1000);
However, it causes results like this..
640 900 610 168 650 610 527 356 802 486
486 640 356 168 610 527 610 650 802 900
the top one is not sorted the lower one is supposed to be sorted. However, it is not correct.
You are comparing the initial start index every singe iteration, even if you have found a smaller number you are still comparing it to the original start which should not happen. Once you find a smaller number you need to use that index for the comparison.
To compare it to the smallest number every iteration change (n[i]<n[start]) to (n[i]<n[smallest]) and that will fix your problem.
Hope this helps!
The second loop is useless because you are comparing, after all, just the first and the last item and smallest takes it's value from this comparison only.
By your code I guess you're trying to do a Bubble Sort, but the comparison and the index management is wrong, here is a posible solution:
public static void main(String[] args) {
Random ran = new Random();
int n = 10;
int[] array = new int[n];
for (int i = 0; i < n; i++) {
array[i] = ran.nextInt(1000);
}
printArray(array);
selectionSort(array);
printArray(array);
}
private static void printArray(int[] array) {
System.out.print("Array: ");
for (int i : array) {
System.out.print(i + " ");
}
System.out.println();
}
private static void selectionSort(int[] array) {
for (int j = 0; j < array.length; j++) {
// Subtract 1 so you don't get a NullPointerException
// Subtract j so you don't compare the numbers already ordered
for (int i = 0; i < array.length - 1 - j; i++) {
if (array[i] > array[i + 1]) {
int tmp = array[i];
array[i] = array[i + 1];
array[i + 1] = tmp;
}
}
}
}
The algorithm is taken from the Spanish version (I'm from Argentina) of the link above, which is also in the English version with other notation.
Hope this helps. Best regards
Try to use this version of code it's functional:
public static void main(String[] args) {
Random ran = new Random();
int n = 10;
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = ran.nextInt(1000);
selectionSort(a);
for (int i : a) {
System.out.print(i+" ");
}
}
public static void selectionSort(int[] n)
{
for(int start = 0; start < n.length-1; start++)
{
int smallest = start;
for(int i = start+1; i<n.length; i++)
{if(n[i]<n[smallest])
smallest = i;}
int tmp = n[start];
n[start] = n[smallest];
n[smallest] = tmp;
}
}
So this is what I have:
public static void Positive () {
int limit = 50;
for(int i=1; i <= limit; i++){
System.out.print(i + ", ");
}
}
That prints out all numbers 1-50 with a loop
How do I return the sum of this using a loop?
I changed the return type to int.
public static int Positive () {
int limit = 50;
int sum = 0;
for(int i=1; i <= limit; i++){
System.out.print(i + ", ");
sum += i;
}
return sum;
}
Another approach
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum += i;
}
You havent cleared yet that you want to return the value or just print it from the function so you can use either of the approach in answers of your question. One with int type can return the value and print as well
Here is what you can do
public static void Positive () {
int limit = 50;
int total = 0;
for(int i=1; i <= limit; i++){
total = total + i;
}
System.out.print(total);
}