java求完數

完全數必以6或者 28結尾,而且除了6各數位之和相加直到變成個位數則一定是1,也就是說一定是9的倍數+1,通過這些性質可以極大縮小遍歷范圍,降低時間復雜度

⑵ java求100以內完數。

for(intm=1;m<=100;m++)//從1開始取值
{
ints=0;//定義整型S
for(inti=1;i<m;i++)//
if(m%i==0)//如果M除以i整除
s=s+i;
if(s==m){//如果S=M
System.out.print(s+"");//輸出內完容數
}
}

⑶ java求完數 且輸出其因子

先要將這個數的每個因子存儲到一個數組
然後,判斷這個數是不是完數
如果是,則按格式輸出就可以了!

⑷ java 完數程序

classforDemo2
{
publicstaticvoidmain(String[]args)
{
內intsum;
inti=2;
while(i!=1001)
{
sum=0;//每次循環sum重新賦容值為0
for(intj=1;j<i;j++)
{
if(i%j==0)sum+=j;
}
if(i==sum)System.out.println(i+"");
i++;
}
}
}

⑸ Java 完數的代碼

class PerfectNumber {
public static void main(String[] args) {
for (int i = 2; i <= 1000; i++)
if (isPerfectNumber(i))
System.out.println(i);
}

static boolean isPerfectNumber(int i) {
int sumof = 0;
int j;
for (j = 1; j <= i / 2; j++) {
if (i % j == 0) {
sumof += j;
}
}
return i == sumof;
}
}

⑹ java判斷完數

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Prefectnumber {

private JFrame frame;
private JPanel panel,bp;
private JTextField tField;
private JButton button;
private JTextArea tArea;

public Prefectnumber() {
frame = new JFrame();
panel = new JPanel();
bp = new JPanel();
tField = new JTextField();
button = new JButton("確定");
tArea = new JTextArea(5,40);

init();
addActionHandle();
}

private void init() {
panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
panel.add(tField);
bp.add(button);
panel.add(bp);
panel.add(tArea);

frame.add(panel);
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}

private void addActionHandle() {
button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
String temp = tField.getText();
if (temp.equals("")||temp==null) {
tArea.setText("請輸入你要判斷的數字!");
return;
}else {
int t;
try {
t = Integer.parseInt(temp);
} catch (NumberFormatException e1) {
tArea.setText("你輸入的數字不合法!請輸入合法數字");
return;
}
String[] al= isPrefectnumber(t);
if (al.length!=0) {
StringBuffer sb = new StringBuffer();
sb.append(t+"是完數,其因子為");
for (int i = 0; i <al.length ; i++) {
sb.append(","+al[i]);
}
tArea.setText(sb.toString());
} else {
tArea.setText(t+"不是完數");
}
}
}
});
}

private String[] isPrefectnumber(int n){
int sum=0;

StringBuffer sBuffer = new StringBuffer();;
for (int i = 1; i < n; i++) {
if (n % i == 0){
sum += i;
sBuffer.append(","+i);
}
}
if (sum==n) {
return sBuffer.toString().replaceFirst(",", "").split(",");
}else {
return new String[0];
}
}

public static void main(String arg[]) {
new Prefectnumber();
}
}
前面遇到過這樣的問題,你拿去跑跑,前面用的ArrayList他說不明白,現在改用StringBuffer和String數組了,這下能明白了吧

⑺ java完數

是不是你的變數sum
報錯???

你應該得給你的變數sum賦值初始值,因為你後面的if裡面進行判斷啦,如果不賦值的話,sum沒有初始值,怎麼進行判斷?

⑻ java語言判斷一個整數是否為完數

import java.util.Scanner;
public class Test1 {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
if(isFullNum(scanner.nextInt())) {
System.out.println("是完數");
}
else {
System.out.println("不是完數");
}
}
//判斷一個數字是不是完數,是的話返回true
public static boolean isFullNum(int num) {
int sum = 0;
for(int i = 1;i <= num/2;i++) {
if(num%i == 0)
sum+=i;
}
return sum == num;
}
}

⑼ java完數的代碼

public static void main(String[] args) {
int a, b, sum;
sum = 0;
for (a = 1; a <= 1000; a++) {
for (b = 1; b < a; b++) {
if (a % b == 0) {
// 此處用sum去累加了 如 sum =1 ,b =2, 則sum = 1 + 2;
sum = sum + b;
}
}
if (sum == a) {
System.out.println(sum);
}
// 如果這里不重置清零,則上方內sum = sum + b; 這行語句會將上一次值累加上去,就不是我容們想要的值了
sum = 0;
}
}