ප්‍රථමක සංඛ්‍යා

Error365!

Well-known member
  • Jun 27, 2012
    13,009
    9,596
    113
    එක තැනක නෑ
    import java.math.BigInteger;
    import java.util.LinkedHashMap;
    import java.util.Map;

    LinkedHashMap එකෙන් ඔබ්ජෙක්ට් එකක් හදල තියෙනව මුලම. මම ඒක උඹ පෙන්නපුහමයි දැක්කෙ..

    එතකොට අනිත් දෙකෙන් කොතනද හදල තියෙන්නෙ? මට හොයාගන්ඩ බෑනෙ
    බිග් ඉන්ටිජර් ක්ලාස් එකයි, මැප් ක්ලාස් එකයි..


    red color karala thiyanne Map Object eka
    blue color karala thiyanne BigInteger Objects

    umba BigInterger kiyala class eka use karanawa nam onama dekata aniwaryenma "import java.math.BigInteger;" me widihata import karaganna one.

    Map class ekatath same

    Code:
    mport java.math.BigInteger;
    import java.util.LinkedHashMap;
    import java.util.Map;
    
    public class powCalc {
    
    public static void main(String[] args) {
    
    powCalc pc = new powCalc();
    
    LinkedHashMap<[COLOR="blue"]BigInteger[/COLOR], String> powLst = new LinkedHashMap<>();
    LinkedHashMap<[COLOR="blue"]BigInteger[/COLOR], String> dupLst = new LinkedHashMap<>();
    
    //variables (change this values as your requirement )
    int base = 100;//max base
    int power = 100;//max power
    boolean onlyPrimeNums = true;//use only prime numbers or not
    boolean showPow = true;//show or not output of power calc
    
    for (int i = 2; i < base; i++) {
    if (onlyPrimeNums) {
    if (pc.isPrime(i)) {
    for (int j = 2; j < power; j++) {
    BigInteger pow = pc.pow([COLOR="blue"]BigInteger[/COLOR].valueOf(i), [COLOR="blue"]BigInteger[/COLOR].valueOf(j));
    String nb = i + "^" + j;
    if (showPow) {
    System.out.println(nb + " = " + pow);
    }
    if (powLst.containsKey(pow)) {
    dupLst.put(pow, nb);
    } else {
    powLst.put(pow, nb);
    }
    }
    }
    } else {
    for (int j = 2; j < power; j++) {
    [COLOR="blue"]BigInteger [/COLOR]pow = pc.pow([COLOR="blue"]BigInteger[/COLOR].valueOf(i), [COLOR="blue"]BigInteger[/COLOR].valueOf(j));
    String nb = i + "^" + j;
    if (showPow) {
    System.out.println(nb + " = " + pow);
    }
    if (powLst.containsKey(pow)) {
    dupLst.put(pow, nb);
    } else {
    powLst.put(pow, nb);
    }
    }
    }
    }
    
    if (dupLst.size() > 0) {
    for ([COLOR="Red"]Map[/COLOR].Entry<[COLOR="blue"]BigInteger[/COLOR], String> entry : dupLst.entrySet()) {
    [COLOR="blue"]BigInteger [/COLOR]pow = entry.getKey();
    String bas = entry.getValue();
    System.out.println(powLst.get(pow) + " overlap with " + bas + "[" + pow + "]");
    
    }
    }else{
    System.out.println("No value is overlapped");
    }
    
    }
    
    private [COLOR="blue"]BigInteger [/COLOR]pow([COLOR="blue"]BigInteger [/COLOR]base, BigInteger exponent) {
    BigInteger result = [COLOR="Blue"]BigInteger[/COLOR].ONE;
    while (exponent.signum() > 0) {
    if (exponent.testBit(0)) {
    result = result.multiply(base);
    }
    base = base.multiply(base);
    exponent = exponent.shiftRight(1);
    }
    return result;
    }
    
    private boolean isPrime(int n) {
    if (n > 2 && n % 2 == 0) {
    return false;
    }
    int top = (int) Math.sqrt(n) + 1;
    for (int i = 3; i < top; i += 2) {
    if (n % i == 0) {
    return false;
    }
    }
    return true;
    }
    }
     

    Error365!

    Well-known member
  • Jun 27, 2012
    13,009
    9,596
    113
    එක තැනක නෑ
    BigInteger pow = entry.getKey()

    මේක මම දන්නව

    entry කියන එකේ getKey කියන මෙතඩ් එක කෝල් කරල, ඒකෙන් එන වැලියු එක
    pow කියන එකට දාගන්ඩ

    entry එක කොහෙද තියෙන්නෙ :confused:

    red color walin thiyanne entry eka.

    Code:
    for (Map.Entry<BigInteger, String> [COLOR="Red"]entry [/COLOR]: dupLst.entrySet()) {
    BigInteger pow = entry.getKey();
    String bas = entry.getValue();
    System.out.println(powLst.get(pow) + " overlap with " + bas + "[" + pow + "]");
    
    }
     

    Error365!

    Well-known member
  • Jun 27, 2012
    13,009
    9,596
    113
    එක තැනක නෑ
    private boolean isPrime(int n) {
    if (n > 2 && n % 2 == 0) {
    return false;

    මේ තියෙන්නෙ මෙතඩ් එකක්

    කෝ ඉතිං මේ මෙතඩ් එක කෝල් කරල තියෙන්නෙ කොහෙද. මට ඇස් පෙන්නෙ නැද්ද ?

    red color walin thiyana thana thamai method eka call karala thiyanne

    blue color walin thiyanne class eke Object eka. e object eken thamai non static method eka (isPrime()) access karanne.

    Code:
    for (int i = 2; i < base; i++) {
    if (onlyPrimeNums) {
    if ([COLOR="Blue"]pc.[/COLOR][COLOR="Red"]isPrime(i)[/COLOR]) {
    for (int j = 2; j < power; j++) {
    BigInteger pow = pc.pow(BigInteger.valueOf(i), BigInteger.valueOf(j));
    String nb = i + "^" + j;
    if (showPow) {
    System.out.println(nb + " = " + pow);
    }
    if (powLst.containsKey(pow)) {
    dupLst.put(pow, nb);
    } else {
    powLst.put(pow, nb);
    }
     

    20_18

    Member
    Feb 27, 2018
    1,297
    45
    0
    powCalc pc = new powCalc();

    LinkedHashMap<BigInteger, String> powLst = new LinkedHashMap<>();
    LinkedHashMap<BigInteger, String> dupLst = new LinkedHashMap<>();

    මේ තියෙන්නෙ ඔබ්ජෙක්ට් හදපුව new keyword එකෙන්

    එතකොට බිග්ඉන්ටිගර් ඔබ්ජෙක්ට් එක හදපු තැන?
    මැප් ඔබ්ජෙක්ට් එක හදපු තැන ?
     

    20_18

    Member
    Feb 27, 2018
    1,297
    45
    0
    or (int i = 2; i < base; i++) {
    if (onlyPrimeNums) {
    if (pc.isPrime(i)) {
    for (int j = 2; j < power; j++) {
    BigInteger pow = pc.pow(BigInteger.valueOf(i), BigInteger.valueOf(j));

    රතු කරපු ටිකෙන් මොකක්ද වෙන්නෙ ?

    BigInteger කියන්නෙ ක්ලාස් එකක් නෙමේද? ඒකෙ ඔබ්ජෙක්ට් එකක් හැදුවද :confused:
     

    20_18

    Member
    Feb 27, 2018
    1,297
    45
    0
    මොකක්ද යකෝ මේ for එක :confused:

    for (Map.Entry<BigInteger, String> entry : dupLst.entrySet())
     

    20_18

    Member
    Feb 27, 2018
    1,297
    45
    0
    ඔහොමයි මට Error365!, JAVA කියල දුන්නෙ... :angry:

    මගෙන් කවුරුහරි සිය පාරක් ඇහුවත් මම කියල දෙනව..

     

    20_18

    Member
    Feb 27, 2018
    1,297
    45
    0
    or (int i = 2; i < base; i++) {
    if (onlyPrimeNums) {
    if (pc.isPrime(i)) {
    for (int j = 2; j < power; j++) {
    BigInteger pow = pc.pow(BigInteger.valueOf(i), BigInteger.valueOf(j));

    රතු කරපු ටිකෙන් මොකක්ද වෙන්නෙ ?

    BigInteger කියන්නෙ ක්ලාස් එකක් නෙමේද? ඒකෙ ඔබ්ජෙක්ට් එකක් හැදුවද :confused:

    මෙතන වෙලා තියෙන්නෙ pc කියන ක්ලාස් එකේ pow මෙතඩ් එක රන් කරල, ඒකෙන් එන රිසල්ට් එක pow කියන බිග්ඉන්ටිජර් එකට දාන්ඩ කියන එක..

    මේක මට කවුරුත් කියල දුන්නෙ නෑ.. :angry:
     

    Error365!

    Well-known member
  • Jun 27, 2012
    13,009
    9,596
    113
    එක තැනක නෑ
    ඔහොමයි මට Error365!, JAVA කියල දුන්නෙ... :angry:

    මගෙන් කවුරුහරි සිය පාරක් ඇහුවත් මම කියල දෙනව..


    මෙතන වෙලා තියෙන්නෙ pc කියන ක්ලාස් එකේ pow මෙතඩ් එක රන් කරල, ඒකෙන් එන රිසල්ට් එක pow කියන බිග්ඉන්ටිජර් එකට දාන්ඩ කියන එක..

    මේක මට කවුරුත් කියල දුන්නෙ නෑ.. :angry:


    Ado mata thread eka miss una ban. Sorry ban aiyandi!!