mardi 28 avril 2015

for loop unintentionally breaking at if statement

I am trying to determine the largest prime factor of a number by working backwards from the largest possible factor. Once a factor is found, I test to see if it is prime by using the PrimeTest3 function within the original function.

However, it is not giving me the answer I am expecting for the number 13195. When I test the code as shown above using the 'this passed the test' statement, I can see that it is skipping from the first factor found (2639) to the last factor found (5), and strangely, when logging the the result of passing i through PrimeTest3, it shows up as false, even though it had to have been true to pass the if statement in the first place.

var largestPrimeFactor3 = function (num) {
    function PrimeTest3(a){
        if (a<=1 || a%1!=0) 
            return false;

        limit = Math.ceil(Math.pow(a,.5));

        if (a%2==0 || a%3==0) 
            return false;
        if (a==2 || a==3) 
            return true;

        for (i=6;i<limit;i+=6){
            if (a%(i-1)==0) 
                return false;
            if (a%(i+1)==0) 
                return false;
        }
        return true;
    }
    for(var i = Math.floor(num/2); i>0; i--){
        console.log(i);
        if(num % i === 0 && PrimeTest3(i)){
            console.log('this passed the test:' + PrimeTest3(i));
            return true;
        }
    }
}
console.log(largestPrimeFactor3(13195));

Would really appreciate any help or clarification. Thanks!!

Add offset in Libre Office

I would like to add a constant offset in case the values I obtain in a cell are negative. Any suggestions on how to do that in Libre Office?

Thanks!

Java: if, switch or question mark double point

Which of the following methods is the fastest?

int i;
String s;

1. If Else

if(i == 0)
    s = "Case A"
else if(i == 1)
    s = "Case B"
else
    s = "Case C"

2. Switch

switch(i) {
    case 0:
        s = "Case A"; break;
    case 1:
        s = "Case B"; break;
    default:
        s = "Case C"; break;
}

3. ? and :

s = (i == 0 ? "Case A" : (i == 1 ? "Case B" : "Case C"))

Also, are method 1 and 3 compiled with the same output?

how to write nested loops that prints the capital letter F using *

  for (int i = 1; i <= 7; i++)
     if (i < 3 || i == 4) for (int j = 1; j <= 5; j++)
     System.out.print("*");
    if (1 > 2 && 1 < 4 || 1 > 4 && 1 <= 7) for (int k = 1; k <= 2; k++)
     System.out.print("*");
    System.out.print("");
    }
  }


**************
*****
*****
**************
**************
*****
*****
*****
*****

i want do like this using (for and if).

if Statmen fails in actionListener [duplicate]

This question already has an answer here:

so i have a problem the if statement at the end of the program seems to not respond when i type in josip in the JTextField it just doesn't react i tried writing hundreds of different combinations but it just isnt reacting i don't know where is the problem can someone please take a look at my code and tell me what is wrong

import javax.swing.JTextField;
import javax.swing.JOptionPane; 
import javax.swing.JFrame; 
import java.awt.CardLayout;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JButton; 
import javax.swing.JLabel;
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent;
import javax.swing.SpringLayout;
import java.awt.event.*;


class start extends JFrame{




public static void main(String args[]){

final String x[]={"Josip","Johan","Lohan"};
final int xl = x.length;


//Created Obecjts
JFrame frame = new JFrame();

final JPanel p = new JPanel();
final JButton b1 = new JButton("OK");
final JTextField ime = new JTextField(15);
final JTextField pass = new JTextField(15);
final JLabel label1 = new JLabel("UpisiSvojeIme");
final JLabel label2 = new JLabel("UpisiSvojuSifru");

//Panel Layout 
p.setLayout(null);

//Frame options
frame.setVisible(true);
frame.setSize(300,150);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.add(p);

//Objects added to the panel 

ime.setBounds(120,15,100,20);
p.add(ime);

label1.setBounds(10,15,150,20);
p.add(label1);



pass.setBounds(120,55,100,20);
p.add(pass);

label2.setBounds(10,55,150,20);
p.add(label2);


b1.setBounds(230,93,65,20);
 p.add(b1);


 b1.addActionListener(new ActionListener(){

public void actionPerformed(ActionEvent e){

String name = ime.getText();

System.out.println(name);

if(name == "JOSIP"){
    System.out.println("JEEEEJ");
 }



}








}

);


}
}

How do I format the following if/else in Jasper?

I am trying to format the following if/else in Jasper and having trouble. This is what I have but my syntax is wrong can someone help?

$F{PAYMENT_METHOD_DATA}  == {"deposit_name":"PayNearMe","is_important":true,"logo":"https:\/\/nj-cdn-casino.ballyinteractive.com\/cms\/V3\/Deposit-Methods\/icon-Paynearme.png"} ? "PayNearMe" :
$F{PAYMENT_METHOD_DATA} ==  {"deposit_name":"ACH (Optimal)","withdrawal_name":"ACH (Optimal)","display_order":"101","is_important":true} ? "ACH" :
$F{PAYMENT_METHOD_DATA} ==  {"deposit_name":"wire transfer","withdrawal_name":"wire transfer","display_order":"42","is_important":true} ? "Wire Transfer" :   
$F{PAYMENT_METHOD_DATA} ==  {"deposit_name":"Personal check","withdrawal_name":"Personal check","display_order":"6","is_important":true} ? "Personal Check" :
$F{PAYMENT_METHOD_DATA} ==  {"deposit_name":"Cashier''s check","withdrawal_name":"Cashier''s check","display_order":"7","is_important":true} ? "Cashiers Check": "Cash"

mysql if inside select with aggregate functions

I have an inventory table that lists quantities at multiple locations. I keep a quantity value and I now have to keep a "threshold" value, and when I reach the threshold stop selling the product. It's just a fudge factor so when people have inaccurate inventory there is a little buffer to make sure they don't over commit sales.

SELECT product, sum(quantity) as pooled, max(quantity) as maxincart FROM `inventory` WHERE location=1 OR location=2 OR location=3 GROUP by product

The main problem with the queries I have tried is that they sum all of my locations. That was fine before thresholds. Now locations that I'd rather ignore are included in my sums, I would like an IF to remove them from the aggregate functions.

Sometimes it works fine. 
locationA has 7 and a threshold of 3.
locationB has 9 and a threshold of 4.

Sum it all up, and it's 16 - 7 = 9 Works fine!

But then there are situations like this
locationA has 7 and a threshold of 3.
locationB has 0 and a threshold of 4. 

Sum it all up, and it's 7 - 7 = 0. LocationA wants to sell their 4 but can't now.

Is there a way to nest an if inside a query so that when a location's qty-threshold <=0 then it will be excluded from the sum? I'm trying to do it in mysql to keep it quick, found some good info about using in select, but combining it with the group by and aggregate functions has me stuck.

The table I'm querying is an intermediate table and can be restructured. So I'm open to ideas about changing the table to make it work. I just want it to be quick so I don't want to have to do another query inside each product. I've considered eliminating the group by, ordering by product, storing values in an array, and when the product changes running through the array with this logic...but that also means a much larger loop and more memory used. I'm hoping some mysql genius out there can lend a hand and help. Thanks!